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"
|
2017-08-20 03:52:42 +05:30
|
|
|
"net/http"
|
2020-04-19 05:01:09 +05:30
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-05-01 05:36:45 +05:30
|
|
|
"syscall"
|
2024-02-07 23:03:06 +05:30
|
|
|
"time"
|
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-07 22:04:57 +05:30
|
|
|
"ely.by/chrly/internal/version"
|
2017-08-20 03:52:42 +05:30
|
|
|
)
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
func StartServer(server *http.Server, logger slf.Logger) {
|
2024-02-07 22:04:57 +05:30
|
|
|
logger.Debug("Chrly :v (:c)", wd.StringParam("v", version.Version()), wd.StringParam("c", version.Commit()))
|
2020-04-23 23:52:12 +05:30
|
|
|
|
2024-02-07 23:03:06 +05:30
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM, os.Kill)
|
|
|
|
defer cancel()
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-02-07 23:03:06 +05:30
|
|
|
srvErr := make(chan error, 1)
|
2020-04-19 05:01:09 +05:30
|
|
|
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)
|
2020-04-19 05:01:09 +05:30
|
|
|
}()
|
|
|
|
|
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")
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-02-07 23:03:06 +05:30
|
|
|
stopCtx, cancelFunc := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancelFunc()
|
2020-02-16 15:53:47 +05:30
|
|
|
|
2024-02-07 23:03:06 +05:30
|
|
|
_ = server.Shutdown(stopCtx)
|
2020-02-16 15:53:47 +05:30
|
|
|
|
2024-02-07 23:03:06 +05:30
|
|
|
logger.Info("Graceful shutdown succeed, exiting")
|
2020-02-16 15:53:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Authenticator interface {
|
|
|
|
Authenticate(req *http.Request) error
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:41:39 +05:30
|
|
|
// The current middleware implementation doesn't check the scope assigned to the token.
|
|
|
|
// For now there is only one scope and at this moment I don't want to spend time on it
|
2020-02-16 15:53:47 +05:30
|
|
|
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")
|
|
|
|
|
2024-02-07 18:54:41 +05:30
|
|
|
func apiServerError(resp http.ResponseWriter, err error) {
|
2024-01-30 13:35:04 +05:30
|
|
|
resp.WriteHeader(http.StatusInternalServerError)
|
2024-02-07 18:54:41 +05:30
|
|
|
resp.Header().Set("Content-Type", "text/plain")
|
2024-01-30 13:35:04 +05:30
|
|
|
_, _ = 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
|
|
|
}
|