Refresh domain map
alex wennerberg alex@alexwennerberg.com
Sat, 09 Jan 2021 16:39:38 -0800
4 files changed,
30 insertions(+),
3 deletions(-)
M
db.go
→
db.go
@@ -99,6 +99,24 @@
return users, nil } +var domains map[string]string + +func refreshDomainMap() error { + rows, err := DB.Query(`SELECT domain, username from user WHERE domain != nil`) + if err != nil { + return err + } + for rows.Next() { + var domain string + var username string + err = rows.Scan(&domain, &username) + if err != nil { + return err + } + } + return nil +} + func getUserByName(username string) (*User, error) { var user User row := DB.QueryRow(`SELECT username, email, active, admin, created_at, reference, domain, domain_enabled from user WHERE username = ?`, username)@@ -182,7 +200,7 @@ reference TEXT NOT NULL default "",
active boolean NOT NULL DEFAULT false, admin boolean NOT NULL DEFAULT false, created_at INTEGER DEFAULT (strftime('%s', 'now')), - domain TEXT NOT NULL default "", + domain TEXT NOT NULL default "" UNIQUE, domain_enabled BOOLEAN NOT NULL DEFAULT false );
M
example-config.toml
→
example-config.toml
@@ -6,7 +6,6 @@ Host="localhost:8165"
# Set this depending on whether you want to run this service standalone or through a reverse proxy server HttpPort=8165 -HttpsEnabled=false # Folder containing subfolders for each user's files FilesDirectory="./files"
M
http.go
→
http.go
@@ -306,6 +306,7 @@ _, err = DB.Exec("update user set domain = ? where username = ?", newDomain, me.Username) // TODO use transaction
if err != nil { errors = append(errors, err.Error()) } else { + refreshDomainMap() log.Printf("Changed domain for %s from %s to %s", authUser, me.Domain, newDomain) } }@@ -544,7 +545,13 @@ // Server a user's file
// TODO replace with gemini proxy // Here be dragons func userFile(w http.ResponseWriter, r *http.Request) { - userName := filepath.Clean(strings.Split(r.Host, ".")[0]) // Clean probably unnecessary + var userName string + custom := domains[r.Host] + if custom != "" { + userName = custom + } else { + userName = filepath.Clean(strings.Split(r.Host, ".")[0]) // Clean probably unnecessary + } p := filepath.Clean(r.URL.Path) var isDir bool fullPath := path.Join(c.FilesDirectory, userName, p) // TODO rename filepath