Jobs & Workflows https://armen.surge.sh
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
armen/internal/api/api.go

181 lines
3.9 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### api ####### Copyright (c) 2021 mls-361 ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package api
import (
"net/http"
"forge.chapril.org/armen/jw"
"forge.chapril.org/armen/model"
"forge.chapril.org/mls-361/errors"
"forge.chapril.org/mls-361/jsonapi"
"forge.chapril.org/mls-361/logger"
"forge.chapril.org/mls-361/uuid"
"forge.chapril.org/armen/armen/internal/components"
)
const _maxBodySize = 1024 * 32
type api struct {
crypto components.Crypto
logger components.Logger
model components.Model
}
func newAPI(ccs *components.Components) *api {
return &api{
crypto: ccs.Crypto,
logger: ccs.Logger.NewLogger(uuid.New(), "api"),
model: ccs.Model,
}
}
func (api *api) debug(hf jsonapi.HandlerFunc) jsonapi.Handler {
return mwDebug(
mwAuth(hf, api.crypto),
api.logger,
)
}
func (api *api) trace(hf jsonapi.HandlerFunc) jsonapi.Handler {
return mwTrace(
mwAuth(hf, api.crypto),
api.logger,
)
}
func (api *api) createJob(r *jsonapi.Request) {
job := new(jw.Job)
if r.Decode(_maxBodySize, job) != nil {
return
}
job, err := api.model.CreateJob(job)
if err != nil {
if errors.Is(err, model.ErrValidation) {
r.BadRequest(err)
} else {
r.InternalServerError(err)
}
return
}
if job == nil {
r.RenderNoContent()
return
}
r.RenderJSON(http.StatusCreated, job, api.logger)
}
func (api *api) nextJob(r *jsonapi.Request) {
namespace := r.Param("namespace")
if namespace == "" {
r.BadRequest(errors.New("namespace must not be empty")) ////////////////////////////////////////////////////////
return
}
job, err := api.model.NextJob(namespace)
if err != nil {
r.InternalServerError(err)
return
}
if job == nil {
r.RenderNoContent()
return
}
r.RenderJSON(http.StatusOK, job, api.logger)
}
func (api *api) updateJob(r *jsonapi.Request) {
job := new(jw.Job)
if r.Decode(_maxBodySize, job) != nil {
return
}
job, err := api.model.UpdateJob(job)
if err != nil {
r.InternalServerError(err)
return
}
if job == nil {
r.RenderAccepted()
return
}
r.RenderJSON(http.StatusOK, job, api.logger)
}
func (api *api) createWorkflow(r *jsonapi.Request) {
wf := new(jw.Workflow)
if r.Decode(_maxBodySize, wf) != nil {
return
}
wf, err := api.model.CreateWorkflow(wf)
if err != nil {
if errors.Is(err, model.ErrValidation) {
r.BadRequest(err)
} else {
r.InternalServerError(err)
}
return
}
r.RenderJSON(http.StatusCreated, wf, api.logger)
}
func (api *api) adminListLogger(r *jsonapi.Request) {
r.RenderJSON(http.StatusOK, logger.All(), api.logger)
}
func (api *api) adminSetLoggerLevel(r *jsonapi.Request) {
loggerID := r.Param("logger")
if loggerID == "" {
r.BadRequest(errors.New("logger identifier must not be empty")) ////////////////////////////////////////////////
return
}
level := r.Param("level")
if level == "" {
r.BadRequest(errors.New("level must not be empty")) ////////////////////////////////////////////////////////////
return
}
if err := logger.SetLevel(loggerID, level); err != nil {
r.BadRequest(err)
return
}
r.RenderOK()
}
func Build(ccs *components.Components) {
api := newAPI(ccs)
router := ccs.Router
router.POST("/api/job/create", api.debug(api.createJob))
router.GET("/api/job/next/:namespace", api.trace(api.nextJob))
router.PUT("/api/job/update", api.debug(api.updateJob))
router.POST("/api/workflow/create", api.debug(api.createWorkflow))
router.GET("/api/admin/logger/list", api.debug(api.adminListLogger))
router.PUT("/api/admin/logger/:logger/setlevel/:level", api.debug(api.adminSetLoggerLevel))
}
/*
######################################################################################################## @(°_°)@ #######
*/