/
utils.go
71 lines (55 loc) · 2.08 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package rest
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/bygui86/go-postgres-cicd/commons"
"github.com/bygui86/go-postgres-cicd/logging"
)
const (
// urls
rootProductsEndpoint = "/products"
productsIdEndpoint = rootProductsEndpoint + "/{id:[0-9]+}"
contentTypeHeaderKey = "Content-Type"
contentTypeApplicationJson = "application/json"
)
// SERVER
func (s *Server) setupRouter() {
logging.Log.Debug("Create new router")
s.router = mux.NewRouter().StrictSlash(true)
s.router.Use(requestInfoPrintingMiddleware)
s.router.HandleFunc(rootProductsEndpoint, s.getProducts).Methods(http.MethodGet)
s.router.HandleFunc(productsIdEndpoint, s.getProduct).Methods(http.MethodGet)
s.router.HandleFunc(rootProductsEndpoint, s.createProduct).Methods(http.MethodPost)
s.router.HandleFunc(productsIdEndpoint, s.updateProduct).Methods(http.MethodPut)
s.router.HandleFunc(productsIdEndpoint, s.deleteProduct).Methods(http.MethodDelete)
}
func (s *Server) setupHTTPServer() {
logging.SugaredLog.Debugf("Create new HTTP server on port %d", s.config.restPort)
if s.config != nil {
s.httpServer = &http.Server{
Addr: fmt.Sprintf(commons.HttpServerHostFormat, s.config.restHost, s.config.restPort),
Handler: s.router,
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: commons.HttpServerWriteTimeoutDefault,
ReadTimeout: commons.HttpServerReadTimeoutDefault,
IdleTimeout: commons.HttpServerIdelTimeoutDefault,
}
return
}
logging.Log.Error("HTTP server creation failed: REST server configurations not loaded")
}
// HANDLERS
func sendJsonResponse(writer http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
writer.Header().Set(contentTypeHeaderKey, contentTypeApplicationJson)
writer.WriteHeader(code)
_, err := writer.Write(response)
if err != nil {
logging.SugaredLog.Errorf("Error sending JSON response: %s", err.Error())
}
}
func sendErrorResponse(writer http.ResponseWriter, code int, message string) {
sendJsonResponse(writer, code, map[string]string{"error": message})
}