1.路徑src\cmd\server.go
2.啟動服務cmd:
package cmd
import (
"context"
"database/sql"
"flag"
"fmt"
v1 "goWebGin/api/service/v1"
"goWebGin/server"
)
type Config struct {
GRPCPort string
DataStoreDBHost string
DataStoreDBUser string
DataStoreDBPassword string
DataStoreDBSchema string
}
func RunServer() error {
ctx := context.Background()
var cfg Config
flag.StringVar(&cfg.GRPCPort, "grpc-port", "", "gRPC port to bind")
flag.StringVar(&cfg.DataStoreDBHost, "db-host", "", "db host")
flag.StringVar(&cfg.DataStoreDBUser, "db-user", "", "db user")
flag.StringVar(&cfg.DataStoreDBPassword, "db-password", "", "db password")
flag.StringVar(&cfg.DataStoreDBSchema, "db-schema", "", "db schema")
flag.Parse()
param := "parseTime=true"
dsnDB := fmt.Sprintf("%s:%s@tcp(%s)/%s?%s",
cfg.DataStoreDBUser,
cfg.DataStoreDBPassword,
cfg.DataStoreDBHost,
cfg.DataStoreDBSchema,
param)
db, err := sql.Open("mysql", dsnDB)
if err != nil {
return fmt.Errorf("資料庫連線失敗,%s", err)
}
defer func() {
_ = db.Close()
}()
v1Api := v1.NewToDoServiceServer(db)
//v1Api 實現了 proto 的ToDoServiceServer介面
return server.RunServer(ctx, v1Api, cfg.GRPCPort)
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結