Browse Source

En cours de développement

master
losyme 3 months ago
parent
commit
5c8fadd06e
  1. 52
      internal/api/api.go
  2. 41
      internal/api/config.go
  3. 4
      internal/api/jobs.go
  4. 41
      internal/application/api.go
  5. 7
      internal/application/application.go
  6. 10
      internal/application/model.go
  7. 2
      internal/components/components.go
  8. 12
      internal/model/jobs.go
  9. 14
      internal/model/model.go
  10. 4
      internal/model/validate.go
  11. 2
      internal/model/workflows.go

52
internal/api/api.go

@ -13,60 +13,46 @@ import (
"forge.chapril.org/losyme/kong/middleware"
"forge.chapril.org/losyme/kong/middleware/json"
"forge.chapril.org/losyme/kong/router"
"forge.chapril.org/losyme/logger"
"forge.chapril.org/losyme/uuid"
"forge.chapril.org/dune/dune/internal/components"
)
type api struct {
cs *components.Components
logger *logger.Component
type API struct {
*Config
}
func newAPI(cs *components.Components) (*api, error) {
logger, err := cs.Logger.NewComponent(uuid.New(), "api")
if err != nil {
func New(cfg *Config) (*API, error) {
if err := cfg.validate(); err != nil {
return nil, err
}
api := &api{
cs: cs,
logger: logger,
}
api := &API{cfg}
return api, nil
}
api.setup()
func (api *api) status(c *context.Context) error {
return c.JSON(http.StatusNoContent, nil)
return api, nil
}
func Setup(cs *components.Components) error {
api, err := newAPI(cs)
if err != nil {
return err
}
cs.Router.MethodNotAllowed = json.MethodNotAllowed(api.logger)
cs.Router.NotFound = json.NotFound(api.logger)
cs.Router.OnRecover = json.OnRecover(api.logger)
func (api *API) setup() {
api.Router.MethodNotAllowed = json.MethodNotAllowed(api.Logger)
api.Router.NotFound = json.NotFound(api.Logger)
api.Router.OnRecover = json.OnRecover(api.Logger)
cs.Router.Use(
api.Router.Use(
middleware.RequestID(),
middleware.Logger(api.logger),
json.OnError(api.logger),
middleware.Logger(api.Logger),
json.OnError(api.Logger),
)
cs.Router.Group("/api", func(g *router.Group) { //api...............................................................
api.Router.Group("/api", func(g *router.Group) { // api.............................................................
g.Get("/status", api.status)
g.New("/jobs", func(g2 *router.Group) { //api/jobs..............................................................
g.New("/jobs", func(g2 *router.Group) { // api/jobs..............................................................
g2.Post("", api.createJob)
})
})
}
return nil
func (api *API) status(c *context.Context) error {
return c.JSON(http.StatusNoContent, nil)
}
/*

41
internal/api/config.go

@ -0,0 +1,41 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### dune ####### Copyright (c) 2021 losyme ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package api
import (
"forge.chapril.org/losyme/errors"
"forge.chapril.org/losyme/kong/router"
"forge.chapril.org/losyme/logger"
"forge.chapril.org/dune/dune/internal/model"
)
type Config struct {
Logger *logger.Component
Model *model.Model
Router *router.Router
}
func (cfg *Config) validate() error {
if cfg.Logger == nil {
return errors.New("[api] logger cannot be nil") ////////////////////////////////////////////////////////////////
}
if cfg.Model == nil {
return errors.New("[api] model cannot be nil") /////////////////////////////////////////////////////////////////
}
if cfg.Router == nil {
return errors.New("[api] router cannot be nil") ////////////////////////////////////////////////////////////////
}
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

4
internal/api/jobs.go

@ -18,7 +18,7 @@ import (
"forge.chapril.org/dune/dune/internal/model"
)
func (api *api) createJob(c *context.Context) error {
func (api *API) createJob(c *context.Context) error {
job := new(jw.Job)
if err := decode.JSON(c, 1024*32, job); err != nil {
@ -29,7 +29,7 @@ func (api *api) createJob(c *context.Context) error {
job.ID = c.RequestID()
}
job, err := api.cs.Model.CreateJob(job)
job, err := api.Model.CreateJob(job)
if err != nil {
status := http.StatusInternalServerError

41
internal/application/api.go

@ -0,0 +1,41 @@
/*
------------------------------------------------------------------------------------------------------------------------
####### dune ####### Copyright (c) 2021 losyme ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package application
import (
"forge.chapril.org/losyme/uuid"
"forge.chapril.org/dune/dune/internal/api"
)
func (app *Application) API() error {
cs := app.components
logger, err := cs.Logger.NewComponent(uuid.New(), "api")
if err != nil {
return err
}
c, err := api.New(
&api.Config{
Logger: logger,
Model: cs.Model,
Router: cs.Router,
},
)
if err != nil {
return err
}
app.components.API = c
return nil
}
/*
######################################################################################################## @(°_°)@ #######
*/

7
internal/application/application.go

@ -10,7 +10,6 @@ import (
"forge.chapril.org/losyme/application"
"forge.chapril.org/losyme/minikit"
"forge.chapril.org/dune/dune/internal/api"
"forge.chapril.org/dune/dune/internal/components"
)
@ -55,7 +54,7 @@ func New(name, version, builtAt string, args []string) *Application {
func (app *Application) Run() error {
return app.minikit.RunServer(
func() error { //build..........................................................................................
func() error { // build..........................................................................................
if fn := app.cmdLine.runAfter; fn != nil {
return fn()
}
@ -68,7 +67,7 @@ func (app *Application) Run() error {
return err
}
if err := api.Setup(app.components); err != nil {
if err := app.API(); err != nil {
return err
}
@ -76,7 +75,7 @@ func (app *Application) Run() error {
return nil
},
func() { //atEnd................................................................................................
func() { // atEnd................................................................................................
<-app.components.Scheduler.Stop().Done()
},
)

10
internal/application/model.go

@ -20,10 +20,12 @@ func (app *Application) model() error {
return err
}
c, err := model.New(&model.Config{
Logger: logger,
Storage: cs.Storage,
})
c, err := model.New(
&model.Config{
Logger: logger,
Storage: cs.Storage,
},
)
if err != nil {
return err
}

2
internal/components/components.go

@ -10,11 +10,13 @@ import (
"forge.chapril.org/dune/jw"
"forge.chapril.org/losyme/minikit/pkg/components"
"forge.chapril.org/dune/dune/internal/api"
"forge.chapril.org/dune/dune/internal/model"
)
type Components struct {
*components.Components
API *api.API
Model *model.Model
Storage jw.Storage
}

12
internal/model/jobs.go

@ -14,9 +14,9 @@ import (
)
func (m *Model) insertJob(job *jw.Job) (bool, error) {
done, err := m.storage.InsertJob(job)
done, err := m.Storage.InsertJob(job)
if err != nil {
m.logger.Error( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
m.Logger.Error( //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
"Unable to insert a new job",
append(
job.Fields(),
@ -28,11 +28,11 @@ func (m *Model) insertJob(job *jw.Job) (bool, error) {
}
if !done {
m.logger.Notice("A job with the same category already exists", job.Fields()...) //::::::::::::::::::::::::::::::
m.Logger.Notice("A job with the same category already exists", job.Fields()...) //::::::::::::::::::::::::::::::
return false, nil
}
m.logger.Info("New job", job.Fields()...) //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
m.Logger.Info("New job", job.Fields()...) //::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
return true, nil
}
@ -74,11 +74,11 @@ func (m *Model) NotifyJob(id string, data interface{}) error {
}
func (m *Model) SetJobPriority(id string, priority jw.Priority) error {
return m.storage.SetJobPriority(id, priority)
return m.Storage.SetJobPriority(id, priority)
}
func (m *Model) SetJobRunAfter(id string, duration time.Duration) error {
return m.storage.SetJobRunAfter(id, duration)
return m.Storage.SetJobRunAfter(id, duration)
}
/*

14
internal/model/model.go

@ -6,11 +6,7 @@
package model
import (
"forge.chapril.org/dune/jw"
"forge.chapril.org/losyme/errors"
"forge.chapril.org/losyme/logger"
)
import "forge.chapril.org/losyme/errors"
const (
ErrInternal = errors.Sentinel("internal error")
@ -18,8 +14,7 @@ const (
)
type Model struct {
logger *logger.Component
storage jw.Storage
*Config
}
func New(cfg *Config) (*Model, error) {
@ -27,10 +22,7 @@ func New(cfg *Config) (*Model, error) {
return nil, err
}
m := &Model{
logger: cfg.Logger,
storage: cfg.Storage,
}
m := &Model{cfg}
return m, nil
}

4
internal/model/validate.go

@ -36,7 +36,7 @@ func validatePriority(p *jw.Priority) {
}
func (m *Model) validateJob(job *jw.Job) error {
if err := m.storage.ValidateJob(job); err != nil {
if err := m.Storage.ValidateJob(job); err != nil {
return err
}
@ -156,7 +156,7 @@ func validateStep(wf *jw.Workflow, step *jw.Step) error {
}
func (m *Model) validateWorkflow(wf *jw.Workflow) error {
if err := m.storage.ValidateWorkflow(wf); err != nil {
if err := m.Storage.ValidateWorkflow(wf); err != nil {
return err
}

2
internal/model/workflows.go

@ -23,7 +23,7 @@ func (m *Model) CreateWorkflow(wf *jw.Workflow) (*jw.Workflow, error) {
}
func (m *Model) SetWorkflowPriority(id string, priority jw.Priority) error {
return m.storage.SetWorkflowPriority(id, priority)
return m.Storage.SetWorkflowPriority(id, priority)
}
/*

Loading…
Cancel
Save