Browse Source

En cours de développement

master
mls-361 5 months ago
parent
commit
1f1d3d0ce9
  1. 60
      cmd/main.go
  2. 11
      internal/api/api.go
  3. 27
      internal/components/components.go
  4. 6
      internal/config/config.go
  5. 4
      internal/crypto/crypto.go
  6. 16
      internal/demo/demo.go
  7. 3
      internal/logger/logger.go
  8. 6
      internal/model/model.go
  9. 2
      internal/router/router.go
  10. 8
      internal/server/server.go
  11. 8
      internal/storage/storage.go

60
cmd/main.go

@ -34,46 +34,58 @@ var (
_builtAt string
)
func createComponents(app components.Application) (components.Logger, components.Server, error) {
config, err := config.Load(app)
func buildComponents(ccs *components.Components) error {
config, err := config.Load(ccs)
if err != nil {
return nil, nil, errors.WithMessage(err, "config") /////////////////////////////////////////////////////////////
return errors.WithMessage(err, "config") ///////////////////////////////////////////////////////////////////////
}
logger, err := logger.Build(app, config)
ccs.Config = config
logger, err := logger.Build(ccs)
if err != nil {
return nil, nil, errors.WithMessage(err, "logger") /////////////////////////////////////////////////////////////
return errors.WithMessage(err, "logger") ///////////////////////////////////////////////////////////////////////
}
crypto, err := crypto.Build(app)
ccs.Logger = logger
crypto, err := crypto.Build(ccs)
if err != nil {
return nil, nil, errors.WithMessage(err, "crypto") /////////////////////////////////////////////////////////////
return errors.WithMessage(err, "crypto") ///////////////////////////////////////////////////////////////////////
}
storage, err := storage.Build(app, config, logger, crypto)
ccs.Crypto = crypto
storage, err := storage.Build(ccs)
if err != nil {
return nil, nil, errors.WithMessage(err, "storage") ////////////////////////////////////////////////////////////
return errors.WithMessage(err, "storage") //////////////////////////////////////////////////////////////////////
}
model, err := model.Build(logger, storage)
ccs.Storage = storage
model, err := model.Build(ccs)
if err != nil {
return nil, nil, errors.WithMessage(err, "model") //////////////////////////////////////////////////////////////
return errors.WithMessage(err, "model") ////////////////////////////////////////////////////////////////////////
}
if err := demo.Build(app, logger, model); err != nil {
return nil, nil, errors.WithMessage(err, "demo") ///////////////////////////////////////////////////////////////
ccs.Model = model
if err := demo.Build(ccs); err != nil {
return errors.WithMessage(err, "demo") /////////////////////////////////////////////////////////////////////////
}
router := router.Build(logger)
ccs.Router = router.Build(ccs)
server, err := server.Build(config, logger, router)
server, err := server.Build(ccs)
if err != nil {
return nil, nil, errors.WithMessage(err, "server") /////////////////////////////////////////////////////////////
return errors.WithMessage(err, "server") ///////////////////////////////////////////////////////////////////////
}
api.Build(logger, router, model)
ccs.Server = server
api.Build(ccs)
return logger, server, nil
return nil
}
func waitEnd(server components.Server) (*sync.WaitGroup, chan struct{}) {
@ -110,12 +122,14 @@ func run() error {
return app.OnError(err)
}
logger, server, err := createComponents(app)
ccs := components.New(app)
err := buildComponents(ccs)
if err != nil {
return app.OnError(err)
}
logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ccs.Logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"APP",
"id", app.ID(),
"name", app.Name(),
@ -124,11 +138,11 @@ func run() error {
"pid", os.Getpid(),
)
group, endCh := waitEnd(server)
group, endCh := waitEnd(ccs.Server)
demo.Start()
err = server.Start()
err = ccs.Server.Start()
close(endCh)
@ -136,7 +150,7 @@ func run() error {
group.Wait()
logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ccs.Logger.Info( //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"END",
"uptime", time.Since(app.StartedAt()).Round(time.Second).String(),
)

11
internal/api/api.go

@ -27,10 +27,10 @@ type api struct {
model components.Model
}
func newAPI(logger components.Logger, model components.Model) *api {
func newAPI(ccs *components.Components) *api {
return &api{
logger: logger,
model: model,
logger: ccs.Logger,
model: ccs.Model,
}
}
@ -60,8 +60,9 @@ func (api *api) createJob(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
}
func Build(logger components.Logger, router components.Router, model components.Model) {
api := newAPI(logger, model)
func Build(ccs *components.Components) {
api := newAPI(ccs)
router := ccs.Router
router.Get("/debug", expvar.Handler())
router.Get("/status", http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {

27
internal/components/components.go

@ -53,15 +53,34 @@ type Router interface {
Post(path string, handler http.Handler)
}
// Server AFAIRE.
type Server interface {
Start() error
Stop()
}
// Storage AFAIRE.
type Storage interface {
jw.Storage
}
// Server AFAIRE.
type Server interface {
Start() error
Stop()
// Components AFAIRE.
type Components struct {
Application Application
Config Config
Crypto Crypto
Logger Logger
Model Model
Router Router
Server Server
Storage Storage
}
// New AFAIRE.
func New(app Application) *Components {
return &Components{
Application: app,
}
}
/*

6
internal/config/config.go

@ -25,16 +25,16 @@ func newConfig() *config {
}
// Load AFAIRE.
func Load(app components.Application) (components.Config, error) {
func Load(ccs *components.Components) (components.Config, error) {
cc := newConfig()
config, ok := app.LookupEnv("CONFIG")
config, ok := ccs.Application.LookupEnv("CONFIG")
if !ok {
cc.data.Default()
} else {
switch config {
case "json":
if err := json.LoadConfig(app, cc.data); err != nil {
if err := json.LoadConfig(ccs.Application, cc.data); err != nil {
return nil, err
}
default:

4
internal/crypto/crypto.go

@ -12,10 +12,10 @@ import (
"forge.chapril.org/armen/armen/internal/components"
)
func Build(app components.Application) (components.Crypto, error) {
func Build(ccs *components.Components) (components.Crypto, error) {
cc := crypto.New()
key, ok := app.LookupEnv("KEY")
key, ok := ccs.Application.LookupEnv("KEY")
if !ok {
return cc, nil
}

16
internal/demo/demo.go

@ -51,20 +51,20 @@ func (d *demo) succeeded(_ *runner.Job) {
// nothing to do
}
func Build(app components.Application, logger components.Logger, model components.Model) error {
if _, ok := app.LookupEnv("CONFIG"); ok {
func Build(ccs *components.Components) error {
if _, ok := ccs.Application.LookupEnv("CONFIG"); ok {
return nil
}
if v, ok := app.LookupEnv("DEMO"); !ok || v != "t" && v != "true" {
if v, ok := ccs.Application.LookupEnv("DEMO"); !ok || v != "t" && v != "true" {
return nil
}
d := &demo{
logger: logger,
logger: ccs.Logger,
}
r := runner.New("demo", model)
r := runner.New("demo", ccs.Model)
if err := r.AddJobHandler("panic", runner.JHF(d.panic)); err != nil {
return err
@ -79,11 +79,11 @@ func Build(app components.Application, logger components.Logger, model component
}
p, err := workers.NewPool(&workers.Config{
Logger: logger,
Model: model,
Logger: ccs.Logger,
Model: ccs.Model,
Runner: r,
PoolSize: 2,
Host: app.Host(),
Host: ccs.Application.Host(),
})
if err != nil {
return err

3
internal/logger/logger.go

@ -13,7 +13,8 @@ import (
)
// AFAIRE.
func Build(app components.Application, _ components.Config) (components.Logger, error) {
func Build(ccs *components.Components) (components.Logger, error) {
app := ccs.Application
return logger.New(app.ID(), app.Name(), "trace", logger.NewStderrOutput())
}

6
internal/model/model.go

@ -13,10 +13,10 @@ import (
)
// Build AFAIRE.
func Build(logger components.Logger, storage components.Storage) (components.Model, error) {
func Build(ccs *components.Components) (components.Model, error) {
config := &model.Config{
Logger: logger,
Storage: storage,
Logger: ccs.Logger,
Storage: ccs.Storage,
}
return model.New(config)

2
internal/router/router.go

@ -25,7 +25,7 @@ func newRouter() *router {
}
// Build AFAIRE.
func Build(logger components.Logger) components.Router {
func Build(_ *components.Components) components.Router {
cr := newRouter()
cr.mux.PanicHandler = func(rw http.ResponseWriter, _ *http.Request, _ interface{}) {

8
internal/server/server.go

@ -30,11 +30,11 @@ func newServer(logger components.Logger) *server {
}
}
func Build(config components.Config, logger components.Logger, router components.Router) (components.Server, error) {
cs := newServer(logger.NewLogger(uuid.New(), "server"))
cfg := config.Server()
func Build(ccs *components.Components) (components.Server, error) {
cs := newServer(ccs.Logger.NewLogger(uuid.New(), "server"))
cfg := ccs.Config.Server()
handler := middleware.Trace(router.Handler(), logger)
handler := middleware.Trace(ccs.Router.Handler(), ccs.Logger)
cs.server = &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Port),

8
internal/storage/storage.go

@ -14,12 +14,8 @@ import (
)
// Build AFAIRE.
func Build(app components.Application,
config components.Config,
logger components.Logger,
crypto components.Crypto) (components.Storage, error) {
cfg := config.Storage()
func Build(ccs *components.Components) (components.Storage, error) {
cfg := ccs.Config.Storage()
switch cfg.Type {
case "memory":

Loading…
Cancel
Save