flounder

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

commit 513b89478a4f33bd6297c94602c71aab17032aac
parent b4a35f3a09684d4186607380551ba803b06f9f3f
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Mon, 19 Oct 2020 19:25:35 -0700

Create skeleton for handler

Diffstat:
A.gitignore | 1+
Aconfig.go | 22++++++++++++++++++++++
Aconfig.toml | 1+
Ago.mod | 8++++++++
Ago.sum | 4++++
Mmain.go | 65++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
Aschema.sql | 7+++++++
7 files changed, 101 insertions(+), 7 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +files/ diff --git a/config.go b/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 +} diff --git a/config.toml b/config.toml @@ -0,0 +1 @@ +key=value diff --git a/go.mod b/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 +) diff --git a/go.sum b/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= diff --git a/main.go b/main.go @@ -1,16 +1,67 @@ package main import ( - "fmt" - "log" - "net/http" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "path" + "path/filepath" ) -func handler(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) +const ( // todo make configurable + userFilesPath = "./files" +) + +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)) } diff --git a/schema.sql b/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')) +);