Add config file parsing
This commit is contained in:
parent
44b2bbc1b9
commit
b534c2e4e3
19
pastebin.go
19
pastebin.go
|
@ -14,6 +14,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
duration "github.com/channelmeter/iso8601duration"
|
duration "github.com/channelmeter/iso8601duration"
|
||||||
|
@ -45,7 +46,7 @@ type Configuration struct {
|
||||||
var configuration Configuration
|
var configuration Configuration
|
||||||
|
|
||||||
// DATABASE connection String
|
// DATABASE connection String
|
||||||
var DATABASE = configuration.Username + ":" + configuration.Pass + "@/" + configuration.Name + "?charset=utf8"
|
var DATABASE string
|
||||||
|
|
||||||
// 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"))
|
||||||
|
@ -383,7 +384,21 @@ func RootHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
file, err := os.Open("config.json")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
decoder := json.NewDecoder(file)
|
||||||
|
configuration := Configuration{}
|
||||||
|
err = decoder.Decode(&configuration)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
DATABASE = configuration.Username + ":" + configuration.Pass + "@/" + configuration.Name + "?charset=utf8"
|
||||||
|
// create new mux router
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
|
|
||||||
router.HandleFunc("/p/{pasteId}", PasteHandler).Methods("GET")
|
router.HandleFunc("/p/{pasteId}", PasteHandler).Methods("GET")
|
||||||
router.HandleFunc("/raw/{pasteId}", RawHandler).Methods("GET")
|
router.HandleFunc("/raw/{pasteId}", RawHandler).Methods("GET")
|
||||||
router.HandleFunc("/p/{pasteId}/{lang}", PasteHandler).Methods("GET")
|
router.HandleFunc("/p/{pasteId}/{lang}", PasteHandler).Methods("GET")
|
||||||
|
@ -393,7 +408,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(configuration.Port, router)
|
err = http.ListenAndServe(configuration.Port, router)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue