From be9d1e3652a86a960477409ce61fd329bf8d22ed Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Thu, 5 Sep 2024 18:26:22 +0200 Subject: Support path prefix --- main.go | 57 +++++++++++++++++++++++++++------------------- templates/admin.html | 4 ++-- templates/admin/users.html | 2 +- templates/base.html | 2 +- templates/createuser.html | 2 +- templates/imgs.html | 8 +++---- templates/imgs_page.html | 4 ++-- templates/index.html | 2 +- templates/login.html | 2 +- templates/nav.html | 2 +- templates/nav_logged.html | 12 +++++----- templates/user.html | 10 ++++---- 12 files changed, 58 insertions(+), 49 deletions(-) diff --git a/main.go b/main.go index 18ae048..c34b952 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "crypto/cipher" "crypto/aes" "embed" + "strings" ) var store = b64decodeAndInitNonce(os.Getenv("SESSION_KEY")) @@ -33,6 +34,8 @@ var baseDocDir string = "docs" const NOTFOUND string = "Not found" const UNAUTH string = "Unauthorized" +var ROOT string + //go:embed templates var tmplContent embed.FS @@ -109,9 +112,11 @@ func serveTemplate(w http.ResponseWriter, r *http.Request, data interface{}, vie d := struct { Data interface{} User *User + ROOT string } { data, nil, + ROOT, } if u, err := checkSession(w, r); u != nil && err == nil { d.User = u @@ -133,9 +138,11 @@ func serveSimple(w http.ResponseWriter, r *http.Request, data interface{}, view d := struct { Data interface{} User *User + ROOT string } { data, nil, + ROOT, } views := []string {view} views = append(views, xviews...) @@ -203,7 +210,7 @@ func serveLogin(w http.ResponseWriter, r *http.Request, errorMsg string) { } func unauthorized(w http.ResponseWriter, r *http.Request) { - sendFlash(w, r, "redirect", r.URL.String()) + sendFlash(w, r, "redirect", ROOT + r.URL.String()) w.WriteHeader(http.StatusUnauthorized) serveLogin(w, r, "") } @@ -227,7 +234,7 @@ func index(w http.ResponseWriter, r *http.Request) { u, err := checkSession(w, r) if u != nil && err == nil { userImpersonation := r.URL.Query().Get("user") - if r.URL.Path != "/" { + if r.URL.Path != ROOT + "/" { data := struct { Msg string UserImpersonation string @@ -294,7 +301,7 @@ func index(w http.ResponseWriter, r *http.Request) { } else if err != nil { log.Println(err) } - if r.URL.Path != "/" { + if r.URL.Path != ROOT + "/" { w.WriteHeader(http.StatusNotFound) serveLogin(w, r, "") return @@ -378,12 +385,12 @@ func createuser(w http.ResponseWriter, r *http.Request) { isadmin := r.FormValue("isadmin") if len(pass) < 10 { sendFlash(w, r, "error", "Le mot de passe doit avoir une longeur supérieure ou égale à 10 caractères.") - http.Redirect(w, r, "/createuser", http.StatusSeeOther) + http.Redirect(w, r, ROOT + "/createuser", http.StatusSeeOther) return } if pass != cpass { sendFlash(w, r, "error", "Le mot de passe et la confirmation du mot de passe ne sont pas les mêmes.") - http.Redirect(w, r, "/createuser", http.StatusSeeOther) + http.Redirect(w, r, ROOT + "/createuser", http.StatusSeeOther) return } user := User{-1, u, email, pass, isadmin == "on"} @@ -392,7 +399,7 @@ func createuser(w http.ResponseWriter, r *http.Request) { sendError(w, r, err.Error(), http.StatusInternalServerError) return } - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, ROOT + "/", http.StatusSeeOther) default: sendInvalidMethod(w, r) } @@ -445,7 +452,7 @@ func logout(w http.ResponseWriter, r *http.Request) { MaxAge: -1, }) } - http.Redirect(w, r, "/login", http.StatusSeeOther) + http.Redirect(w, r, ROOT + "/login", http.StatusSeeOther) default: sendInvalidMethod(w, r) } @@ -454,7 +461,7 @@ func logout(w http.ResponseWriter, r *http.Request) { func login(w http.ResponseWriter, r *http.Request) { u, err := checkSession(w, r) if u != nil && err == nil { - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, ROOT + "/", http.StatusSeeOther) return } switch r.Method { @@ -500,7 +507,7 @@ func handleFileServer(dir, prefix string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { u, err := checkSession(w, r) if u != nil && err == nil { - dir := filepath.Dir(r.URL.Path) + dir := filepath.Dir(strings.TrimPrefix(r.URL.Path, ROOT)) username := filepath.Base(dir) if u.Username == username || u.IsAdmin { hdlr(w, r) @@ -527,7 +534,7 @@ func download(w http.ResponseWriter, r *http.Request) { selection := r.Form["selection"] if len(selection) == 0 { sendFlash(w, r, "error", "Aucun fichier sélectionné") - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, ROOT + "/", http.StatusSeeOther) return } contentDisposition := fmt.Sprintf("attachment; filename=\"Documents.zip\"") @@ -624,9 +631,9 @@ func upload(w http.ResponseWriter, r *http.Request) { } } if userImpersonation { - http.Redirect(w, r, fmt.Sprintf("/?user=%s", username), http.StatusSeeOther) + http.Redirect(w, r, fmt.Sprintf("%s/?user=%s", ROOT, username), http.StatusSeeOther) } else { - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, ROOT + "/", http.StatusSeeOther) } } else { unauthorized(w, r) @@ -640,7 +647,9 @@ func main() { p := flag.Int("p", 8080, "the port to bind to") dbPath := flag.String("d", "./db/test.db", "the db to connect to") docPath := flag.String("f", "docs", "the path of the docs folder") + r := flag.String("r", "", "the root document (for links for reverse proxies)") flag.Parse() + ROOT = *r var err error log.Printf("Connecting to db: %s\n", *dbPath) db, err = InitAndGetDB("sqlite3", *dbPath) @@ -651,24 +660,24 @@ func main() { baseDocDir = *docPath log.Printf("baseDocDir: %s\n", baseDocDir) - http.HandleFunc("/docs/", handleFileServer(baseDocDir, "/docs/")) + http.HandleFunc(ROOT + "/docs/", handleFileServer(baseDocDir, "/docs/")) //http.Handle("/docs/", http.StripPrefix("/docs/", http.FileServer(http.Dir("docs")))) - http.Handle("/static/", http.FileServer(http.FS(staticContent))) - http.HandleFunc("/", index) + http.Handle(ROOT + "/static/", http.FileServer(http.FS(staticContent))) + http.HandleFunc(ROOT + "/", index) //http.HandleFunc("/createuser", createuser) - http.HandleFunc("/login", login) - http.HandleFunc("/logout", logout) - http.HandleFunc("/upload", upload) - http.HandleFunc("/download", download) - http.HandleFunc("/imgs", imgs) - http.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc(ROOT + "/login", login) + http.HandleFunc(ROOT + "/logout", logout) + http.HandleFunc(ROOT + "/upload", upload) + http.HandleFunc(ROOT + "/download", download) + http.HandleFunc(ROOT + "/imgs", imgs) + http.HandleFunc(ROOT + "/robots.txt", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./robots.txt") }) - http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc(ROOT + "/favicon.ico", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "./favicon.ico") }) - http.HandleFunc("/admin", admin) - http.HandleFunc("/admin/users", adminUsers) + http.HandleFunc(ROOT + "/admin", admin) + http.HandleFunc(ROOT + "/admin/users", adminUsers) log.Printf("Serving http://localhost:%d\n", *p) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *p), nil)) } diff --git a/templates/admin.html b/templates/admin.html index 7032daa..b8f7b6d 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -3,10 +3,10 @@
diff --git a/templates/admin/users.html b/templates/admin/users.html index 9aedf44..56561f2 100644 --- a/templates/admin/users.html +++ b/templates/admin/users.html @@ -5,7 +5,7 @@ diff --git a/templates/base.html b/templates/base.html index 653570d..8ce6c65 100644 --- a/templates/base.html +++ b/templates/base.html @@ -3,7 +3,7 @@ - + {{block "title" .}}{{end}} - docspace diff --git a/templates/createuser.html b/templates/createuser.html index aa00ee3..6ad76a3 100644 --- a/templates/createuser.html +++ b/templates/createuser.html @@ -4,7 +4,7 @@ {{if .Data.Error}}

