Skip to content

Commit

Permalink
add initContext() function
Browse files Browse the repository at this point in the history
  • Loading branch information
stremovsky committed Nov 4, 2024
1 parent f235bed commit 5752ade
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/bunker.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,10 @@ func (w *CustomResponseWriter) WriteHeader(statusCode int) {
var statusCounter = 0
var statusErrorCounter = 0

func reqMiddleware(handler http.Handler) http.Handler {
func (e mainEnv) reqMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
//log.Printf("Set host %s\n", r.Host)
autocontext.Set(r, "host", r.Host)
e.initContext(r)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("X-Frame-Options", "SAMEORIGIN")
w.Header().Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
Expand Down
5 changes: 5 additions & 0 deletions src/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/julienschmidt/httprouter"
"github.com/securitybunker/databunker/src/autocontext"
)

func (e mainEnv) setupConfRouter(router *httprouter.Router) *httprouter.Router {
Expand All @@ -15,6 +16,10 @@ func (e mainEnv) setupConfRouter(router *httprouter.Router) *httprouter.Router {
return router
}

func (e mainEnv) initContext(r *http.Request) {
autocontext.Set(r, "host", r.Host)
}

func (e mainEnv) cookieSettings(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
resultJSON, scriptsJSON, _, err := e.db.getLegalBasisCookieConf()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func loadService() {
},
}
listener := cfg.Server.Host + ":" + cfg.Server.Port
srv := &http.Server{Addr: listener, Handler: reqMiddleware(router), TLSConfig: tlsConfig}
srv := &http.Server{Addr: listener, Handler: e.reqMiddleware(router), TLSConfig: tlsConfig}

stop := make(chan os.Signal, 2)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
Expand Down
2 changes: 1 addition & 1 deletion src/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestUtilGetJSONPost(t *testing.T) {
}

func TestUtilSMS(t *testing.T) {
server := httptest.NewServer(reqMiddleware(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
server := httptest.NewServer(e.reqMiddleware(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(200)
defer req.Body.Close()
Expand Down

0 comments on commit 5752ade

Please sign in to comment.