Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Cubox committed Apr 26, 2014
0 parents commit 37aa232
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 22 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Cubox's paste.</title>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript">
document.onkeyup = function(e) {
e = e || window.event;
if ((e.ctrlKey && e.keyCode == 83)) {
document.paste.submit();
}
};
</script>
</head>
<body>
<form name="paste" action="/" method="post">
<textarea autofocus name="text"></textarea>
<input type="submit" value="Paste">
</form>
</body>
</html>
120 changes: 120 additions & 0 deletions pasteyourmom.go
Original file line number Diff line number Diff line change
@@ -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()
}
20 changes: 20 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 37aa232

Please sign in to comment.