add storage and mux handle with handlers
This commit is contained in:
@@ -2,9 +2,14 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
cmsstorage "go.unistack.org/cms-service/storage"
|
||||
intcfg "go.unistack.org/unistack-org/pkgdash/config"
|
||||
"go.unistack.org/unistack-org/pkgdash/handler"
|
||||
pbmicro "go.unistack.org/unistack-org/pkgdash/proto/micro"
|
||||
"go.unistack.org/unistack-org/pkgdash/handler/encoders"
|
||||
"net/http"
|
||||
|
||||
//pbmicro "go.unistack.org/unistack-org/pkgdash/proto/micro"
|
||||
"go.unistack.org/unistack-org/pkgdash/storage"
|
||||
|
||||
cmsservice "go.unistack.org/cms-service"
|
||||
grpcsrv "go.unistack.org/micro-server-grpc/v3"
|
||||
@@ -49,7 +54,12 @@ func NewService(ctx context.Context) (micro.Service, error) {
|
||||
micro.Config(cs...),
|
||||
)
|
||||
|
||||
h := handler.NewHandler(svc)
|
||||
writer, err := handler.NewWriter(encoders.NewJSONProto())
|
||||
if err != nil {
|
||||
logger.Fatalf(ctx, "failed init writer: %v", err)
|
||||
}
|
||||
|
||||
h := handler.NewHandler(svc, writer)
|
||||
|
||||
if err := svc.Init(
|
||||
micro.AfterStart(func(_ context.Context) error {
|
||||
@@ -70,7 +80,7 @@ func NewService(ctx context.Context) (micro.Service, error) {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := svc.Server("grpc").Init(
|
||||
if err := svc.Server("http").Init(
|
||||
server.Address(cfg.App.Address),
|
||||
server.Name(cfg.Service.Name),
|
||||
server.Version(cfg.Service.Version),
|
||||
@@ -84,7 +94,7 @@ func NewService(ctx context.Context) (micro.Service, error) {
|
||||
level := logger.InfoLevel
|
||||
if v, ok := cfg.Logger.Level[cfg.Service.Name]; ok {
|
||||
level = logger.ParseLevel(v)
|
||||
} else if v, ok := cfg.Logger.Level["all"]; ok {
|
||||
} else if v, ok = cfg.Logger.Level["all"]; ok {
|
||||
level = logger.ParseLevel(v)
|
||||
}
|
||||
log := logger.NewLogger(
|
||||
@@ -93,11 +103,41 @@ func NewService(ctx context.Context) (micro.Service, error) {
|
||||
)
|
||||
return svc.Init(micro.Logger(log))
|
||||
}),
|
||||
micro.BeforeStart(func(_ context.Context) error {
|
||||
var connstr string
|
||||
if v, ok := cfg.Storage.DSN[cfg.Service.Name]; ok {
|
||||
connstr = v
|
||||
} else if v, ok = cfg.Storage.DSN["all"]; ok {
|
||||
connstr = v
|
||||
}
|
||||
scheme, dsn, err := cmsstorage.StorageOptions(connstr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, dbok := cmsstorage.FromContext(svc.Options().Context)
|
||||
if !dbok {
|
||||
db, err = cmsstorage.NewStorage(scheme, dsn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
store, err := storage.NewStorage(scheme, db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return svc.Init(micro.Context(cmsstorage.InterfaceNewContext(svc.Options().Context, store)))
|
||||
}),
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := pbmicro.RegisterDashboardServiceServer(mgsrv, h); err != nil {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/listPackage", h.ListPackage)
|
||||
mux.HandleFunc("/updateInfo", h.UpdateInfo)
|
||||
mux.HandleFunc("/addComment", h.AddComment)
|
||||
|
||||
if err = svc.Server().Handle(svc.Server().NewHandler(mux)); err != nil {
|
||||
logger.Fatalf(ctx, "failed to register handler: %v", err)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user