From 37aa232adbb40e0967b784b9f12b7c42192a6fc8 Mon Sep 17 00:00:00 2001 From: Andy Pilate Date: Sat, 26 Apr 2014 15:06:26 +0200 Subject: [PATCH] Initial commit --- README | 6 +++ index.html | 22 +++++++++ pasteyourmom.go | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ style.css | 20 ++++++++ 4 files changed, 168 insertions(+) create mode 100644 README create mode 100644 index.html create mode 100644 pasteyourmom.go create mode 100644 style.css diff --git a/README b/README new file mode 100644 index 0000000..d64cb69 --- /dev/null +++ b/README @@ -0,0 +1,6 @@ +This is a paste program. + +Why this name? I don't know. +You need Go to use it. + +Under the MIT license. diff --git a/index.html b/index.html new file mode 100644 index 0000000..44ea49a --- /dev/null +++ b/index.html @@ -0,0 +1,22 @@ + + + + Cubox's paste. + + + + + +
+ + +
+ + diff --git a/pasteyourmom.go b/pasteyourmom.go new file mode 100644 index 0000000..3679206 --- /dev/null +++ b/pasteyourmom.go @@ -0,0 +1,120 @@ +package main + +import ( + "io" + "log" + "math/rand" + "net/http" + "os" + "strings" + "time" + + "github.com/zenazn/goji" + "github.com/zenazn/goji/web" +) + +const ( + idSet string = "abcdefjhijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ1234567890" + idLength int = 5 + dataFolder string = "./" +) + +var ( + stdoutLogger *log.Logger + staticFiles []string = []string{"index.html", "style.css"} +) + +func genId() string { + length := idLength + endstr := make([]byte, length) + i := 0 + for ; length > 0; length-- { + endstr[i] = idSet[rand.Intn(len(idSet)-1)] + i++ + } + return string(endstr) +} + +func isStaticFile(file string) bool { + for _, element := range staticFiles { + if file == element { + return true + } + } + return false +} + +func root(c web.C, w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + + file, err := os.Open(dataFolder + "index.html") + if err != nil { + http.Error(w, http.StatusText(500), 500) + stdoutLogger.Print(err) + return + } + + io.Copy(w, file) +} + +func getPaste(c web.C, w http.ResponseWriter, r *http.Request) { + if isStaticFile(c.URLParams["id"]) { + getStatic(c.URLParams["id"], w) + return + } + file, err := os.Open(dataFolder + c.URLParams["id"] + ".paste") + if os.IsNotExist(err) { + http.Error(w, http.StatusText(404), 404) + return + } + if err != nil { + http.Error(w, http.StatusText(500), 500) + stdoutLogger.Print(err) + return + } + io.Copy(w, file) +} + +func createPaste(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + id := genId() + file, err := os.Create(dataFolder + id + ".paste") + for os.IsExist(err) { + id = genId() + file, err = os.Create(dataFolder + id + ".paste") + } + if err != nil { + http.Error(w, http.StatusText(500), 500) + stdoutLogger.Print(err) + return + } + file.Write([]byte(r.Form["text"][0])) + http.Redirect(w, r, id, http.StatusSeeOther) +} + +func getStatic(filename string, w http.ResponseWriter) { + filenameSplitted := strings.Split(filename, ".") + if len(filenameSplitted) > 1 { + w.Header().Set("Content-Type", "text/"+filenameSplitted[len(filenameSplitted)-1]) + } + file, err := os.Open(dataFolder + filename) + if os.IsNotExist(err) { + http.Error(w, http.StatusText(404), 404) + return + } + if err != nil { + http.Error(w, http.StatusText(500), 500) + stdoutLogger.Print(err) + return + } + io.Copy(w, file) +} + +func main() { + stdoutLogger = log.New(os.Stderr, "", log.Flags()) + rand.Seed(time.Now().Unix()) + goji.Get("/", root) + goji.Get("/:id", getPaste) + goji.Post("/", createPaste) + goji.Serve() +} diff --git a/style.css b/style.css new file mode 100644 index 0000000..aec6a7a --- /dev/null +++ b/style.css @@ -0,0 +1,20 @@ +input { + position: fixed; + top: 0px; + left: 0px; +} + +textarea { + position: absolute; + top: 0; + left: 0; + box-sizing: border-box; + width: 100%; + height: 100%; + border: 0; + margin: 0; + padding: 30px 20px; + font-family: monospace; + font-size: 13px; + resize: none; +}