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

@@ -107,11 +107,7 @@ func main() {
apiEndpoint := utils.ApiEndpointUrl()
log.Printf("Photoview API public endpoint ready at %s\n", apiEndpoint.String())
if uiEndpoint := utils.UiEndpointUrl(); uiEndpoint != nil {
log.Printf("Photoview UI public endpoint ready at %s\n", uiEndpoint.String())
} else {
log.Println("Photoview UI public endpoint ready at /")
}
logUIendpointURL()
if !shouldServeUI {
log.Printf("Notice: UI is not served by the the api (%s=0)", utils.EnvServeUI.GetName())
@@ -121,3 +117,11 @@ func main() {
log.Panic(http.ListenAndServe(apiListenURL.Host, handlers.CompressHandler(rootRouter)))
}
func logUIendpointURL() {
if uiEndpoint := utils.UiEndpointUrl(); uiEndpoint != nil {
log.Printf("Photoview UI public endpoint ready at %s\n", uiEndpoint.String())
} else {
log.Println("Photoview UI public endpoint ready at /")
}
}

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
}
}

View File

@@ -56,7 +56,8 @@ func ApiEndpointUrl() *url.URL {
apiEndpointURL, err := url.Parse(apiEndpointStr)
if err != nil {
log.Fatalf("ERROR: Environment variable %s is not a proper url (%s)", EnvAPIEndpoint.GetName(), EnvAPIEndpoint.GetValue())
log.Fatalf("ERROR: Environment variable %s is not a proper url (%s)",
EnvAPIEndpoint.GetName(), EnvAPIEndpoint.GetValue())
}
if shouldServeUI {
@@ -74,7 +75,8 @@ func UiEndpointUrl() *url.URL {
uiEndpointURL, err := url.Parse(EnvUIEndpoint.GetValue())
if err != nil {
log.Fatalf("ERROR: Environment variable %s is not a proper url (%s)", EnvUIEndpoint.GetName(), EnvUIEndpoint.GetValue())
log.Fatalf("ERROR: Environment variable %s is not a proper url (%s)",
EnvUIEndpoint.GetName(), EnvUIEndpoint.GetValue())
}
return uiEndpointURL

View File

@@ -84,7 +84,8 @@ func IsDirSymlink(path string) (bool, error) {
resolvedFile, err := os.Stat(resolvedPath)
if err != nil {
return false, errors.Wrapf(err, "Cannot get fileinfo of linktarget %s of symlink %s, ignoring it", resolvedPath, path)
return false, fmt.Errorf("Cannot get fileinfo of linktarget %s of symlink %s, ignoring it: %w",
resolvedPath, path, err)
}
isDirSymlink = resolvedFile.IsDir()