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.
 
 
 

76 lines
1.7 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### api ####### Copyright (c) 2021 mls-361 ##################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package api
import (
"errors"
"expvar"
"net/http"
"forge.chapril.org/armen/jw"
"forge.chapril.org/armen/model"
"forge.chapril.org/mls-361/jsonapi"
"forge.chapril.org/armen/armen/internal/components"
)
const (
_maxBodySize = 1024 * 32
)
type api struct {
logger components.Logger
model components.Model
}
func newAPI(logger components.Logger, model components.Model) *api {
return &api{
logger: logger,
model: model,
}
}
func (api *api) createJob(rw http.ResponseWriter, r *http.Request) {
job := new(jw.Job)
if jsonapi.Decode(rw, r, _maxBodySize, job) != nil {
return
}
done, err := api.model.CreateJob(job)
if err != nil {
if errors.Is(err, model.ErrValidation) {
jsonapi.BadRequest(rw, err)
} else {
jsonapi.InternalServerError(rw, err)
}
return
}
if done {
jsonapi.Render(rw, r, job, api.logger)
return
}
rw.WriteHeader(http.StatusOK)
}
func Build(logger components.Logger, router components.Router, model components.Model) {
api := newAPI(logger, model)
router.Get("/debug", expvar.Handler())
router.Get("/status", http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(http.StatusOK)
}))
router.Post("/job/create", http.HandlerFunc(api.createJob))
}
/*
######################################################################################################## @(°_°)@ #######
*/