feat: Initial

This commit is contained in:
Artёm Mishin committed 2025-04-25 11:20:31 +03:00
commit f0abca60db
26 files changed
+1112

No files matched your search

+89
View File
@@ -0,0 +1,89 @@
package app
import (
"go.mrlokop.dev/universe/core/config"
"go.mrlokop.dev/universe/core/errors"
"go.mrlokop.dev/universe/core/modules"
"go.uber.org/fx"
"go.uber.org/fx/fxevent"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"sync/atomic"
)
var (
ErrAlreadyStarted = errors.Type("ErrAlreadyStarted", "already started")
ErrUnknownModule = errors.Type("ErrUnknownModule", "specified module does not exist")
)
type Application interface {
Config() *config.UniverseConfig
Run() error
}
type UniverseApplication struct {
fx *fx.App
config *config.UniverseConfig
options []fx.Option
started atomic.Bool
}
func (app *UniverseApplication) Config() *config.UniverseConfig {
return app.config
}
func (app *UniverseApplication) Run() error {
if !app.started.CompareAndSwap(false, true) {
return ErrAlreadyStarted.CreateEmpty()
}
options := make([]fx.Option, 0)
options = append(options, app.options...)
options = append(options,
fx.Provide(func() Application {
return app
}),
fx.WithLogger(func(log *zap.Logger) fxevent.Logger {
logger := &fxevent.ZapLogger{Logger: log.WithOptions(zap.AddCallerSkip(2))}
logger.UseLogLevel(zapcore.DebugLevel)
return logger
}),
)
if app.config.Modules != nil && len(app.config.Modules) > 0 {
for id, modConf := range app.config.Modules {
kind := modConf["kind"].(string)
module := modules.GetModule(kind)
if module == nil {
return ErrUnknownModule.Builder().
Messagef("module '%s' is not registered", kind).
WithField("module", kind).
Build()
}
option, err := module.Create(id, modConf)
if err != nil {
return err
}
options = append(options, option)
}
}
app.fx = fx.New(options...)
app.fx.Run()
return nil
}
func (app *UniverseApplication) WithOption(options ...fx.Option) *UniverseApplication {
if app.started.Load() {
panic(ErrAlreadyStarted)
}
app.options = append(app.options, options...)
return app
}
func NewUniverseApplication(config *config.UniverseConfig) *UniverseApplication {
return &UniverseApplication{config: config}
}
+53
View File
@@ -0,0 +1,53 @@
package app
import (
"go.mrlokop.dev/universe/core/config"
"go.mrlokop.dev/universe/core/fs"
"go.mrlokop.dev/universe/core/modules"
"go.uber.org/zap"
"os"
)
var (
Module = modules.SimpleModule("universe:core", func(context modules.ModuleFactoryContext) error {
context.Invoke(func(log *zap.Logger, info *Info, env AppEnvType) {
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown"
}
log.Sugar().Infof("Starting %s on %s, pid=%v, env=%s", info.ServiceName, hostname, os.Getpid(), env)
})
context.Provide(func(env AppEnvType) RunPath {
if env == AppEnvTypeDevelopment {
return fs.System.INodeAt("run")
}
runEnv := os.Getenv("UNIVERSE_RUN_PATH")
if runEnv == "" {
return fs.System.INodeAt(".")
}
return fs.System.INodeAt(runEnv)
})
context.Provide(func(path RunPath) StoragePath {
return path.Join("storage")
})
context.Provide(provideEnv)
context.Provide(provideAppInfo)
context.Provide(func(app Application) *config.UniverseConfig {
return app.Config()
})
context.Provide(func(env AppEnvType) *zap.Logger {
if env == AppEnvTypeDevelopment {
config := zap.NewDevelopmentConfig()
config.OutputPaths = []string{"stdout"}
return zap.Must(config.Build(
zap.WithCaller(true),
))
}
return zap.Must(zap.NewProduction())
})
return nil
})
)
+56
View File
@@ -0,0 +1,56 @@
package app
import (
"os"
)
type AppEnvType int
type AppEnvName string
const (
AppEnvTypeDevelopment AppEnvType = iota
AppEnvTypeStaging
AppEnvTypeProduction
)
func (app AppEnvType) String() string {
switch app {
case AppEnvTypeDevelopment:
return "development"
case AppEnvTypeStaging:
return "staging"
case AppEnvTypeProduction:
return "production"
default:
return "unknown"
}
}
var (
knownEnvTypeEnvs = []string{
"UNIVERSE_ENV",
"APP_ENV",
}
)
func provideEnv() AppEnvType {
for _, envType := range knownEnvTypeEnvs {
value := os.Getenv(envType)
if value == "" {
continue
}
switch envType {
case "development", "dev", "devel":
return AppEnvTypeDevelopment
case "staging", "test":
return AppEnvTypeStaging
case "production", "prod":
return AppEnvTypeProduction
default:
return AppEnvTypeDevelopment
}
}
return AppEnvTypeDevelopment
}
+44
View File
@@ -0,0 +1,44 @@
package app
import (
"go.mrlokop.dev/universe/core/config"
"reflect"
)
type Info struct {
ServiceName string
Version string
BuildInfo *BuildInfo
}
type BuildInfo struct {
GitCommit string
BuildDate string
}
func provideAppInfo(
app Application,
cfg *config.UniverseConfig,
) *Info {
serviceInfo := cfg.ServiceInfo
if serviceInfo == nil {
serviceInfo = &config.ServiceInfo{}
// extract service name from app
serviceInfo.Name = extractServiceName(app)
}
return &Info{
ServiceName: serviceInfo.Name,
BuildInfo: &BuildInfo{
GitCommit: "",
BuildDate: "",
},
}
}
func extractServiceName(app Application) string {
appType := reflect.TypeOf(app)
if appType.Kind() == reflect.Ptr {
return appType.Elem().Name()
}
return appType.Name()
}
+6
View File
@@ -0,0 +1,6 @@
package app
import "go.mrlokop.dev/universe/core/fs"
type StoragePath fs.INode
type RunPath fs.INode
+11
View File
@@ -0,0 +1,11 @@
package app
import "fmt"
func Group(name string) string {
return fmt.Sprintf(`group:"%s"`, name)
}
func Name(name string) string {
return fmt.Sprintf(`name:"%s"`, name)
}