flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit 9bf285ed973755ade877c6178960650659b634d9
parent 858ec21770c0e2d0b25fc302688fbde9bb24d9f1
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat,  9 Jan 2021 16:39:38 -0800

Refresh domain map

Diffstat:
Mdb.go | 20+++++++++++++++++++-
Mexample-config.toml | 1-
Mhttp.go | 9++++++++-
Mmain.go | 3+++
4 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/db.go b/db.go @@ -99,6 +99,24 @@ func getActiveUserNames() ([]string, error) { 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 @@ func createTablesIfDNE() { 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 ); diff --git a/example-config.toml b/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" diff --git a/http.go b/http.go @@ -306,6 +306,7 @@ func myAccountHandler(w http.ResponseWriter, r *http.Request) { 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 @@ func getFavicon(user string) string { // 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 diff --git a/main.go b/main.go @@ -46,6 +46,9 @@ func main() { s1.Every(5).Minute().Do(dumpLogs) } + // load domains in memory + refreshDomainMap() + switch args[0] { case "serve": s1.StartAsync()