From e39a843f187f685335f302fc59844282b7713e43 Mon Sep 17 00:00:00 2001 From: fshee Date: Sat, 5 Jun 2021 23:03:35 +0000 Subject: [PATCH] Feat(index.html, main.go): Add ability to strip fsh.ee specific data. --- index.html | 30 ++++++++++++++++++------------ main.go | 33 +++++++++++++++++++++++++-------- makefile | 2 +- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/index.html b/index.html index 92368c9..f0b245c 100644 --- a/index.html +++ b/index.html @@ -29,13 +29,17 @@ A Minimal, SQLite-Backed URL Shortener
| Examples:
 | 
 | 1. Create a short link to https://duckduckgo.com
-| 	$ curl -d https://duckduckgo.com %s
-| 	%s/502fb5543c36014f
+| 	$ curl -d https://duckduckgo.com {{.URL}}
+| 	{{.URL}}/502fb5543c36014f
 | 
-| 2. Deleting a short link
+| 2. Create a short link with a custom path
+|	$ curl -d https://duckduckgo.com {{.URL}}/ddg
+| 	{{.URL}}/ddg
+| 
+| 3. Deleting a short link
 | 	$ TMP=$(mktemp)
 | 	$ # temp file will store header
-| 	$ LINK=$(curl -sS %s -d https://duckduckgo.com -D $TMP)
+| 	$ LINK=$(curl -sS {{.URL}} -d https://duckduckgo.com -D $TMP)
 | 	$ # the link has been successfully created
 | 	$ DEL=$(cat $TMP | grep -i delete-with | awk '{print$2}'| tr -d '\r')
 | 	$ # deletion key is stored in 'X-Delete-With' header.
@@ -47,6 +51,7 @@ A Minimal, SQLite-Backed URL Shortener
 | 	record not found
 | 	$ # the link has been successfully deleted.
+{{if .Demo}}

Please note: this is an example deployment. If you attempt to create a short link here you will receive a 401 Unauthorized. If you like the examples above @@ -56,26 +61,27 @@ to do (one of the design goals). Below are instructions detailing how.

| How to self-host:
 | 
-| 1. Install dependencies:
-| 	a. The Go programming language:
+| 1. Install dependencies
+| 	a. The Go programming language
 | 		https://golang.org/doc/install
-| 	b. Git version control.
+| 	b. Git version control
 | 		https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
 | 
 | 	* On FreeBSD this would be: 
 | 		$ pkg install -y go git 
 | 
-| 2. Fetch, complile, and execute the source code:
+| 2. Fetch, complile, and execute the source code
 | 	$ env GO111MODULE=off go get git.fsh.ee/i/link
 | 	$ env GO111MODULE=off go run git.fsh.ee/i/link -url https://your.domain.com -port 8080 -db /path/to/sqlite/file -seed secret
 | 
 | * The server is now running on localhost at port 8080.
 | * If the SQLite database at this filepath does not exist it will be created.
 | * All logging will be printed to standard error and standard output.
+{{end}} diff --git a/main.go b/main.go index b06eda1..d35a1d0 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ import ( "flag" "fmt" "hash/maphash" + "html/template" "io/ioutil" "log" "net/http" @@ -115,13 +116,15 @@ func (db DB) DelLink(smol, del string) error { } type controller struct { - log *log.Logger - db DB - url string + log *log.Logger + db DB + demo bool + url, copy string + tmpl *template.Template } -func NewController(logger *log.Logger, db DB, url string) controller { - return controller{logger, db, strings.TrimRight(url, "/")} +func NewController(logger *log.Logger, db DB, demo bool, url, copy string, tmpl *template.Template) controller { + return controller{logger, db, demo, strings.TrimRight(url, "/"), copy, tmpl} } func (c controller) Err(rw http.ResponseWriter, r *http.Request, err error) { @@ -142,8 +145,15 @@ func (c controller) ServeHTTP(rw http.ResponseWriter, r *http.Request) { switch strings.TrimRight(r.URL.Path, "/") { case "": - rw.Header().Add("Content-Type", "text/html") - rw.Write([]byte(fmt.Sprintf(indexTemplate, c.url, c.url, c.url))) + data := map[string]interface{}{ + "URL": c.url, + "Demo": c.demo, + "Copy": c.copy, + } + if err := c.tmpl.Execute(rw, data); err != nil { + c.Err(rw, r, err) + return + } return case "/favicon.ico": @@ -229,10 +239,12 @@ func main() { startupLogger = log.New(os.Stdout, logPrefix, 0) applicationLogger = log.New(ioutil.Discard, logPrefix, 0) v = flag.Bool("v", false, "verbose logging") + demo = flag.Bool("demo", false, "turn on demo mode") port = flag.Uint("port", 8080, "port to listen on") dbFilePath = flag.String("db", "", "sqlite database filepath: required") url = flag.String("url", "", "service url: required") hashSeed = flag.String("seed", "", "hash seed: required") + copy = flag.String("copy", "", "copyright information") ) flag.Parse() if *dbFilePath == "" || *url == "" || *hashSeed == "" { @@ -247,7 +259,12 @@ func main() { startupLogger.Fatal(err) return } - http.Handle("/", NewController(applicationLogger, db, *url)) + tmpl, err := template.New("").Parse(indexTemplate) + if err != nil { + startupLogger.Fatal(err) + return + } + http.Handle("/", NewController(applicationLogger, db, *demo, *url, *copy, tmpl)) startupLogger.Println("listening on port", *port) startupLogger.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)) } diff --git a/makefile b/makefile index f0a9b5e..0a16680 100644 --- a/makefile +++ b/makefile @@ -22,7 +22,7 @@ test: run: gen build @clear - @env $(ENV) ./$(BIN) -v -url https://dev.fsh.ee -port 8080 -db /tmp/link_test_db_1.sql -seed secret + @env $(ENV) ./$(BIN) -v -demo -copy "2021 i@fsh.ee" -url https://dev.fsh.ee -port 8080 -db /tmp/link_test_db_1.sql -seed secret dev: @find . -type f | grep -E '(.*)\.(go|html)' | entr -cr make run