Create skeleton for handler
alex wennerberg alex@alexwennerberg.com
Mon, 19 Oct 2020 19:25:35 -0700
7 files changed,
101 insertions(+),
7 deletions(-)
A
config.go
@@ -0,0 +1,22 @@
+package main + +import ( + "github.com/BurntSushi/toml" +) + +type Config struct { + DbURI string + FilesPath string + RootDomain string + SecretKey string +} + +func getConfig(filename string) (Config, error) { + var config Config + // Attempt to overwrite defaults from file + _, err := toml.DecodeFile(filename, &config) + if err != nil { + return config, err + } + return config, nil +}
A
go.mod
@@ -0,0 +1,8 @@
+module git.sr.ht/flounder + +go 1.15 + +require ( + github.com/BurntSushi/toml v0.3.1 + github.com/mattn/go-sqlite3 v1.14.4 +)
A
go.sum
@@ -0,0 +1,4 @@
+github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/mattn/go-sqlite3 v1.14.4 h1:4rQjbDxdu9fSgI/r3KN72G3c2goxknAqHHgPWWs8UlI= +github.com/mattn/go-sqlite3 v1.14.4/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
M
main.go
→
main.go
@@ -1,16 +1,67 @@
package main import ( - "fmt" - "log" - "net/http" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "path" + "path/filepath" +) + +const ( // todo make configurable + userFilesPath = "./files" ) -func handler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) +type File struct { + Creator string + Name string + UpdatedTime string +} + +func getIndexFiles() ([]*File, error) { // cache this function + result := []*File{} + err := filepath.Walk(userFilesPath, func(path string, info os.FileInfo, err error) error { + if err != nil { + log.Printf("Failure accessing a path %q: %v\n", path, err) + return err // think about + } + // make this do what it should + result = append(result, &File{ + Name: info.Name(), + Creator: "alex", + UpdatedTime: "123123", + }) + return nil + }) + if err != nil { + return nil, err + } + // sort + // truncate + return result, nil +} + +func indexHandler(w http.ResponseWriter, r *http.Request) { + indexFiles, _ := getIndexFiles() + for _, file := range indexFiles { + fmt.Fprintf(w, "%s\n", file.Name) + } +} + +func mySiteHandler(w http.ResponseWriter, r *http.Request) { + authUser := "alex" + files, _ := ioutil.ReadDir(path.Join(userFilesPath, authUser)) + for _, file := range files { + fmt.Fprintf(w, "%s\n", file.Name()) + } } func main() { - http.HandleFunc("/", handler) - log.Fatal(http.ListenAndServe(":8080", nil)) + http.HandleFunc("/", indexHandler) + http.HandleFunc("/my_site", mySiteHandler) + // go serve gemini + // go serve http + log.Fatal(http.ListenAndServe(":8080", nil)) }
A
schema.sql
@@ -0,0 +1,7 @@
+CREATE TABLE user ( + id INTEGER PRIMARY KEY NOT NULL, + username TEXT NOT NULL UNIQUE, + email TEXT NOT NULL UNIQUE, + password_hash TEXT NOT NULL, + created_at INTEGER DEFAULT (strftime('%s', 'now')) +);