More comments, rename package to main and roothandler
This commit is contained in:
parent
676fac4b2e
commit
58052b5a9f
24
pastebin.go
24
pastebin.go
|
@ -1,5 +1,5 @@
|
||||||
// Package pastebin is a simple modern and powerful pastebin service
|
// Package pastebin is a simple modern and powerful pastebin service
|
||||||
package pastebin
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
|
@ -215,14 +215,13 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
switch output {
|
switch output {
|
||||||
case "json":
|
case "json":
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
err := json.NewEncoder(w).Encode(b)
|
err := json.NewEncoder(w).Encode(b)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
case "xml":
|
case "xml":
|
||||||
x, err := xml.MarshalIndent(b, "", " ")
|
x, err := xml.MarshalIndent(b, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -296,10 +295,14 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
paste := vars["pasteId"]
|
paste := vars["pasteId"]
|
||||||
lang := vars["lang"]
|
lang := vars["lang"]
|
||||||
|
|
||||||
s, title := getPaste(paste, lang)
|
s, title := getPaste(paste, lang)
|
||||||
|
|
||||||
|
// button links
|
||||||
link := ADDRESS + "/raw/" + paste
|
link := ADDRESS + "/raw/" + paste
|
||||||
download := ADDRESS + "/download/" + paste
|
download := ADDRESS + "/download/" + paste
|
||||||
clone := ADDRESS + "/clone/" + paste
|
clone := ADDRESS + "/clone/" + paste
|
||||||
|
// Page struct
|
||||||
p := &Page{
|
p := &Page{
|
||||||
Title: title,
|
Title: title,
|
||||||
Body: []byte(s),
|
Body: []byte(s),
|
||||||
|
@ -324,10 +327,15 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
func cloneHandler(w http.ResponseWriter, r *http.Request) {
|
func cloneHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
paste := vars["pasteId"]
|
paste := vars["pasteId"]
|
||||||
|
|
||||||
s, title := getPaste(paste, "")
|
s, title := getPaste(paste, "")
|
||||||
|
|
||||||
|
// Page links
|
||||||
link := ADDRESS + "/raw/" + paste
|
link := ADDRESS + "/raw/" + paste
|
||||||
download := ADDRESS + "/download/" + paste
|
download := ADDRESS + "/download/" + paste
|
||||||
clone := ADDRESS + "/clone/" + paste
|
clone := ADDRESS + "/clone/" + paste
|
||||||
|
|
||||||
|
// Clone page struct
|
||||||
p := &Page{
|
p := &Page{
|
||||||
Title: title,
|
Title: title,
|
||||||
Body: []byte(s),
|
Body: []byte(s),
|
||||||
|
@ -357,11 +365,19 @@ func rawHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
paste := vars["pasteId"]
|
paste := vars["pasteId"]
|
||||||
s, _ := getPaste(paste, "")
|
s, _ := getPaste(paste, "")
|
||||||
|
|
||||||
// simply write string to browser
|
// simply write string to browser
|
||||||
io.WriteString(w, s)
|
io.WriteString(w, s)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
err := templates.ExecuteTemplate(w, "index.html", &Page{})
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
router := mux.NewRouter()
|
router := mux.NewRouter()
|
||||||
router.HandleFunc("/p/{pasteId}", pasteHandler)
|
router.HandleFunc("/p/{pasteId}", pasteHandler)
|
||||||
|
@ -372,7 +388,7 @@ func main() {
|
||||||
router.HandleFunc("/save", saveHandler)
|
router.HandleFunc("/save", saveHandler)
|
||||||
router.HandleFunc("/save/{output}", saveHandler)
|
router.HandleFunc("/save/{output}", saveHandler)
|
||||||
router.HandleFunc("/del/{pasteId}/{delKey}", delHandler)
|
router.HandleFunc("/del/{pasteId}/{delKey}", delHandler)
|
||||||
router.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("assets/"))))
|
router.HandleFunc("/", rootHandler)
|
||||||
err := http.ListenAndServe(PORT, router)
|
err := http.ListenAndServe(PORT, router)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
Loading…
Reference in New Issue