feat: api middleware + header content-type as json #5
This commit is contained in:
parent
4c916bdab9
commit
2600c1debb
|
|
@ -3,6 +3,7 @@ package api
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/leandrofars/oktopus/internal/api/middleware"
|
||||||
"github.com/leandrofars/oktopus/internal/db"
|
"github.com/leandrofars/oktopus/internal/db"
|
||||||
"github.com/leandrofars/oktopus/internal/mtp"
|
"github.com/leandrofars/oktopus/internal/mtp"
|
||||||
usp_msg "github.com/leandrofars/oktopus/internal/usp_message"
|
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}/del", a.deviceDeleteMsg).Methods("PUT")
|
||||||
r.HandleFunc("/device/{sn}/set", a.deviceUpdateMsg).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{
|
srv := &http.Server{
|
||||||
Addr: "0.0.0.0:" + a.Port,
|
Addr: "0.0.0.0:" + a.Port,
|
||||||
// Good practice to set timeouts to avoid Slowloris attacks.
|
// Good practice to set timeouts to avoid Slowloris attacks.
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user