mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 11:55:40 +01:00
Add some documentation (controller)
This commit is contained in:
parent
b872f28acd
commit
e8cbab2c29
2 changed files with 22 additions and 0 deletions
|
|
@ -129,6 +129,8 @@ func (c *controller) tlpParam(r *http.Request) (tlp, error) {
|
||||||
return "", fmt.Errorf("unsupported TLP type '%s'", t)
|
return "", fmt.Errorf("unsupported TLP type '%s'", t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// creates calls the "ensureFolders" functions to create the directories and files.
|
||||||
|
// It returns a struct by success, otherwise an error.
|
||||||
func (c *controller) create(*http.Request) (interface{}, error) {
|
func (c *controller) create(*http.Request) (interface{}, error) {
|
||||||
if err := ensureFolders(c.cfg); err != nil {
|
if err := ensureFolders(c.cfg); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -37,11 +37,14 @@ func asMultiError(err error) multiError {
|
||||||
return multiError([]string{err.Error()})
|
return multiError([]string{err.Error()})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// controller contains the config values and the html templates.
|
||||||
type controller struct {
|
type controller struct {
|
||||||
cfg *config
|
cfg *config
|
||||||
tmpl *template.Template
|
tmpl *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// newController assigns the given configs to a controller variable and parses the html template
|
||||||
|
// if the config value "NoWebUI" is true. It returns the controller variable and nil, otherwise error.
|
||||||
func newController(cfg *config) (*controller, error) {
|
func newController(cfg *config) (*controller, error) {
|
||||||
|
|
||||||
c := controller{cfg: cfg}
|
c := controller{cfg: cfg}
|
||||||
|
|
@ -56,6 +59,8 @@ func newController(cfg *config) (*controller, error) {
|
||||||
return &c, nil
|
return &c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// bind binds the paths with the corresponding http.handler and wraps it with the respective middleware,
|
||||||
|
// according to the "NoWebUI" config value.
|
||||||
func (c *controller) bind(pim *pathInfoMux) {
|
func (c *controller) bind(pim *pathInfoMux) {
|
||||||
if !c.cfg.NoWebUI {
|
if !c.cfg.NoWebUI {
|
||||||
pim.handleFunc("/", c.auth(c.index))
|
pim.handleFunc("/", c.auth(c.index))
|
||||||
|
|
@ -66,6 +71,9 @@ func (c *controller) bind(pim *pathInfoMux) {
|
||||||
pim.handleFunc("/api/create", c.auth(api(c.create)))
|
pim.handleFunc("/api/create", c.auth(api(c.create)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// auth wraps the given http.HandlerFunc and returns an new one after authenticating the
|
||||||
|
// password contained in the header "X-CSAF-PROVIDER-AUTH" with the "password" config value
|
||||||
|
// if set, otherwise returns the given http.HandlerFunc.
|
||||||
func (c *controller) auth(
|
func (c *controller) auth(
|
||||||
fn func(http.ResponseWriter, *http.Request),
|
fn func(http.ResponseWriter, *http.Request),
|
||||||
) func(http.ResponseWriter, *http.Request) {
|
) func(http.ResponseWriter, *http.Request) {
|
||||||
|
|
@ -83,6 +91,9 @@ func (c *controller) auth(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// render sets the headers for the response. It applies the given template "tmpl" to
|
||||||
|
// the given object "arg" and writes the output to http.ResponseWriter.
|
||||||
|
// It logs a warning in case of error.
|
||||||
func (c *controller) render(rw http.ResponseWriter, tmpl string, arg interface{}) {
|
func (c *controller) render(rw http.ResponseWriter, tmpl string, arg interface{}) {
|
||||||
rw.Header().Set("Content-type", "text/html; charset=utf-8")
|
rw.Header().Set("Content-type", "text/html; charset=utf-8")
|
||||||
rw.Header().Set("X-Content-Type-Options", "nosniff")
|
rw.Header().Set("X-Content-Type-Options", "nosniff")
|
||||||
|
|
@ -91,17 +102,24 @@ func (c *controller) render(rw http.ResponseWriter, tmpl string, arg interface{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// failed constructs the error messages by calling "asMultiError" and calls "render"
|
||||||
|
// function to render the passed template and error object.
|
||||||
func (c *controller) failed(rw http.ResponseWriter, tmpl string, err error) {
|
func (c *controller) failed(rw http.ResponseWriter, tmpl string, err error) {
|
||||||
result := map[string]interface{}{"Error": asMultiError(err)}
|
result := map[string]interface{}{"Error": asMultiError(err)}
|
||||||
c.render(rw, tmpl, result)
|
c.render(rw, tmpl, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// index calls the "render" function and passes the "index.html" and c.cfg to it.
|
||||||
func (c *controller) index(rw http.ResponseWriter, r *http.Request) {
|
func (c *controller) index(rw http.ResponseWriter, r *http.Request) {
|
||||||
c.render(rw, "index.html", map[string]interface{}{
|
c.render(rw, "index.html", map[string]interface{}{
|
||||||
"Config": c.cfg,
|
"Config": c.cfg,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// web executes the given function "fn", calls the "render" function and passes
|
||||||
|
// the result content from "fn", the given template and the http.ResponseWriter to it
|
||||||
|
// in case of no error occurred, otherwise calls the "failed" function and passes the given
|
||||||
|
// template and the error from "fn".
|
||||||
func (c *controller) web(
|
func (c *controller) web(
|
||||||
fn func(*http.Request) (interface{}, error),
|
fn func(*http.Request) (interface{}, error),
|
||||||
tmpl string,
|
tmpl string,
|
||||||
|
|
@ -116,6 +134,8 @@ func (c *controller) web(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// writeJSON sets the header for the response and writes the JSON encoding of the given "content".
|
||||||
|
// It logs out an error message in case of an error.
|
||||||
func writeJSON(rw http.ResponseWriter, content interface{}, code int) {
|
func writeJSON(rw http.ResponseWriter, content interface{}, code int) {
|
||||||
rw.Header().Set("Content-type", "application/json; charset=utf-8")
|
rw.Header().Set("Content-type", "application/json; charset=utf-8")
|
||||||
rw.Header().Set("X-Content-Type-Options", "nosniff")
|
rw.Header().Set("X-Content-Type-Options", "nosniff")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue