chanched handlers

This commit is contained in:
2023-08-12 19:26:50 +03:00
parent 85f4b92fa7
commit 17a09f7fb3
28 changed files with 503 additions and 1001 deletions

View File

@@ -2,20 +2,18 @@ package service
import (
"context"
"net/http"
cmsservice "go.unistack.org/cms-service"
cmsstorage "go.unistack.org/cms-service/storage"
httpsrv "go.unistack.org/micro-server-http/v4"
cmsstorage "go.unistack.org/cms-service/storage" // TODO
httpsrv "go.unistack.org/micro-server-http/v3"
"go.unistack.org/micro/v4"
"go.unistack.org/micro/v4/config"
microcfg "go.unistack.org/micro/v4/config"
"go.unistack.org/micro/v4/logger"
"go.unistack.org/micro/v4/options"
"go.unistack.org/micro/v4/register"
"go.unistack.org/micro/v4/server"
intcfg "go.unistack.org/unistack-org/pkgdash/config"
"go.unistack.org/unistack-org/pkgdash/handler"
"go.unistack.org/unistack-org/pkgdash/handler/encoders"
pb "go.unistack.org/unistack-org/pkgdash/proto"
"go.unistack.org/unistack-org/pkgdash/service/client_git"
"go.unistack.org/unistack-org/pkgdash/storage"
)
@@ -23,51 +21,44 @@ import (
func NewService(ctx context.Context) (micro.Service, error) {
var reg register.Register
if ctx == nil {
ctx = context.Background()
}
cfg := intcfg.NewConfig()
cs := cmsservice.NewConfigLocal(cfg)
cs := microcfg.NewConfig(config.Struct(cfg))
// TODO
mgsrv := httpsrv.NewServer(
options.Register(reg),
)
svc := micro.NewService(
micro.Config(cs...),
micro.Config(cs),
)
writer, err := handler.NewWriter(encoders.NewJSONProto())
if err != nil {
logger.Fatalf(ctx, "failed init writer: %v", err)
}
h := handler.NewHandler(svc, client_git.NewClient(5))
h := handler.NewHandler(svc, writer, client_git.NewClient(5))
if err = svc.Init(
if err := svc.Init(
micro.AfterStart(func(_ context.Context) error {
return h.Init(svc.Options().Context)
}),
micro.BeforeStart(func(ctx context.Context) error {
if err = config.Load(ctx, cs, config.LoadOverride(true)); err != nil {
if err := config.Load(ctx, []config.Config{cs}, config.LoadOverride(true)); err != nil {
return err
}
if err = config.Validate(ctx, cfg); err != nil {
if err := config.Validate(ctx, cfg); err != nil {
return err
}
if err = svc.Init(
micro.Name(cfg.Service.Name),
micro.Version(cfg.Service.Version),
if err := svc.Init(
micro.Name(intcfg.ServiceName),
micro.Version(intcfg.ServiceVersion),
); err != nil {
return err
}
if err := svc.Server("http").Init(
options.Address(cfg.App.Address),
options.Name(cfg.Service.Name),
server.Version(cfg.Service.Version),
options.Address(cfg.Address),
options.Name(cfg.App.Name),
server.Version(cfg.App.Version),
); err != nil {
return err
}
@@ -75,23 +66,17 @@ func NewService(ctx context.Context) (micro.Service, error) {
return nil
}),
micro.BeforeStart(func(_ context.Context) 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 {
level = logger.ParseLevel(v)
}
log := logger.NewLogger(
logger.WithLevel(level),
logger.WithLevel(logger.ParseLevel(cfg.LogLevel)),
logger.WithCallerSkipCount(3),
)
return svc.Init(micro.Logger(log))
}),
micro.BeforeStart(func(_ context.Context) error {
micro.BeforeStart(func(_ context.Context) error { // TODO
var connstr string
if v, ok := cfg.Storage.DSN[cfg.Service.Name]; ok {
if v, ok := cfg.StorageDSN[cfg.App.Name]; ok {
connstr = v
} else if v, ok = cfg.Storage.DSN["all"]; ok {
} else if v, ok = cfg.StorageDSN["all"]; ok {
connstr = v
}
scheme, dsn, err := cmsstorage.StorageOptions(connstr)
@@ -115,15 +100,15 @@ func NewService(ctx context.Context) (micro.Service, error) {
return nil, err
}
mux := http.NewServeMux()
//mux := http.NewServeMux()
mux.HandleFunc("/listPackage", handler.Methods(http.MethodGet, h.ListPackage))
mux.HandleFunc("/updatePackage", handler.Methods(http.MethodPost, h.UpdatePackage))
mux.HandleFunc("/addComment", handler.Methods(http.MethodPut, h.AddComment))
mux.HandleFunc("/addPackage", handler.Methods(http.MethodPost, h.AddPackage))
mux.HandleFunc("/getModule", handler.Methods(http.MethodGet, h.GetModule))
//mux.HandleFunc("/listPackage", handler.Methods(http.MethodGet, h.ListPackage))
//mux.HandleFunc("/updatePackage", handler.Methods(http.MethodPost, h.UpdatePackage))
//mux.HandleFunc("/addComment", handler.Methods(http.MethodPut, h.AddComment))
//mux.HandleFunc("/addPackage", handler.Methods(http.MethodPost, h.AddPackage))
//mux.HandleFunc("/getModule", handler.Methods(http.MethodGet, h.GetModule))
if err = svc.Server().Handle(svc.Server().NewHandler(mux)); err != nil {
if err := pb.RegisterPkgdashServiceServer(mgsrv, h); err != nil {
logger.Fatalf(ctx, "failed to register handler: %v", err)
}