aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorJulian Hurst <julian.hurst@digdash.com>2024-09-05 18:26:22 +0200
committerJulian Hurst <julian.hurst@digdash.com>2024-09-05 18:26:22 +0200
commitbe9d1e3652a86a960477409ce61fd329bf8d22ed (patch)
tree92b036d66583ed6e9925a3c12f2fecf008f294a9 /main.go
parent635b5da0768d912a7ddf8a3f1ddc6bb8f8865a45 (diff)
downloaddocspace-rootprefix.tar.gz
Support path prefixrootprefix
Diffstat (limited to 'main.go')
-rw-r--r--main.go57
1 files changed, 33 insertions, 24 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))
}