From 920306cbcad3df05e01699cd19f3767e069ab139 Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Mon, 13 May 2019 17:42:32 -0400 Subject: template initialization added --- assets/style.css | 32 ++++++++++++++++++++++++++++++ assets/tmpl/index.html | 53 +++++++++++++++++++++++++++++++++++++++++++++++++- getwtxt.json | 5 +++-- handlers.go | 8 +++++--- init.go | 31 ++++++++++++++++++++--------- types.go | 16 ++++++++------- 6 files changed, 123 insertions(+), 22 deletions(-) diff --git a/assets/style.css b/assets/style.css index e148bda..5f62e1c 100644 --- a/assets/style.css +++ b/assets/style.css @@ -1 +1,33 @@ @import url("https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css"); + +#container { + max-width: 800px; + margin: auto; +} + +#head { + padding-top: 20px; + padding-bottom: 20px; + text-align: center; + font-size: 2em; +} + +#subhead { + text-align: right; + font-size: 1.2em; +} + +#body { + font-size: 1.0em; +} + +#info { + font-size: 0.9em; + padding-top: 10px; +} + +#foot { + padding-top: 20px; + text-align: center; + font-size: 0.8em; +} diff --git a/assets/tmpl/index.html b/assets/tmpl/index.html index fdd91ca..e702d08 100644 --- a/assets/tmpl/index.html +++ b/assets/tmpl/index.html @@ -3,11 +3,62 @@ + - {{.Title}} - twtxt Registry + {{.Name}} - twtxt Registry
+ +
+ twtxt registry +
+
+

{{.Desc}}

+

API base URL:

+
/api
+

Formats available:

+
/api/plain
+

Endpoints:

+
/api/plain/users
+/api/plain/mentions
+/api/plain/tweets
+/api/plain/tags
+

Query by user:

+
$ curl 'https://getwtxt.example.com/api/plain/users?q=foo'
+foo               https://example.com/twtxt.txt     2019-05-09T08:42:23.000Z
+foobar            https://example2.com/twtxt.txt    2019-03-14T19:23:00.000Z
+foo_barrington    https://example3.com/twtxt.txt    2019-05-01T15:59:39.000Z
+

Query by tweet content:

+
$ curl 'https://getwtxt.example.com/api/plain/tweets?q=getwtxt'
+foo_barrington    https://example3.com/twtxt.txt    2019-04-30T06:00:09.000Z    I just built getwtxt, time to set it up!
+

Query by mention:

+
$ curl 'https://getwtxt.example.com/api/plain/mentions?url=https://foobarrington.co.uk/twtxt.txt'
+foo    https://example.com/twtxt.txt    2019-02-26T11:06:44.000Z    @ Hey!! Are you still working on that project?
+

Query by tag:

+
$ curl 'https://getwtxt.example.com/api/plain/tags/programming'
+foo    https://example.com/twtxt.txt    2019-03-01T09:31:02.000Z    I love #programming!
+

Get latest 20 tweets:

+
$ curl 'https://getwtxt.example.com/api/plain/tweets'
+foobar    https://example2.com/twtxt.txt    2019-05-13T12:46:20.000Z    It's been a busy day at work!
+...
+

Get all users:

+
$ curl 'https://getwtxt.example.com/api/plain/users'
+foo_barrington    https://example3.com/twtxt.txt    2018-11-21T18:31:00.000Z
+...
+

Add new user:

+
$ curl -X POST 'https://getwtxt.example.com/api/plain/users?url=https://example3.com/twtxt.txt&nickname=foo_barrington'
+OK
+
+
+ Instance Owner: {{.Owner}}
+ Mail: {{.Mail}}
+
+
diff --git a/getwtxt.json b/getwtxt.json index 66c725e..60afa6a 100644 --- a/getwtxt.json +++ b/getwtxt.json @@ -5,7 +5,8 @@ "instance": { "name": "getwtxt", "url": "https://twtxt.example.com", - "owner": "gbmor", - "mail": "ben@gbmor.dev" + "owner": "foo barrington", + "mail": "foo@barrington.ext", + "description": "This is the twtxt registry for the tildeverse network of public unix servers." } } diff --git a/handlers.go b/handlers.go index 3b23761..961ac4f 100644 --- a/handlers.go +++ b/handlers.go @@ -15,9 +15,11 @@ import ( // handles "/" func indexHandler(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", htmlutf8) - n, err := w.Write([]byte("getwtxt v" + getwtxt)) - if err != nil || n == 0 { - log.Printf("Error writing to HTTP stream: %v bytes, %v\n", n, err) + err := tmpls.ExecuteTemplate(w, "index.html", confObj.Instance) + if err != nil { + log.Printf("Error writing to HTTP stream: %v\n", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return } } diff --git a/init.go b/init.go index f871a72..ee76f01 100644 --- a/init.go +++ b/init.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "html/template" "log" "os" "os/signal" @@ -24,11 +25,15 @@ var confObj = &configuration{} // signals to close the log file var closelog = make(chan bool, 1) +// templates +var tmpls *template.Template + func init() { checkFlags() titleScreen() initConfig() initLogging() + tmpls = initTemplates() watchForInterrupt() } @@ -69,15 +74,17 @@ func initConfig() { viper.SetDefault("port", 9001) viper.SetDefault("logfile", "getwtxt.log") - viper.SetDefault("twtxtfile", "/var/twtxt/twtxt.txt") + viper.SetDefault("stdoutLogging", false) confObj.port = viper.GetInt("port") confObj.logfile = viper.GetString("logfile") confObj.stdoutLogging = viper.GetBool("stdoutLogging") - confObj.instance.name = viper.GetString("instance.name") - confObj.instance.url = viper.GetString("instance.url") - confObj.instance.owner = viper.GetString("instance.owner") - confObj.instance.mail = viper.GetString("instance.mail") + confObj.version = getwtxt + confObj.Instance.Name = viper.GetString("instance.name") + confObj.Instance.URL = viper.GetString("instance.url") + confObj.Instance.Owner = viper.GetString("instance.owner") + confObj.Instance.Mail = viper.GetString("instance.mail") + confObj.Instance.Desc = viper.GetString("instance.description") } func initLogging() { @@ -121,15 +128,21 @@ func rebindConfig() { confObj.port = viper.GetInt("port") confObj.logfile = viper.GetString("logfile") confObj.stdoutLogging = viper.GetBool("stdoutLogging") - confObj.instance.name = viper.GetString("instance.name") - confObj.instance.url = viper.GetString("instance.url") - confObj.instance.owner = viper.GetString("instance.owner") - confObj.instance.mail = viper.GetString("instance.mail") + confObj.Instance.Name = viper.GetString("instance.name") + confObj.Instance.URL = viper.GetString("instance.url") + confObj.Instance.Owner = viper.GetString("instance.owner") + confObj.Instance.Mail = viper.GetString("instance.mail") + confObj.Instance.Desc = viper.GetString("instance.description") // reinitialize logging initLogging() } +// Parse the HTML templates +func initTemplates() *template.Template { + return template.Must(template.ParseFiles("assets/tmpl/index.html")) +} + // Watch for SIGINT aka ^C // Close the log file then exit func watchForInterrupt() { diff --git a/types.go b/types.go index c7d1c50..8a707bd 100644 --- a/types.go +++ b/types.go @@ -10,13 +10,15 @@ type configuration struct { port int logfile string stdoutLogging bool - instance + version string + Instance } -// refers to this specific instance of getwtxt -type instance struct { - name string - url string - owner string - mail string +// Instance refers to this specific instance of getwtxt +type Instance struct { + Name string + URL string + Owner string + Mail string + Desc string } -- cgit 1.4.1-2-gfad0