{{.Data.Error}}

{{end}} -
+ Pour des raisons de sécurité, le mot de passe doit avoir une longeur supérieure ou égale à 10 caractères.





diff --git a/templates/imgs.html b/templates/imgs.html index 2f49cc2..9795a9b 100644 --- a/templates/imgs.html +++ b/templates/imgs.html @@ -5,9 +5,9 @@ diff --git a/templates/imgs_page.html b/templates/imgs_page.html index 3aa6bfa..74df9a3 100644 --- a/templates/imgs_page.html +++ b/templates/imgs_page.html @@ -2,11 +2,11 @@ {{range $i, $img := .Data.Imgs}} {{if eq (add $i $.Data.Start) $.Data.End}} {{if ne $.Data.UserImpersonation ""}} -
{{else}} -
{{end}} diff --git a/templates/index.html b/templates/index.html index 860bce2..5f5d6da 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,7 +2,7 @@ {{define "content"}} {{end}} diff --git a/templates/login.html b/templates/login.html index 037879d..1b779d5 100644 --- a/templates/login.html +++ b/templates/login.html @@ -4,7 +4,7 @@ {{if .Data.Error}}

{{.Data.Error}}

{{end}} - +



diff --git a/templates/nav.html b/templates/nav.html index daf061c..fbfc0fb 100644 --- a/templates/nav.html +++ b/templates/nav.html @@ -3,7 +3,7 @@ diff --git a/templates/nav_logged.html b/templates/nav_logged.html index d57df79..4f3c9ca 100644 --- a/templates/nav_logged.html +++ b/templates/nav_logged.html @@ -7,16 +7,16 @@ {{end}} {{if and .Data .Data.UserImpersonation}} -
  • Accueil
  • -
  • Images
  • +
  • Accueil
  • +
  • Images
  • {{else}} -
  • Accueil
  • -
  • Images
  • +
  • Accueil
  • +
  • Images
  • {{end}} {{if .User.IsAdmin}} -
  • Admin
  • +
  • Admin
  • {{end}} -
  • Se déconnecter
  • +
  • Se déconnecter
  • {{end}} diff --git a/templates/user.html b/templates/user.html index 42ebb3d..7e5a98e 100644 --- a/templates/user.html +++ b/templates/user.html @@ -6,17 +6,17 @@

    {{.Data.Error}}

    {{end}} {{if ne .Data.UserImpersonation ""}} - + {{else}} - + {{end}} {{if ne .Data.UserImpersonation ""}} -
    + {{else}} - + {{end}}
    @@ -47,7 +47,7 @@
    - + -- cgit v1.2.3