-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 37aa232
Showing
4 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |