flounder

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

commit 7dff255849f753cf7b0d73de584f3bb6cb6f1645
parent 8e3a3b5bff7f3f9fd98b1cf8c3d636623b7164ff
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Wed, 21 Oct 2020 22:16:18 -0700

Add wildcard domains to gemini

Diffstat:
Mconfig.go | 10+++++-----
Dconfig.toml | 4----
Mflounder.toml | 3++-
Mgemini.go | 19++++++++++++++++---
Mgo.mod | 2+-
Mgo.sum | 2++
Mmain.go | 4++--
7 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/config.go b/config.go @@ -5,11 +5,11 @@ import ( ) type Config struct { - FilesPath string - RootDomain string - SiteTitle string - Debug bool - SecretKey string + FilesDirectory string + RootDomain string + SiteTitle string + Debug bool + SecretKey string } func getConfig(filename string) (Config, error) { diff --git a/config.toml b/config.toml @@ -1,4 +0,0 @@ -site_title="flounder" -root_domain="localhost:8080" -debug=true - diff --git a/flounder.toml b/flounder.toml @@ -1,3 +1,4 @@ SiteTitle="­čÉčflounder" -RootDomain="localhost:8080" +RootDomain="localhost" +FilesDirectory="./files" diff --git a/gemini.go b/gemini.go @@ -5,11 +5,13 @@ import ( "crypto/tls" "crypto/x509" // todo move into cert file "encoding/pem" + "strings" // "fmt" "git.sr.ht/~adnano/gmi" + "io/ioutil" "log" "os" - // "path" + "path" "text/template" "time" ) @@ -34,9 +36,19 @@ func gmiIndex(w *gmi.ResponseWriter, r *gmi.Request) { } func gmiPage(w *gmi.ResponseWriter, r *gmi.Request) { + userName := strings.Split(r.URL.Host, ".")[0] + fileName := path.Join(c.FilesDirectory, userName, r.URL.Path) + data, err := ioutil.ReadFile(fileName) + if err != nil { + // return 404 equivalent + log.Fatal(err) + } + // TODO handle error + w.Write(data) } -func runGeminiServer(config *Config) { +func runGeminiServer() { + log.Println("Starting gemini server") var server gmi.Server if err := server.CertificateStore.Load("./tmpcerts"); err != nil { @@ -70,7 +82,8 @@ func runGeminiServer(config *Config) { } // replace with wildcard cert - server.HandleFunc("localhost", gmiIndex) + server.HandleFunc(c.RootDomain, gmiIndex) + server.HandleFunc("*."+c.RootDomain, gmiPage) server.ListenAndServe() } diff --git a/go.mod b/go.mod @@ -3,7 +3,7 @@ module git.sr.ht/flounder go 1.15 require ( - git.sr.ht/~adnano/gmi v0.1.0-alpha.1 + git.sr.ht/~adnano/gmi v0.1.0-alpha.2 github.com/BurntSushi/toml v0.3.1 github.com/mattn/go-sqlite3 v1.14.4 ) diff --git a/go.sum b/go.sum @@ -1,5 +1,7 @@ git.sr.ht/~adnano/gmi v0.1.0-alpha.1 h1:5sha5ucev32U95drEEaPw9rm6hGWJtL8y5HViQ3VDJQ= git.sr.ht/~adnano/gmi v0.1.0-alpha.1/go.mod h1:t/m2KtH+7lXIF7jjVN+bNvwPbE0nxHOpvlA/WZ/KeLQ= +git.sr.ht/~adnano/gmi v0.1.0-alpha.2 h1:5/wzImYT3mJmZ27lazJ8YAdpiVN3QNJruLX7PXOITeo= +git.sr.ht/~adnano/gmi v0.1.0-alpha.2/go.mod h1:t/m2KtH+7lXIF7jjVN+bNvwPbE0nxHOpvlA/WZ/KeLQ= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= diff --git a/main.go b/main.go @@ -71,7 +71,7 @@ func main() { if err != nil { log.Fatal(err) } - runHTTPServer() - // runGeminiServer(&config) + // runHTTPServer() + runGeminiServer() // go log.Fatal(gmi.ListenAndServe(":8080", nil)) }