pkgdash/service/service.go

134 lines
3.1 KiB
Go
Raw Normal View History

2023-08-07 21:30:30 +03:00
package service
import (
"context"
2023-08-13 00:09:57 +03:00
"database/sql"
httpsrv "go.unistack.org/micro-server-http/v4"
"go.unistack.org/micro/v4"
"go.unistack.org/micro/v4/config"
2023-08-12 19:26:50 +03:00
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"
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-12 19:26:50 +03:00
pb "go.unistack.org/unistack-org/pkgdash/proto"
"go.unistack.org/unistack-org/pkgdash/service/client_git"
"go.unistack.org/unistack-org/pkgdash/storage"
2023-08-13 00:09:57 +03:00
"net/url"
"strings"
2023-08-07 21:30:30 +03:00
)
func NewService(ctx context.Context) (micro.Service, error) {
var reg register.Register
cfg := intcfg.NewConfig()
2023-08-12 19:26:50 +03:00
cs := microcfg.NewConfig(config.Struct(cfg))
// TODO
mgsrv := httpsrv.NewServer(
options.Register(reg),
2023-08-07 21:30:30 +03:00
)
svc := micro.NewService(
2023-08-12 19:26:50 +03:00
micro.Config(cs),
2023-08-07 21:30:30 +03:00
)
2023-08-12 19:26:50 +03:00
h := handler.NewHandler(svc, client_git.NewClient(5))
2023-08-07 21:30:30 +03:00
2023-08-12 19:26:50 +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-12 19:26:50 +03:00
if err := config.Load(ctx, []config.Config{cs}, config.LoadOverride(true)); err != nil {
2023-08-07 21:30:30 +03:00
return err
}
2023-08-12 19:26:50 +03:00
if err := config.Validate(ctx, cfg); err != nil {
2023-08-07 21:30:30 +03:00
return err
}
2023-08-12 19:26:50 +03:00
if err := svc.Init(
micro.Name(intcfg.ServiceName),
micro.Version(intcfg.ServiceVersion),
2023-08-07 21:30:30 +03:00
); err != nil {
return err
}
if err := svc.Server("http").Init(
2023-08-12 19:26:50 +03:00
options.Address(cfg.Address),
options.Name(cfg.App.Name),
server.Version(cfg.App.Version),
2023-08-07 21:30:30 +03:00
); err != nil {
return err
}
return nil
}),
micro.BeforeStart(func(_ context.Context) error {
log := logger.NewLogger(
2023-08-12 19:26:50 +03:00
logger.WithLevel(logger.ParseLevel(cfg.LogLevel)),
2023-08-07 21:30:30 +03:00
logger.WithCallerSkipCount(3),
)
return svc.Init(micro.Logger(log))
}),
2023-08-13 00:09:57 +03:00
micro.BeforeStart(func(ctx context.Context) error {
var connstr string
2023-08-12 19:26:50 +03:00
if v, ok := cfg.StorageDSN[cfg.App.Name]; ok {
connstr = v
2023-08-12 19:26:50 +03:00
} else if v, ok = cfg.StorageDSN["all"]; ok {
connstr = v
}
2023-08-13 00:09:57 +03:00
scheme, dsn, err := storageOptions(connstr)
if err != nil {
return err
}
2023-08-13 00:09:57 +03:00
conn, err := connectDataBase(scheme, dsn)
if err != nil {
return err
}
2023-08-13 00:09:57 +03:00
store, err := storage.NewStorage(scheme, conn)
if err != nil {
return err
}
2023-08-13 00:09:57 +03:00
ctx = storage.InContext(ctx, store)
return svc.Init(micro.Context(ctx))
}),
2023-08-07 21:30:30 +03:00
); err != nil {
return nil, err
}
2023-08-12 19:26:50 +03:00
if err := pb.RegisterPkgdashServiceServer(mgsrv, h); err != nil {
2023-08-07 21:30:30 +03:00
logger.Fatalf(ctx, "failed to register handler: %v", err)
}
return svc, nil
}
2023-08-13 00:09:57 +03:00
func storageOptions(dsn string) (string, string, error) {
u, err := url.Parse(dsn)
if err != nil {
return "", "", err
}
scheme := u.Scheme
if idx := strings.Index(u.Scheme, "+"); idx > 0 {
scheme = u.Scheme[:idx]
u.Scheme = u.Scheme[idx+1:]
}
return scheme, u.String(), nil
}
func connectDataBase(driverName, dsn string) (*sql.DB, error) {
conn, err := sql.Open(driverName, dsn)
if err != nil {
return nil, err
}
if err = conn.Ping(); err != nil {
return nil, err
}
return conn, err
}