63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.usbharu.dev/usbharu/unos/illust/data"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/reflection"
|
|
"gopkg.in/ini.v1"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"os/signal"
|
|
)
|
|
|
|
func main() {
|
|
load, err := ini.Load("config.ini")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
return
|
|
}
|
|
port := load.Section("gRPC").Key("port").MustInt(50051)
|
|
mongoUrl := load.Section("mongodb").Key("url").MustString("mongodb://localhost:27017")
|
|
|
|
listen, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
server := grpc.NewServer()
|
|
|
|
connect, err := mongo.Connect(context.Background(), options.Client().ApplyURI(mongoUrl))
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
return
|
|
}
|
|
|
|
database := connect.Database("illust-data")
|
|
|
|
data.RegisterIllustDataServiceServer(server, data.NewIllustDataServiceImpl(data.NewMongodbIllustDataRepository(*database)))
|
|
|
|
reflection.Register(server)
|
|
|
|
go func() {
|
|
log.Printf("start gRPC server port: %v", port)
|
|
server.Serve(listen)
|
|
}()
|
|
|
|
defer func() {
|
|
if err := connect.Disconnect(context.Background()); err != nil {
|
|
panic(err)
|
|
}
|
|
}()
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
signal.Notify(quit, os.Interrupt)
|
|
<-quit
|
|
|
|
server.GracefulStop()
|
|
}
|