-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fuck beego project in progress: uber/fx is pretty cool + setting up e…
…cho from labstack
- Loading branch information
Showing
11 changed files
with
313 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/karngyan/maek/config" | ||
"github.com/karngyan/maek/libs/logger" | ||
"github.com/karngyan/maek/ui_api" | ||
"go.uber.org/fx" | ||
"go.uber.org/fx/fxevent" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func main() { | ||
fx.New( | ||
fx.Provide( | ||
config.New, | ||
logger.New, | ||
), | ||
fx.Decorate(func(l *zap.Logger) *zap.Logger { | ||
return l.With(zap.String("service", "ui_api")) | ||
}), | ||
fx.Invoke(ui_api.Run), | ||
fx.WithLogger(func(l *zap.Logger) fxevent.Logger { | ||
return &fxevent.ZapLogger{ | ||
Logger: l, | ||
} | ||
}), | ||
).Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package config | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/knadh/koanf/providers/env" | ||
|
||
"github.com/knadh/koanf/parsers/toml" | ||
"github.com/knadh/koanf/providers/file" | ||
"github.com/knadh/koanf/v2" | ||
) | ||
|
||
type Config struct { | ||
*koanf.Koanf | ||
} | ||
|
||
func New() (*Config, error) { | ||
k := koanf.New(".") | ||
|
||
configFile := os.Getenv("CONFIG_FILE") | ||
if configFile == "" { | ||
log.Fatal("CONFIG_FILE environment variable is not set") | ||
} | ||
|
||
if _, err := os.Stat(configFile); os.IsNotExist(err) { | ||
log.Fatalf("configuration file does not exist: %s", configFile) | ||
} | ||
|
||
if err := k.Load(file.Provider(configFile), toml.Parser()); err != nil { | ||
log.Fatalf("error loading configuration file (%s): %v", configFile, err) | ||
} | ||
|
||
// e.g. export MAEK_API_SERVER__PORT=8081 | ||
if err := k.Load(env.Provider("MAEK_", ".", func(s string) string { | ||
return strings.ToLower(strings.ReplaceAll(strings.TrimPrefix(s, "MAEK_"), "__", ".")) | ||
}), nil); err != nil { | ||
log.Fatalf("error loading environment variables: %v", err) | ||
} | ||
|
||
c := &Config{k} | ||
|
||
if c.IsDev() { | ||
k.Print() | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func (c *Config) IsDev() bool { | ||
return c.String("environment") == "development" | ||
} | ||
|
||
type ServiceMeta struct { | ||
Name string | ||
} | ||
|
||
func NewServiceMetaProvider(name string) func() *ServiceMeta { | ||
return func() *ServiceMeta { | ||
return &ServiceMeta{ | ||
Name: name, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
environment = "development" | ||
|
||
[api_server] | ||
port = 8080 | ||
cors_allowed_origins = ["http://localhost:3000"] | ||
|
||
[database] | ||
dsn = "postgres://maek:passwd@localhost:5432/maek_dev?sslmode=disable" | ||
dsn_test = "postgres://maek:passwd@localhost:5433/maek_test?sslmode=disable" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package logger | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"syscall" | ||
|
||
"go.uber.org/fx" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"github.com/karngyan/maek/config" | ||
) | ||
|
||
func New(lc fx.Lifecycle, c *config.Config) (*zap.Logger, error) { | ||
cfg := zap.NewProductionConfig() | ||
if c.IsDev() { | ||
cfg = zap.NewDevelopmentConfig() | ||
cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder | ||
} | ||
|
||
l, err := cfg.Build() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
lc.Append(fx.Hook{ | ||
OnStop: func(ctx context.Context) error { | ||
l.Info("syncing logger before shutdown") | ||
err := l.Sync() | ||
|
||
// ignore ENOTTY errors when logs are written to a console | ||
// https://github.com/uber-go/zap/issues/991#issuecomment-962098428 | ||
if err != nil && !errors.Is(err, syscall.ENOTTY) { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
}) | ||
|
||
return l, nil | ||
} |
Oops, something went wrong.