Add configuration file

This commit is contained in:
Eliot Whalan 2016-07-20 07:13:34 +10:00
parent e55c76a654
commit 951bb8f2c5
3 changed files with 37 additions and 24 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
pastebin pastebin
database.db config.json

10
config.example.json Normal file
View File

@ -0,0 +1,10 @@
{
"Port": ":8080",
"Length": 6,
"Username": "",
"Password": "",
"Name": "",
"Address": "https://p.pantsu.cat"
}

View File

@ -27,22 +27,25 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
const ( type Configuration struct {
// ADDRESS that pastebin will return links for // ADDRESS that pastebin will return links for
ADDRESS = "http://localhost:9900" Address string
// LENGTH of paste id // LENGTH of paste id
LENGTH = 6 Length int
// PORT that pastebin will listen on // PORT that pastebin will listen on
PORT = ":9900" Port string
// USERNAME for database // USERNAME for database
USERNAME = "" Username string
// PASS database password // PASS database password
PASS = "" Pass string
// NAME database name // NAME database name
NAME = "" Name string
// DATABASE connection String }
DATABASE = USERNAME + ":" + PASS + "@/" + NAME + "?charset=utf8"
) var configuration Configuration
// DATABASE connection String
var DATABASE = configuration.Username + ":" + configuration.Pass + "@/" + configuration.Name + "?charset=utf8"
// Template pages // Template pages
var templates = template.Must(template.ParseFiles("assets/paste.html", "assets/index.html", "assets/clone.html")) var templates = template.Must(template.ParseFiles("assets/paste.html", "assets/index.html", "assets/clone.html"))
@ -79,7 +82,7 @@ func Check(err error) {
// database // database
func GenerateName() string { func GenerateName() string {
// use uniuri to generate random string // use uniuri to generate random string
id := uniuri.NewLen(LENGTH) id := uniuri.NewLen(configuration.Length)
db, err := sql.Open("mysql", DATABASE) db, err := sql.Open("mysql", DATABASE)
Check(err) Check(err)
@ -140,12 +143,12 @@ func Save(raw string, lang string, title string, expiry string) Response {
var id, title, hash, paste, delkey string var id, title, hash, paste, delkey string
err := query.Scan(&id, &title, &hash, &paste, &delkey) err := query.Scan(&id, &title, &hash, &paste, &delkey)
Check(err) Check(err)
url := ADDRESS + "/p/" + id url := configuration.Address + "/p/" + id
return Response{id, title, hash, url, len(paste), delkey} return Response{id, title, hash, url, len(paste), delkey}
} }
} }
id := GenerateName() id := GenerateName()
url := ADDRESS + "/p/" + id url := configuration.Address + "/p/" + id
if lang != "" { if lang != "" {
url += "/" + lang url += "/" + lang
} }
@ -228,7 +231,7 @@ func SaveHandler(w http.ResponseWriter, r *http.Request) {
case "html": case "html":
w.Header().Set("Content-Type", "text/html") w.Header().Set("Content-Type", "text/html")
io.WriteString(w, "<p><b>URL</b>: <a href='"+b.URL+"'>"+b.URL+"</a></p>") io.WriteString(w, "<p><b>URL</b>: <a href='"+b.URL+"'>"+b.URL+"</a></p>")
io.WriteString(w, "<p><b>Delete Key</b>: <a href='"+ADDRESS+"/del/"+b.ID+"/"+b.DELKEY+"'>"+b.DELKEY+"</a></p>") io.WriteString(w, "<p><b>Delete Key</b>: <a href='"+configuration.Address+"/del/"+b.ID+"/"+b.DELKEY+"'>"+b.DELKEY+"</a></p>")
case "redirect": case "redirect":
http.Redirect(w, r, b.URL, 301) http.Redirect(w, r, b.URL, 301)
@ -293,15 +296,15 @@ func PasteHandler(w http.ResponseWriter, r *http.Request) {
s, title := GetPaste(paste, lang) s, title := GetPaste(paste, lang)
// button links // button links
link := ADDRESS + "/raw/" + paste link := configuration.Address + "/raw/" + paste
download := ADDRESS + "/download/" + paste download := configuration.Address + "/download/" + paste
clone := ADDRESS + "/clone/" + paste clone := configuration.Address + "/clone/" + paste
// Page struct // Page struct
p := &Page{ p := &Page{
Title: title, Title: title,
Body: []byte(s), Body: []byte(s),
Raw: link, Raw: link,
Home: ADDRESS, Home: configuration.Address,
Download: download, Download: download,
Clone: clone, Clone: clone,
} }
@ -326,16 +329,16 @@ func CloneHandler(w http.ResponseWriter, r *http.Request) {
s, title := GetPaste(paste, "") s, title := GetPaste(paste, "")
// Page links // Page links
link := ADDRESS + "/raw/" + paste link := configuration.Address + "/raw/" + paste
download := ADDRESS + "/download/" + paste download := configuration.Address + "/download/" + paste
clone := ADDRESS + "/clone/" + paste clone := configuration.Address + "/clone/" + paste
// Clone page struct // Clone page struct
p := &Page{ p := &Page{
Title: title, Title: title,
Body: []byte(s), Body: []byte(s),
Raw: link, Raw: link,
Home: ADDRESS, Home: configuration.Address,
Download: download, Download: download,
Clone: clone, Clone: clone,
} }
@ -390,7 +393,7 @@ func main() {
router.HandleFunc("/p/{output}", SaveHandler).Methods("POST") router.HandleFunc("/p/{output}", SaveHandler).Methods("POST")
router.HandleFunc("/p/{pasteId}/{delKey}", DelHandler).Methods("DELETE") router.HandleFunc("/p/{pasteId}/{delKey}", DelHandler).Methods("DELETE")
router.HandleFunc("/", RootHandler) router.HandleFunc("/", RootHandler)
err := http.ListenAndServe(PORT, router) err := http.ListenAndServe(configuration.Port, router)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }