Add json output

This commit is contained in:
Eliot Whalan 2016-06-19 16:16:08 +10:00
parent aefde6a154
commit eb6c9ad1fb
No known key found for this signature in database
GPG Key ID: C0A42175139840D6
1 changed files with 68 additions and 17 deletions

85
main.go
View File

@ -1,7 +1,10 @@
package main package main
import ( import (
"crypto/sha1"
"database/sql" "database/sql"
"encoding/base64"
"encoding/json"
"fmt" "fmt"
"html" "html"
"io" "io"
@ -9,10 +12,9 @@ import (
"log" "log"
"net/http" "net/http"
"github.com/gorilla/mux"
"github.com/dchest/uniuri" "github.com/dchest/uniuri"
"github.com/ewhal/pygments" "github.com/ewhal/pygments"
"github.com/gorilla/mux"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
@ -23,6 +25,14 @@ const (
PORT = ":9900" PORT = ":9900"
) )
type Response struct {
ID string `json:"id"`
HASH string `json:"hash"`
URL string `json:"url"`
SIZE int `json:"size"`
DELKEY string `json:"delkey"`
}
func check(err error) { func check(err error) {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -50,29 +60,69 @@ func generateName() string {
return s return s
} }
func save(raw []byte) string { func hash(paste []byte) string {
paste := raw[86 : len(raw)-46] hasher := sha1.New()
hasher.Write(paste)
s := generateName() sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
db, err := sql.Open("sqlite3", "./database.db") return sha
check(err)
stmt, err := db.Prepare("INSERT INTO pastebin(id, hash, data, delkey) values(?,?,?,?)")
_, err = stmt.Exec(s, html.EscapeString(string(paste)))
check(err)
db.Close()
return s
} }
func save(raw []byte) []string {
p := raw[86 : len(raw)-46]
db, err := sql.Open("sqlite3", "./database.db")
check(err)
sha := hash(p)
id := generateName()
url := ADDRESS + "/p/" + id
delKey := uniuri.NewLen(40)
paste := html.EscapeString(string(p))
stmt, err := db.Prepare("INSERT INTO pastebin(id, hash, data, delkey) values(?,?,?,?)")
check(err)
_, err = stmt.Exec(id, sha, paste, delKey)
check(err)
db.Close()
return []string{id, sha, url, paste, delKey}
}
func delHandler(w http.ResponseWriter, r *http.Request) {
}
func saveHandler(w http.ResponseWriter, r *http.Request) { func saveHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
output := vars["output"]
switch r.Method { switch r.Method {
case "POST": case "POST":
buf, err := ioutil.ReadAll(r.Body) buf, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
} }
name := save(buf)
io.WriteString(w, ADDRESS+"/p/"+name+"\n") values := save(buf)
switch output {
case "json":
b := &Response{
ID: values[0],
HASH: values[1],
URL: values[2],
SIZE: len(values[3]),
DELKEY: values[4],
}
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(b)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
default:
io.WriteString(w, values[2]+"\n")
}
} }
} }
@ -120,7 +170,8 @@ func main() {
router.HandleFunc("/p/{pasteId}", pasteHandler) router.HandleFunc("/p/{pasteId}", pasteHandler)
router.HandleFunc("/p/{pasteId}/{lang}", langHandler) router.HandleFunc("/p/{pasteId}/{lang}", langHandler)
router.HandleFunc("/save", saveHandler) router.HandleFunc("/save", saveHandler)
router.HandleFunc("/save", saveHandler) router.HandleFunc("/save/{output}", saveHandler)
router.HandleFunc("/del/{pasteId}/{delKey}", delHandler)
err := http.ListenAndServe(PORT, router) err := http.ListenAndServe(PORT, router)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)