feat: api middleware + header content-type as json #5

This commit is contained in:
Leandro Antônio Farias Machado 2023-04-28 08:32:23 -03:00
parent 4c916bdab9
commit 2600c1debb
2 changed files with 17 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package api
import (
"encoding/json"
"github.com/gorilla/mux"
"github.com/leandrofars/oktopus/internal/api/middleware"
"github.com/leandrofars/oktopus/internal/db"
"github.com/leandrofars/oktopus/internal/mtp"
usp_msg "github.com/leandrofars/oktopus/internal/usp_message"
@ -45,6 +46,10 @@ func StartApi(a Api) {
r.HandleFunc("/device/{sn}/del", a.deviceDeleteMsg).Methods("PUT")
r.HandleFunc("/device/{sn}/set", a.deviceUpdateMsg).Methods("PUT")
r.Use(func(handler http.Handler) http.Handler {
return middleware.Middleware(handler)
})
srv := &http.Server{
Addr: "0.0.0.0:" + a.Port,
// Good practice to set timeouts to avoid Slowloris attacks.

View File

@ -0,0 +1,12 @@
package middleware
import "net/http"
func Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
next.ServeHTTP(w, r)
},
)
}