super-graph/internal/serv/actions.go

41 lines
718 B
Go
Raw Permalink Normal View History

package serv
import (
"fmt"
"net/http"
)
type actionFn func(w http.ResponseWriter, r *http.Request) error
2020-04-11 08:45:06 +02:00
func newAction(a *Action) (http.Handler, error) {
var fn actionFn
var err error
if len(a.SQL) != 0 {
fn, err = newSQLAction(a)
} else {
return nil, fmt.Errorf("invalid config for action '%s'", a.Name)
}
if err != nil {
return nil, err
}
httpFn := func(w http.ResponseWriter, r *http.Request) {
if err := fn(w, r); err != nil {
renderErr(w, err)
}
}
return http.HandlerFunc(httpFn), nil
}
2020-04-11 08:45:06 +02:00
func newSQLAction(a *Action) (actionFn, error) {
fn := func(w http.ResponseWriter, r *http.Request) error {
_, err := db.ExecContext(r.Context(), a.SQL)
return err
}
return fn, nil
}