@@ -2,34 +2,181 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
appconfig "git.unistack.org/unistack-org/pkgdash/internal/config"
|
||||
"git.unistack.org/unistack-org/pkgdash/internal/handler"
|
||||
"git.unistack.org/unistack-org/pkgdash/internal/service/client_git"
|
||||
pb "git.unistack.org/unistack-org/pkgdash/proto"
|
||||
jsoncodec "go.unistack.org/micro-codec-json/v4"
|
||||
yamlcodec "go.unistack.org/micro-codec-yaml/v4"
|
||||
envconfig "go.unistack.org/micro-config-env/v4"
|
||||
fileconfig "go.unistack.org/micro-config-file/v4"
|
||||
vaultconfig "go.unistack.org/micro-config-vault/v4"
|
||||
zlogger "go.unistack.org/micro-logger-zerolog/v4"
|
||||
httpsrv "go.unistack.org/micro-server-http/v4"
|
||||
healthhandler "go.unistack.org/micro-server-http/v4/handler/health"
|
||||
meterhandler "go.unistack.org/micro-server-http/v4/handler/meter"
|
||||
"go.unistack.org/micro/v4"
|
||||
"go.unistack.org/micro/v4/config"
|
||||
"go.unistack.org/micro/v4/logger"
|
||||
"go.unistack.org/unistack-org/pkgdash/service"
|
||||
"go.unistack.org/micro/v4/options"
|
||||
"go.unistack.org/micro/v4/server"
|
||||
rutil "go.unistack.org/micro/v4/util/reflect"
|
||||
)
|
||||
|
||||
const appName = "pkgdash"
|
||||
|
||||
var (
|
||||
BuildDate string = "now" // filled when build
|
||||
AppVersion string = "latest" // filled when build
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
|
||||
logger.DefaultLogger = zlogger.NewLogger(zlogger.ReportCaller(), logger.WithLevel(logger.DebugLevel), logger.WithCallerSkipCount(3))
|
||||
if err := logger.DefaultLogger.Init(); err != nil {
|
||||
logger.Fatalf(ctx, "failed to init logger")
|
||||
}
|
||||
|
||||
go func() {
|
||||
sig := <-ch
|
||||
logger.Infof(ctx, "handle signal %v, exiting", sig)
|
||||
cancel()
|
||||
cfg := appconfig.NewConfig(appName, AppVersion) // create new empty config
|
||||
vc := vaultconfig.NewConfig(
|
||||
config.AllowFail(true), // that may be not exists
|
||||
config.Struct(cfg), // load from vault
|
||||
options.Codec(jsoncodec.NewCodec()), // vault config in json
|
||||
config.BeforeLoad(func(ctx context.Context, c config.Config) error {
|
||||
return c.Init(
|
||||
vaultconfig.HTTPClient(&http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
}),
|
||||
vaultconfig.Address(cfg.Vault.Addr),
|
||||
vaultconfig.Timeout(5*time.Second),
|
||||
vaultconfig.Token(cfg.Vault.Token),
|
||||
vaultconfig.Path(cfg.Vault.Path),
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
||||
if err := config.Load(ctx,
|
||||
[]config.Config{
|
||||
config.NewConfig( // load from defaults
|
||||
config.Struct(cfg), // pass config struct
|
||||
),
|
||||
fileconfig.NewConfig( // load from file
|
||||
config.AllowFail(true), // that may be not exists
|
||||
config.Struct(cfg), // pass config struct
|
||||
options.Codec(yamlcodec.NewCodec()), // file config in json
|
||||
fileconfig.Path("./local.yaml"), // nearby file
|
||||
),
|
||||
envconfig.NewConfig( // load from environment
|
||||
config.Struct(cfg), // pass config struct
|
||||
),
|
||||
vc,
|
||||
}, config.LoadOverride(true),
|
||||
); err != nil {
|
||||
logger.Fatalf(ctx, "failed to load config: %v", err)
|
||||
}
|
||||
|
||||
if err := config.Validate(ctx, cfg); err != nil {
|
||||
logger.Fatalf(ctx, "failed to validate config: %v", err)
|
||||
}
|
||||
|
||||
svc := micro.NewService()
|
||||
|
||||
if err := svc.Init(
|
||||
micro.Name(cfg.Server.Name),
|
||||
micro.Version(cfg.Server.Version),
|
||||
); err != nil {
|
||||
logger.Fatalf(ctx, "failed to init service: %v", err)
|
||||
}
|
||||
|
||||
if err := svc.Server("http").Init(
|
||||
options.Address(cfg.Server.Addr),
|
||||
options.Name(cfg.Server.Name),
|
||||
server.Version(cfg.Server.Version),
|
||||
); err != nil {
|
||||
logger.Fatalf(ctx, "failed to init service: %v", err)
|
||||
}
|
||||
|
||||
h := handler.NewHandler(svc, client_git.NewClient(5))
|
||||
if err := h.Init(svc.Options().Context); err != nil {
|
||||
logger.Fatalf(ctx, "failed to init handler: %v", err)
|
||||
}
|
||||
|
||||
log := logger.NewLogger(
|
||||
logger.WithLevel(logger.ParseLevel(cfg.Server.LoggerLevel)),
|
||||
logger.WithCallerSkipCount(3),
|
||||
)
|
||||
if err := svc.Init(micro.Logger(log)); err != nil {
|
||||
logger.Fatalf(ctx, "failed to init service: %v", err)
|
||||
}
|
||||
|
||||
if err := pb.RegisterPkgdashServiceServer(svc.Server("http"), h); err != nil {
|
||||
logger.Fatalf(ctx, "failed to register handler: %v", err)
|
||||
}
|
||||
|
||||
intsvc := httpsrv.NewServer(
|
||||
options.Codecs("application/json", jsoncodec.NewCodec()),
|
||||
options.Address(cfg.Meter.Addr),
|
||||
options.Context(ctx),
|
||||
)
|
||||
|
||||
if err := intsvc.Init(); err != nil {
|
||||
logger.Fatalf(ctx, "failed to init http srv: %v", err)
|
||||
}
|
||||
|
||||
if err := healthhandler.RegisterHealthServiceServer(intsvc, healthhandler.NewHandler()); err != nil {
|
||||
logger.Fatalf(ctx, "failed to set http handler: %v", err)
|
||||
}
|
||||
|
||||
if err := meterhandler.RegisterMeterServiceServer(intsvc, meterhandler.NewHandler()); err != nil {
|
||||
logger.Fatalf(ctx, "failed to set http handler: %v", err)
|
||||
}
|
||||
|
||||
if err := intsvc.Start(); err != nil {
|
||||
logger.Fatalf(ctx, "failed to run http srv: %v", err)
|
||||
}
|
||||
|
||||
cw, err := vc.Watch(ctx, config.WatchCoalesce(true), config.WatchInterval(1*time.Second, 5*time.Second))
|
||||
if err != nil {
|
||||
logger.Fatalf(ctx, "failed to watch config: %v", err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := cw.Stop(); err != nil {
|
||||
logger.Error(ctx, err)
|
||||
}
|
||||
}()
|
||||
|
||||
svc, err := service.NewService(ctx)
|
||||
if err != nil {
|
||||
logger.Fatalf(ctx, "failed to create service: %v", err)
|
||||
}
|
||||
|
||||
// start server
|
||||
if err = svc.Run(); err != nil {
|
||||
logger.Fatal(ctx, err)
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
changes, err := cw.Next()
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "failed to get config update: %v", err)
|
||||
}
|
||||
for k, v := range changes {
|
||||
if err = rutil.SetFieldByPath(cfg, v, k); err != nil {
|
||||
logger.Errorf(ctx, "failed to set config update: %v", err)
|
||||
break
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
for k := range changes {
|
||||
switch k {
|
||||
case "Server.LoggerLevel":
|
||||
if lvl, ok := changes[k].(string); ok {
|
||||
logger.Infof(ctx, "logger level changed to %s", lvl)
|
||||
logger.DefaultLogger.Level(logger.ParseLevel(lvl))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go.unistack.org/unistack-org/pkgdash/internal"
|
||||
"git.unistack.org/unistack-org/pkgdash/internal"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
|
Reference in New Issue
Block a user