Feat(index.html, main.go): Add ability to strip fsh.ee specific data.

This commit is contained in:
fshee 2021-06-05 23:03:35 +00:00
parent 054feab897
commit e39a843f18
3 changed files with 44 additions and 21 deletions

View File

@ -29,13 +29,17 @@ A Minimal, SQLite-Backed URL Shortener
<pre><code>| 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.</code></pre>
{{if .Demo}}
<p>
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.
<pre><code>| How to self-host:
|
| 1. Install dependencies:
| a. The Go programming language:
| 1. Install dependencies
| a. The Go programming language
| <a href='https://golang.org/doc/install'>https://golang.org/doc/install</a>
| b. Git version control.
| b. Git version control
| <a href='https://git-scm.com/book/en/v2/Getting-Started-Installing-Git'>https://git-scm.com/book/en/v2/Getting-Started-Installing-Git</a>
|
| * 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.</code></pre>
{{end}}
<footer style='white-space: pre;'>Source code: <a href='https://fsh.ee/src'>fsh.ee/src</a>
License: AGPL v3
Copy: 2021 i@fsh.ee
Made with: Go, Vim, and FreeBSD
Unrelated blog: <a href='https://etc.fsh.ee/'>etc.fsh.ee</a>
License: AGPL v3{{if .Copy}}
Copy: {{.Copy}}{{end}}
Made with: Go, Vim, and FreeBSD{{if .Demo}}
Unrelated blog: <a href='https://etc.fsh.ee/'>etc.fsh.ee</a>{{end}}
</footer>

33
main.go
View File

@ -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))
}

View File

@ -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