From 951bb8f2c5a0af970269c89db5e2364a36b8c3fe Mon Sep 17 00:00:00 2001 From: Eliot Whalan Date: Wed, 20 Jul 2016 07:13:34 +1000 Subject: [PATCH] Add configuration file --- .gitignore | 2 +- config.example.json | 10 +++++++++ pastebin.go | 49 ++++++++++++++++++++++++--------------------- 3 files changed, 37 insertions(+), 24 deletions(-) create mode 100644 config.example.json diff --git a/.gitignore b/.gitignore index 7d2db9c..4e96fc2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ pastebin -database.db +config.json diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..0a8bff4 --- /dev/null +++ b/config.example.json @@ -0,0 +1,10 @@ +{ + "Port": ":8080", + "Length": 6, + "Username": "", + "Password": "", + "Name": "", + "Address": "https://p.pantsu.cat" + +} + diff --git a/pastebin.go b/pastebin.go index 7c91496..7d5d824 100644 --- a/pastebin.go +++ b/pastebin.go @@ -27,22 +27,25 @@ import ( "github.com/gorilla/mux" ) -const ( +type Configuration struct { // ADDRESS that pastebin will return links for - ADDRESS = "http://localhost:9900" + Address string // LENGTH of paste id - LENGTH = 6 + Length int // PORT that pastebin will listen on - PORT = ":9900" + Port string // USERNAME for database - USERNAME = "" + Username string // PASS database password - PASS = "" + Pass string // NAME database name - NAME = "" - // DATABASE connection String - DATABASE = USERNAME + ":" + PASS + "@/" + NAME + "?charset=utf8" -) + Name string +} + +var configuration Configuration + +// DATABASE connection String +var DATABASE = configuration.Username + ":" + configuration.Pass + "@/" + configuration.Name + "?charset=utf8" // Template pages var templates = template.Must(template.ParseFiles("assets/paste.html", "assets/index.html", "assets/clone.html")) @@ -79,7 +82,7 @@ func Check(err error) { // database func GenerateName() string { // use uniuri to generate random string - id := uniuri.NewLen(LENGTH) + id := uniuri.NewLen(configuration.Length) db, err := sql.Open("mysql", DATABASE) Check(err) @@ -140,12 +143,12 @@ func Save(raw string, lang string, title string, expiry string) Response { var id, title, hash, paste, delkey string err := query.Scan(&id, &title, &hash, &paste, &delkey) Check(err) - url := ADDRESS + "/p/" + id + url := configuration.Address + "/p/" + id return Response{id, title, hash, url, len(paste), delkey} } } id := GenerateName() - url := ADDRESS + "/p/" + id + url := configuration.Address + "/p/" + id if lang != "" { url += "/" + lang } @@ -228,7 +231,7 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) { case "html": w.Header().Set("Content-Type", "text/html") io.WriteString(w, "

URL: "+b.URL+"

") - io.WriteString(w, "

Delete Key: "+b.DELKEY+"

") + io.WriteString(w, "

Delete Key: "+b.DELKEY+"

") case "redirect": http.Redirect(w, r, b.URL, 301) @@ -293,15 +296,15 @@ func PasteHandler(w http.ResponseWriter, r *http.Request) { s, title := GetPaste(paste, lang) // button links - link := ADDRESS + "/raw/" + paste - download := ADDRESS + "/download/" + paste - clone := ADDRESS + "/clone/" + paste + link := configuration.Address + "/raw/" + paste + download := configuration.Address + "/download/" + paste + clone := configuration.Address + "/clone/" + paste // Page struct p := &Page{ Title: title, Body: []byte(s), Raw: link, - Home: ADDRESS, + Home: configuration.Address, Download: download, Clone: clone, } @@ -326,16 +329,16 @@ func CloneHandler(w http.ResponseWriter, r *http.Request) { s, title := GetPaste(paste, "") // Page links - link := ADDRESS + "/raw/" + paste - download := ADDRESS + "/download/" + paste - clone := ADDRESS + "/clone/" + paste + link := configuration.Address + "/raw/" + paste + download := configuration.Address + "/download/" + paste + clone := configuration.Address + "/clone/" + paste // Clone page struct p := &Page{ Title: title, Body: []byte(s), Raw: link, - Home: ADDRESS, + Home: configuration.Address, Download: download, Clone: clone, } @@ -390,7 +393,7 @@ func main() { router.HandleFunc("/p/{output}", SaveHandler).Methods("POST") router.HandleFunc("/p/{pasteId}/{delKey}", DelHandler).Methods("DELETE") router.HandleFunc("/", RootHandler) - err := http.ListenAndServe(PORT, router) + err := http.ListenAndServe(configuration.Port, router) if err != nil { log.Fatal(err) }