Refactoring part 2.11: Long func refactoring in server and utils (#1083)

* Split long functions and optimize code

* Addressing comments

---------

Co-authored-by: Konstantin Koval
This commit is contained in:
Kostiantyn
2024-10-09 18:20:03 +03:00
committed by GitHub
parent 83e441650b
commit 69e595000d
5 changed files with 42 additions and 25 deletions

View File

@@ -28,17 +28,7 @@ func CORSMiddleware(devMode bool) mux.MiddlewareFunc {
}
}
corsEnabled := devMode || uiEndpoint != nil
if corsEnabled {
methods := []string{http.MethodGet, http.MethodPost, http.MethodOptions}
requestHeaders := []string{"authorization", "content-type", "content-length", "TokenPassword"}
responseHeaders := []string{"content-length"}
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ", "))
w.Header().Set("Access-Control-Allow-Headers", strings.Join(requestHeaders, ", "))
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Expose-Headers", strings.Join(responseHeaders, ", "))
}
w = handleCORS(devMode, uiEndpoint, w)
if req.Method != http.MethodOptions {
next.ServeHTTP(w, req)
@@ -48,3 +38,18 @@ func CORSMiddleware(devMode bool) mux.MiddlewareFunc {
})
}
}
func handleCORS(devMode bool, uiEndpoint *url.URL, w http.ResponseWriter) http.ResponseWriter {
corsEnabled := devMode || uiEndpoint != nil
if corsEnabled {
methods := []string{http.MethodGet, http.MethodPost, http.MethodOptions}
requestHeaders := []string{"authorization", "content-type", "content-length", "TokenPassword"}
responseHeaders := []string{"content-length"}
w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ", "))
w.Header().Set("Access-Control-Allow-Headers", strings.Join(requestHeaders, ", "))
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Expose-Headers", strings.Join(responseHeaders, ", "))
}
return w
}

View File

@@ -30,13 +30,18 @@ func WebsocketUpgrader(devMode bool) websocket.Upgrader {
return false
}
if uiEndpoint.Host == originURL.Host {
return true
} else {
log.Printf("Not allowing websocket request from %s because it doesn't match PHOTOVIEW_UI_ENDPOINT %s", originURL.Host, uiEndpoint.Host)
return false
}
return isUIOnSameHost(uiEndpoint, originURL)
}
},
}
}
func isUIOnSameHost(uiEndpoint *url.URL, originURL *url.URL) bool {
if uiEndpoint.Host == originURL.Host {
return true
} else {
log.Printf("Not allowing websocket request from %s because it doesn't match PHOTOVIEW_UI_ENDPOINT %s",
originURL.Host, uiEndpoint.Host)
return false
}
}