mirror of
https://git.swurl.xyz/swirl/link.git
synced 2023-11-08 10:44:52 +02:00
Feat(index.html, main.go): Add ability to strip fsh.ee specific data.
This commit is contained in:
parent
054feab897
commit
e39a843f18
30
index.html
30
index.html
|
@ -29,13 +29,17 @@ A Minimal, SQLite-Backed URL Shortener
|
||||||
<pre><code>| Examples:
|
<pre><code>| Examples:
|
||||||
|
|
|
|
||||||
| 1. Create a short link to https://duckduckgo.com
|
| 1. Create a short link to https://duckduckgo.com
|
||||||
| $ curl -d https://duckduckgo.com %s
|
| $ curl -d https://duckduckgo.com {{.URL}}
|
||||||
| %s/502fb5543c36014f
|
| {{.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)
|
| $ TMP=$(mktemp)
|
||||||
| $ # temp file will store header
|
| $ # 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
|
| $ # the link has been successfully created
|
||||||
| $ DEL=$(cat $TMP | grep -i delete-with | awk '{print$2}'| tr -d '\r')
|
| $ DEL=$(cat $TMP | grep -i delete-with | awk '{print$2}'| tr -d '\r')
|
||||||
| $ # deletion key is stored in 'X-Delete-With' header.
|
| $ # deletion key is stored in 'X-Delete-With' header.
|
||||||
|
@ -47,6 +51,7 @@ A Minimal, SQLite-Backed URL Shortener
|
||||||
| record not found
|
| record not found
|
||||||
| $ # the link has been successfully deleted.</code></pre>
|
| $ # the link has been successfully deleted.</code></pre>
|
||||||
|
|
||||||
|
{{if .Demo}}
|
||||||
<p>
|
<p>
|
||||||
Please note: this is an example deployment. If you attempt to create a short
|
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
|
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:
|
<pre><code>| How to self-host:
|
||||||
|
|
|
|
||||||
| 1. Install dependencies:
|
| 1. Install dependencies
|
||||||
| a. The Go programming language:
|
| a. The Go programming language
|
||||||
| <a href='https://golang.org/doc/install'>https://golang.org/doc/install</a>
|
| <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>
|
| <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:
|
| * On FreeBSD this would be:
|
||||||
| $ pkg install -y go git
|
| $ 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 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
|
| $ 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.
|
| * The server is now running on localhost at port 8080.
|
||||||
| * If the SQLite database at this filepath does not exist it will be created.
|
| * 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>
|
| * 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>
|
<footer style='white-space: pre;'>Source code: <a href='https://fsh.ee/src'>fsh.ee/src</a>
|
||||||
License: AGPL v3
|
License: AGPL v3{{if .Copy}}
|
||||||
Copy: 2021 i@fsh.ee
|
Copy: {{.Copy}}{{end}}
|
||||||
Made with: Go, Vim, and FreeBSD
|
Made with: Go, Vim, and FreeBSD{{if .Demo}}
|
||||||
Unrelated blog: <a href='https://etc.fsh.ee/'>etc.fsh.ee</a>
|
Unrelated blog: <a href='https://etc.fsh.ee/'>etc.fsh.ee</a>{{end}}
|
||||||
</footer>
|
</footer>
|
||||||
|
|
33
main.go
33
main.go
|
@ -22,6 +22,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"hash/maphash"
|
"hash/maphash"
|
||||||
|
"html/template"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -115,13 +116,15 @@ func (db DB) DelLink(smol, del string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
type controller struct {
|
type controller struct {
|
||||||
log *log.Logger
|
log *log.Logger
|
||||||
db DB
|
db DB
|
||||||
url string
|
demo bool
|
||||||
|
url, copy string
|
||||||
|
tmpl *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewController(logger *log.Logger, db DB, url string) controller {
|
func NewController(logger *log.Logger, db DB, demo bool, url, copy string, tmpl *template.Template) controller {
|
||||||
return controller{logger, db, strings.TrimRight(url, "/")}
|
return controller{logger, db, demo, strings.TrimRight(url, "/"), copy, tmpl}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c controller) Err(rw http.ResponseWriter, r *http.Request, err error) {
|
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, "/") {
|
switch strings.TrimRight(r.URL.Path, "/") {
|
||||||
|
|
||||||
case "":
|
case "":
|
||||||
rw.Header().Add("Content-Type", "text/html")
|
data := map[string]interface{}{
|
||||||
rw.Write([]byte(fmt.Sprintf(indexTemplate, c.url, c.url, c.url)))
|
"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
|
return
|
||||||
|
|
||||||
case "/favicon.ico":
|
case "/favicon.ico":
|
||||||
|
@ -229,10 +239,12 @@ func main() {
|
||||||
startupLogger = log.New(os.Stdout, logPrefix, 0)
|
startupLogger = log.New(os.Stdout, logPrefix, 0)
|
||||||
applicationLogger = log.New(ioutil.Discard, logPrefix, 0)
|
applicationLogger = log.New(ioutil.Discard, logPrefix, 0)
|
||||||
v = flag.Bool("v", false, "verbose logging")
|
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")
|
port = flag.Uint("port", 8080, "port to listen on")
|
||||||
dbFilePath = flag.String("db", "", "sqlite database filepath: required")
|
dbFilePath = flag.String("db", "", "sqlite database filepath: required")
|
||||||
url = flag.String("url", "", "service url: required")
|
url = flag.String("url", "", "service url: required")
|
||||||
hashSeed = flag.String("seed", "", "hash seed: required")
|
hashSeed = flag.String("seed", "", "hash seed: required")
|
||||||
|
copy = flag.String("copy", "", "copyright information")
|
||||||
)
|
)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if *dbFilePath == "" || *url == "" || *hashSeed == "" {
|
if *dbFilePath == "" || *url == "" || *hashSeed == "" {
|
||||||
|
@ -247,7 +259,12 @@ func main() {
|
||||||
startupLogger.Fatal(err)
|
startupLogger.Fatal(err)
|
||||||
return
|
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.Println("listening on port", *port)
|
||||||
startupLogger.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
|
startupLogger.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
|
||||||
}
|
}
|
||||||
|
|
2
makefile
2
makefile
|
@ -22,7 +22,7 @@ test:
|
||||||
|
|
||||||
run: gen build
|
run: gen build
|
||||||
@clear
|
@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:
|
dev:
|
||||||
@find . -type f | grep -E '(.*)\.(go|html)' | entr -cr make run
|
@find . -type f | grep -E '(.*)\.(go|html)' | entr -cr make run
|
||||||
|
|
Loading…
Reference in New Issue