2017-08-20 03:52:42 +05:30
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2020-04-19 05:01:09 +05:30
|
|
|
"context"
|
2020-01-02 02:12:45 +05:30
|
|
|
"encoding/json"
|
2024-01-10 06:12:10 +05:30
|
|
|
"errors"
|
2024-01-30 13:35:04 +05:30
|
|
|
"log/slog"
|
2017-08-20 03:52:42 +05:30
|
|
|
"net/http"
|
2020-04-19 05:01:09 +05:30
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-02-16 15:53:47 +05:30
|
|
|
"strings"
|
2020-05-01 05:36:45 +05:30
|
|
|
"syscall"
|
2020-02-16 15:53:47 +05:30
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2020-04-19 05:01:09 +05:30
|
|
|
"github.com/mono83/slf"
|
|
|
|
"github.com/mono83/slf/wd"
|
2020-04-20 16:59:33 +05:30
|
|
|
|
2024-02-01 12:42:34 +05:30
|
|
|
"ely.by/chrly/internal/dispatcher"
|
|
|
|
v "ely.by/chrly/internal/version"
|
2017-08-20 03:52:42 +05:30
|
|
|
)
|
|
|
|
|
2020-01-29 04:04:15 +05:30
|
|
|
type Emitter interface {
|
2020-04-20 16:59:33 +05:30
|
|
|
dispatcher.Emitter
|
2020-01-29 04:04:15 +05:30
|
|
|
}
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
func StartServer(server *http.Server, logger slf.Logger) {
|
2020-04-23 23:52:12 +05:30
|
|
|
logger.Debug("Chrly :v (:c)", wd.StringParam("v", v.Version()), wd.StringParam("c", v.Commit()))
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
done := make(chan bool, 1)
|
|
|
|
go func() {
|
|
|
|
logger.Info("Starting the server, HTTP on: :addr", wd.StringParam("addr", server.Addr))
|
2024-01-10 06:12:10 +05:30
|
|
|
if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
2020-04-19 05:01:09 +05:30
|
|
|
logger.Emergency("Error in main(): :err", wd.ErrParam(err))
|
2020-05-01 02:36:56 +05:30
|
|
|
close(done)
|
2020-04-19 05:01:09 +05:30
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
s := waitForExitSignal()
|
|
|
|
logger.Info("Got signal: :signal, starting graceful shutdown", wd.StringParam("signal", s.String()))
|
2021-02-26 07:15:45 +05:30
|
|
|
_ = server.Shutdown(context.Background())
|
2020-04-19 05:01:09 +05:30
|
|
|
logger.Info("Graceful shutdown succeed, exiting", wd.StringParam("signal", s.String()))
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitForExitSignal() os.Signal {
|
|
|
|
ch := make(chan os.Signal, 1)
|
2020-05-01 05:36:45 +05:30
|
|
|
signal.Notify(ch, os.Interrupt, syscall.SIGTERM, os.Kill)
|
2020-04-19 05:01:09 +05:30
|
|
|
|
|
|
|
return <-ch
|
|
|
|
}
|
|
|
|
|
2020-02-16 15:53:47 +05:30
|
|
|
type loggingResponseWriter struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
statusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lrw *loggingResponseWriter) WriteHeader(code int) {
|
|
|
|
lrw.statusCode = code
|
|
|
|
lrw.ResponseWriter.WriteHeader(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateRequestEventsMiddleware(emitter Emitter, prefix string) mux.MiddlewareFunc {
|
|
|
|
beforeTopic := strings.Join([]string{prefix, "before_request"}, ":")
|
|
|
|
afterTopic := strings.Join([]string{prefix, "after_request"}, ":")
|
|
|
|
|
|
|
|
return func(handler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
emitter.Emit(beforeTopic, req)
|
|
|
|
|
|
|
|
lrw := &loggingResponseWriter{
|
|
|
|
ResponseWriter: resp,
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
}
|
|
|
|
handler.ServeHTTP(lrw, req)
|
|
|
|
|
|
|
|
emitter.Emit(afterTopic, req, lrw.statusCode)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Authenticator interface {
|
|
|
|
Authenticate(req *http.Request) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateAuthenticationMiddleware(checker Authenticator) mux.MiddlewareFunc {
|
|
|
|
return func(handler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
err := checker.Authenticate(req)
|
|
|
|
if err != nil {
|
|
|
|
apiForbidden(resp, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.ServeHTTP(resp, req)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
func NotFoundHandler(response http.ResponseWriter, _ *http.Request) {
|
2020-01-02 02:12:45 +05:30
|
|
|
data, _ := json.Marshal(map[string]string{
|
|
|
|
"status": "404",
|
|
|
|
"message": "Not Found",
|
|
|
|
})
|
2017-08-20 03:52:42 +05:30
|
|
|
|
2020-01-02 02:12:45 +05:30
|
|
|
response.Header().Set("Content-Type", "application/json")
|
|
|
|
response.WriteHeader(http.StatusNotFound)
|
|
|
|
_, _ = response.Write(data)
|
2017-08-20 03:52:42 +05:30
|
|
|
}
|
|
|
|
|
2020-01-02 02:12:45 +05:30
|
|
|
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]interface{}{
|
|
|
|
"errors": errorsPerField,
|
|
|
|
})
|
|
|
|
_, _ = resp.Write(result)
|
2017-08-20 03:52:42 +05:30
|
|
|
}
|
|
|
|
|
2024-01-30 13:35:04 +05:30
|
|
|
var internalServerError = []byte("Internal server error")
|
|
|
|
|
|
|
|
func apiServerError(resp http.ResponseWriter, msg string, err error) {
|
|
|
|
resp.WriteHeader(http.StatusInternalServerError)
|
|
|
|
resp.Header().Set("Content-Type", "application/json")
|
|
|
|
slog.Error(msg, slog.Any("error", err))
|
|
|
|
_, _ = resp.Write(internalServerError)
|
|
|
|
}
|
|
|
|
|
2020-01-02 02:12:45 +05:30
|
|
|
func apiForbidden(resp http.ResponseWriter, reason string) {
|
|
|
|
resp.WriteHeader(http.StatusForbidden)
|
|
|
|
resp.Header().Set("Content-Type", "application/json")
|
|
|
|
result, _ := json.Marshal(map[string]interface{}{
|
|
|
|
"error": reason,
|
|
|
|
})
|
|
|
|
_, _ = resp.Write(result)
|
2017-08-20 03:52:42 +05:30
|
|
|
}
|
|
|
|
|
2020-01-02 02:12:45 +05:30
|
|
|
func apiNotFound(resp http.ResponseWriter, reason string) {
|
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
|
|
|
resp.Header().Set("Content-Type", "application/json")
|
|
|
|
result, _ := json.Marshal([]interface{}{
|
|
|
|
reason,
|
|
|
|
})
|
|
|
|
_, _ = resp.Write(result)
|
|
|
|
}
|