api: Begin implementing an API

This commit is contained in:
Tom 2023-04-28 10:25:36 +02:00
parent 1a5e83be10
commit 2fac0390b1
11 changed files with 186 additions and 23 deletions

View file

@ -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.