2023-08-07 21:30:30 +03:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-11 21:27:38 +03:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
cmsservice "go.unistack.org/cms-service"
|
2023-08-09 14:31:23 +03:00
|
|
|
cmsstorage "go.unistack.org/cms-service/storage"
|
2023-08-11 21:27:38 +03:00
|
|
|
httpsrv "go.unistack.org/micro-server-http/v4"
|
|
|
|
"go.unistack.org/micro/v4"
|
|
|
|
"go.unistack.org/micro/v4/config"
|
|
|
|
"go.unistack.org/micro/v4/logger"
|
|
|
|
"go.unistack.org/micro/v4/options"
|
|
|
|
"go.unistack.org/micro/v4/register"
|
2023-08-11 21:45:08 +03:00
|
|
|
"go.unistack.org/micro/v4/server"
|
2023-08-07 21:30:30 +03:00
|
|
|
intcfg "go.unistack.org/unistack-org/pkgdash/config"
|
|
|
|
"go.unistack.org/unistack-org/pkgdash/handler"
|
2023-08-09 14:31:23 +03:00
|
|
|
"go.unistack.org/unistack-org/pkgdash/handler/encoders"
|
2023-08-11 20:12:15 +03:00
|
|
|
"go.unistack.org/unistack-org/pkgdash/service/client_git"
|
2023-08-09 14:31:23 +03:00
|
|
|
"go.unistack.org/unistack-org/pkgdash/storage"
|
2023-08-07 21:30:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-08-11 21:27:38 +03:00
|
|
|
mgsrv := httpsrv.NewServer(
|
|
|
|
options.Register(reg),
|
2023-08-07 21:30:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
svc := micro.NewService(
|
|
|
|
micro.Config(cs...),
|
|
|
|
)
|
|
|
|
|
2023-08-09 14:31:23 +03:00
|
|
|
writer, err := handler.NewWriter(encoders.NewJSONProto())
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf(ctx, "failed init writer: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-08-11 20:12:15 +03:00
|
|
|
h := handler.NewHandler(svc, writer, client_git.NewClient(5))
|
2023-08-07 21:30:30 +03:00
|
|
|
|
2023-08-11 20:12:15 +03:00
|
|
|
if err = svc.Init(
|
2023-08-07 21:30:30 +03:00
|
|
|
micro.AfterStart(func(_ context.Context) error {
|
|
|
|
return h.Init(svc.Options().Context)
|
|
|
|
}),
|
|
|
|
micro.BeforeStart(func(ctx context.Context) error {
|
2023-08-11 20:12:15 +03:00
|
|
|
if err = config.Load(ctx, cs, config.LoadOverride(true)); err != nil {
|
2023-08-07 21:30:30 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-08-11 20:12:15 +03:00
|
|
|
if err = config.Validate(ctx, cfg); err != nil {
|
2023-08-07 21:30:30 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-11 20:12:15 +03:00
|
|
|
if err = svc.Init(
|
2023-08-07 21:30:30 +03:00
|
|
|
micro.Name(cfg.Service.Name),
|
|
|
|
micro.Version(cfg.Service.Version),
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-09 14:31:23 +03:00
|
|
|
if err := svc.Server("http").Init(
|
2023-08-11 21:27:38 +03:00
|
|
|
options.Address(cfg.App.Address),
|
|
|
|
options.Name(cfg.Service.Name),
|
2023-08-07 21:30:30 +03:00
|
|
|
server.Version(cfg.Service.Version),
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2023-08-09 14:31:23 +03:00
|
|
|
} else if v, ok = cfg.Logger.Level["all"]; ok {
|
2023-08-07 21:30:30 +03:00
|
|
|
level = logger.ParseLevel(v)
|
|
|
|
}
|
|
|
|
log := logger.NewLogger(
|
|
|
|
logger.WithLevel(level),
|
|
|
|
logger.WithCallerSkipCount(3),
|
|
|
|
)
|
|
|
|
return svc.Init(micro.Logger(log))
|
|
|
|
}),
|
2023-08-09 14:31:23 +03:00
|
|
|
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)))
|
|
|
|
}),
|
2023-08-07 21:30:30 +03:00
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-09 14:31:23 +03:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
2023-08-11 20:12:15 +03:00
|
|
|
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))
|
2023-08-09 14:31:23 +03:00
|
|
|
|
|
|
|
if err = svc.Server().Handle(svc.Server().NewHandler(mux)); err != nil {
|
2023-08-07 21:30:30 +03:00
|
|
|
logger.Fatalf(ctx, "failed to register handler: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc, nil
|
|
|
|
}
|