api: Begin implementing an API
This commit is contained in:
parent
1a5e83be10
commit
2fac0390b1
11 changed files with 186 additions and 23 deletions
|
|
@ -39,6 +39,10 @@ http:
|
|||
# This email address can be configured here.
|
||||
certbot_email: null
|
||||
|
||||
# Enable or Disable the HTTP API.
|
||||
# In the future, it will be enabled by default, but at this point it is not.
|
||||
api: null
|
||||
|
||||
# Configuration for the (public) homepage of the distillery.
|
||||
home:
|
||||
# the title of the distillery to be set
|
||||
|
|
|
|||
|
|
@ -2,9 +2,12 @@ package config
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/config/validators"
|
||||
"github.com/tkw1536/pkglib/httpx"
|
||||
"golang.org/x/net/idna"
|
||||
)
|
||||
|
||||
|
|
@ -22,6 +25,30 @@ type HTTPConfig struct {
|
|||
// It can be enabled by setting an email for certbot certificates.
|
||||
// This email address can be configured here.
|
||||
CertbotEmail string `yaml:"certbot_email" validate:"email"`
|
||||
|
||||
// API determines if the API is enabled.
|
||||
// In a future version of the distillery, it will be enabled by default.
|
||||
API validators.NullableBool `yaml:"api" validate:"bool" default:"false"`
|
||||
}
|
||||
|
||||
var apiNotEnabled = httpx.Response{
|
||||
StatusCode: http.StatusForbidden,
|
||||
Body: []byte(`{"message":"API is not enabled"}`),
|
||||
}
|
||||
|
||||
func (hcfg HTTPConfig) APIDecorator(methods ...string) func(http.Handler) http.Handler {
|
||||
methods = append(methods, "OPTIONS") // always permit the options method!
|
||||
|
||||
if !hcfg.API.Value {
|
||||
return func(http.Handler) http.Handler {
|
||||
return httpx.PermitMethods(apiNotEnabled, methods...)
|
||||
}
|
||||
}
|
||||
|
||||
// permit only the specified methods
|
||||
return func(h http.Handler) http.Handler {
|
||||
return httpx.PermitMethods(h, methods...)
|
||||
}
|
||||
}
|
||||
|
||||
// JoinPath returns the root public url joined with the provided parts.
|
||||
|
|
|
|||
|
|
@ -8,13 +8,13 @@ import (
|
|||
|
||||
// NullableBool represents a bool that can be null
|
||||
type NullableBool struct {
|
||||
Null, Value bool
|
||||
Set, Value bool
|
||||
}
|
||||
|
||||
func (nb *NullableBool) UnmarshalYAML(value *yaml.Node) error {
|
||||
nb.Null = false
|
||||
nb.Set = true
|
||||
if err := value.Decode(&nb.Value); err != nil {
|
||||
nb.Null = true
|
||||
nb.Set = false
|
||||
nb.Value = false
|
||||
}
|
||||
|
||||
|
|
@ -22,19 +22,19 @@ func (nb *NullableBool) UnmarshalYAML(value *yaml.Node) error {
|
|||
}
|
||||
|
||||
func (nb NullableBool) MarshalYAML() (interface{}, error) {
|
||||
if nb.Null {
|
||||
if !nb.Set {
|
||||
return nil, nil
|
||||
}
|
||||
return nb.Value, nil
|
||||
}
|
||||
|
||||
func ValidateBool(value *NullableBool, dflt string) (err error) {
|
||||
if value.Null {
|
||||
if !value.Set {
|
||||
res, err := strconv.ParseBool(dflt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
value.Null = false
|
||||
value.Set = true
|
||||
value.Value = res
|
||||
}
|
||||
return err
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue