aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go57
-rw-r--r--templates/admin.html4
-rw-r--r--templates/admin/users.html2
-rw-r--r--templates/base.html2
-rw-r--r--templates/createuser.html2
-rw-r--r--templates/imgs.html8
-rw-r--r--templates/imgs_page.html4
-rw-r--r--templates/index.html2
-rw-r--r--templates/login.html2
-rw-r--r--templates/nav.html2
-rw-r--r--templates/nav_logged.html12
-rw-r--r--templates/user.html10
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 @@
<div>
<ul>
<li>
- <a href="/admin/users">list users</a>
+ <a href="{{.ROOT}}/admin/users">list users</a>
</li>
<li>
- <a href="/admin/createuser">create user</a>
+ <a href="{{.ROOT}}/admin/createuser">create user</a>
</li>
</ul>
</div>
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 @@
<ul>
{{range .Data.Users}}
<li>
- id: {{.Id}}, user: <a href="/?user={{.Username}}">{{.Username}}</a>, email: <a href="mailto:{{.Email}}">{{.Email}}</a> pass: {{.Pass}}
+ id: {{.Id}}, user: <a href="{{$.ROOT}}/?user={{.Username}}">{{.Username}}</a>, email: <a href="mailto:{{.Email}}">{{.Email}}</a> pass: {{.Pass}}
</li>
{{end}}
</ul>
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 @@
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<link rel="stylesheet" type="text/css" href="/static/style.css">
- <script src="/static/htmx.min.js"></script>
+ <script src="{{.ROOT}}/static/htmx.min.js"></script>
<title>{{block "title" .}}{{end}} - docspace</title>
</head>
<body>
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}}
<p class="error">{{.Data.Error}}</p>
{{end}}
-<form action="/createuser" method="POST">
+<form action="{{.ROOT}}/createuser" method="POST">
<span>Pour des raisons de sécurité, le mot de passe doit avoir une longeur supérieure ou égale à 10 caractères.<br/><br/>
<input required type="text" name="user" id="user" placeholder="Nom d'utilisateur"><br/><br/>
<input required type="email" name="email" id="email" placeholder="Email"><br/><br/>
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 @@
<noscript>
{{if ne .Data.Page 0}}
{{if ne .Data.UserImpersonation ""}}
- <a href="/imgs?page={{add .Data.Page -1}}&user={{.Data.UserImpersonation}}">Previous page</a>
+ <a href="{{.ROOT}}/imgs?page={{add .Data.Page -1}}&user={{.Data.UserImpersonation}}">Previous page</a>
{{else}}
- <a href="/imgs?page={{add .Data.Page -1}}">Previous page</a>
+ <a href="{{.ROOT}}/imgs?page={{add .Data.Page -1}}">Previous page</a>
{{end}}
{{if lt .Data.End .Data.NbFiles}}
|
@@ -15,9 +15,9 @@
{{end}}
{{if lt .Data.End .Data.NbFiles}}
{{if ne .Data.UserImpersonation ""}}
- <a href="/imgs?page={{add .Data.Page 1}}&user={{.Data.UserImpersonation}}">Next page</a>
+ <a href="{{.ROOT}}/imgs?page={{add .Data.Page 1}}&user={{.Data.UserImpersonation}}">Next page</a>
{{else}}
- <a href="/imgs?page={{add .Data.Page 1}}">Next page</a>
+ <a href="{{.ROOT}}/imgs?page={{add .Data.Page 1}}">Next page</a>
{{end}}
{{end}}
</noscript>
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 ""}}
- <div hx-get="/imgs?page={{add $.Data.Page 1}}&fragment&user={{$.Data.UserImpersonation}}"
+ <div hx-get="{{.ROOT}}/imgs?page={{add $.Data.Page 1}}&fragment&user={{$.Data.UserImpersonation}}"
hx-trigger="revealed"
hx-swap="afterend">
{{else}}
- <div hx-get="/imgs?page={{add $.Data.Page 1}}&fragment"
+ <div hx-get="{{.ROOT}}/imgs?page={{add $.Data.Page 1}}&fragment"
hx-trigger="revealed"
hx-swap="afterend">
{{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"}}
<ul>
<li>
- <a href="/login">Connexion</a>
+ <a href="{{.ROOT}}/login">Connexion</a>
</li>
</ul>
{{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}}
<p class="error">{{.Data.Error}}</p>
{{end}}
-<form action="/login" method="POST">
+<form action="{{.ROOT}}/login" method="POST">
<input required type="text" name="user" id="user" placeholder="Nom d'utilisateur"><br/><br/>
<input required type="password" name="pass" id="pass" placeholder="Mot de passe"><br/><br/>
<input type="submit" value="Connexion">
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 @@
<ul class="nav">
<!--<li><a href="/admin">Admin</a></li>
<li><a href="/">Accueil</a></li>-->
- <li><a href="/login">Se connecter</a></li>
+ <li><a href="{{.ROOT}}/login">Se connecter</a></li>
<!--<li><a href="/createuser">Créer un compte</a></li>-->
</ul>
</nav>
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}}
<!--<li><a href="/admin">Admin</a></li>-->
{{if and .Data .Data.UserImpersonation}}
- <li><a href="/?user={{.Data.UserImpersonation}}">Accueil</a></li>
- <li><a href="/imgs?user={{.Data.UserImpersonation}}">Images</a></li>
+ <li><a href="{{.ROOT}}/?user={{.Data.UserImpersonation}}">Accueil</a></li>
+ <li><a href="{{.ROOT}}/imgs?user={{.Data.UserImpersonation}}">Images</a></li>
{{else}}
- <li><a href="/">Accueil</a></li>
- <li><a href="/imgs">Images</a></li>
+ <li><a href="{{.ROOT}}/">Accueil</a></li>
+ <li><a href="{{.ROOT}}/imgs">Images</a></li>
{{end}}
{{if .User.IsAdmin}}
- <li><a href="/admin">Admin</a></li>
+ <li><a href="{{.ROOT}}/admin">Admin</a></li>
{{end}}
- <li><a href="/logout">Se déconnecter</a></li>
+ <li><a href="{{.ROOT}}/logout">Se déconnecter</a></li>
</ul>
</nav>
{{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 @@
<p class="error">{{.Data.Error}}</p>
{{end}}
{{if ne .Data.UserImpersonation ""}}
- <form action="/upload?user={{.Data.UserImpersonation}}" method="POST" enctype="multipart/form-data">
+ <form action="{{.ROOT}}/upload?user={{.Data.UserImpersonation}}" method="POST" enctype="multipart/form-data">
{{else}}
- <form action="/upload" method="POST" enctype="multipart/form-data">
+ <form action="{{.ROOT}}/upload" method="POST" enctype="multipart/form-data">
{{end}}
<input type="file" name="files" multiple />
<input type="submit" value="Upload" />
</form>
{{if ne .Data.UserImpersonation ""}}
- <form action="/download?user={{.Data.UserImpersonation}}" method="POST" class="inline">
+ <form action="{{.ROOT}}/download?user={{.Data.UserImpersonation}}" method="POST" class="inline">
{{else}}
- <form action="/download" method="POST" class="inline">
+ <form action="{{.ROOT}}/download" method="POST" class="inline">
{{end}}
<div class="docs">
<table>
@@ -47,7 +47,7 @@
<br/>
<input type="submit" value="Télécharger les fichiers sélectionnés">
</form>
- <form action="/download" method="GET" class="inlineblk">
+ <form action="{{.ROOT}}/download" method="GET" class="inlineblk">
<input type="hidden" name="user" id="userAll" value="{{.Data.UserImpersonation}}">
<input type="submit" value="Télécharger tous les fichiers">
</form>