chrly/internal/http/http.go

105 lines
2.7 KiB
Go
Raw Normal View History

package http
import (
"context"
"encoding/json"
"net/http"
2024-02-07 23:03:06 +05:30
"time"
"github.com/gorilla/mux"
"github.com/mono83/slf"
"github.com/mono83/slf/wd"
"ely.by/chrly/internal/security"
)
2024-02-14 05:26:48 +05:30
func StartServer(ctx context.Context, server *http.Server, logger slf.Logger) {
2024-02-07 23:03:06 +05:30
srvErr := make(chan error, 1)
go func() {
2024-02-07 23:03:06 +05:30
logger.Info("Starting the server, HTTP on: :addr", wd.StringParam("addr", server.Addr))
srvErr <- server.ListenAndServe()
close(srvErr)
}()
2024-02-07 23:03:06 +05:30
select {
case err := <-srvErr:
logger.Emergency("Error in main(): :err", wd.ErrParam(err))
case <-ctx.Done():
logger.Info("Got stop signal, starting graceful shutdown: :ctx")
2024-02-07 23:03:06 +05:30
stopCtx, cancelFunc := context.WithTimeout(context.Background(), 3*time.Second)
defer cancelFunc()
2024-02-07 23:03:06 +05:30
_ = server.Shutdown(stopCtx)
2024-02-07 23:03:06 +05:30
logger.Info("Graceful shutdown succeed, exiting")
}
}
type Authenticator interface {
Authenticate(req *http.Request, scope security.Scope) error
}
func NewAuthenticationMiddleware(authenticator Authenticator, scope security.Scope) mux.MiddlewareFunc {
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
err := authenticator.Authenticate(req, scope)
if err != nil {
apiForbidden(resp, err.Error())
return
}
handler.ServeHTTP(resp, req)
})
}
}
func NewConditionalMiddleware(cond func(req *http.Request) bool, m mux.MiddlewareFunc) mux.MiddlewareFunc {
return func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
if cond(req) {
handler = m.Middleware(handler)
}
handler.ServeHTTP(resp, req)
})
}
}
func NotFoundHandler(response http.ResponseWriter, _ *http.Request) {
data, _ := json.Marshal(map[string]string{
"status": "404",
"message": "Not Found",
})
response.Header().Set("Content-Type", "application/json")
response.WriteHeader(http.StatusNotFound)
_, _ = response.Write(data)
}
func apiBadRequest(resp http.ResponseWriter, errorsPerField map[string][]string) {
resp.WriteHeader(http.StatusBadRequest)
resp.Header().Set("Content-Type", "application/json")
result, _ := json.Marshal(map[string]any{
"errors": errorsPerField,
})
_, _ = resp.Write(result)
}
var internalServerError = []byte("Internal server error")
2024-02-07 18:54:41 +05:30
func apiServerError(resp http.ResponseWriter, err error) {
resp.WriteHeader(http.StatusInternalServerError)
2024-02-07 18:54:41 +05:30
resp.Header().Set("Content-Type", "text/plain")
_, _ = resp.Write(internalServerError)
}
func apiForbidden(resp http.ResponseWriter, reason string) {
resp.WriteHeader(http.StatusForbidden)
resp.Header().Set("Content-Type", "application/json")
result, _ := json.Marshal(map[string]any{
"error": reason,
})
_, _ = resp.Write(result)
}