all repos — flounder @ 9bf285ed973755ade877c6178960650659b634d9

A small site builder for the Gemini protocol

Refresh domain map
alex wennerberg alex@alexwennerberg.com
Sat, 09 Jan 2021 16:39:38 -0800
commit

9bf285ed973755ade877c6178960650659b634d9

parent

858ec21770c0e2d0b25fc302688fbde9bb24d9f1

4 files changed, 30 insertions(+), 3 deletions(-)

jump to
M db.godb.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.tomlexample-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.gohttp.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
M main.gomain.go

@@ -46,6 +46,9 @@ if c.AnalyticsDBFile != "" {

s1.Every(5).Minute().Do(dumpLogs) } + // load domains in memory + refreshDomainMap() + switch args[0] { case "serve": s1.StartAsync()