Compare commits

...

3 commits

4 changed files with 65 additions and 29 deletions

2
db.go
View file

@ -18,7 +18,7 @@ func connectDB() *redis.Client {
return db
}
func insert_paste(key string, content string, secret string, ttl time.Duration) {
func insertPaste(key string, content string, secret string, ttl time.Duration) {
type dbSchema struct {
content string
secret string

17
main.go
View file

@ -2,6 +2,7 @@ package main
import (
"fmt"
"io"
"net/http"
"strings"
)
@ -19,10 +20,22 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
} else {
w.WriteHeader(http.StatusNotFound)
if urlExist(path) {
io.WriteString(w, "exist")
} else {
w.WriteHeader(http.StatusNotFound)
}
}
case "POST":
fmt.Println("Post!")
if path == "/" {
secret := generateSecret()
url := "/" + generateUrl()
content := r.FormValue("content")
insertPaste(url, content, secret, -1)
http.Redirect(w, r, url, http.StatusSeeOther)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
}

View file

@ -9,30 +9,32 @@
<link href="/static/style.css" rel="stylesheet">
</head>
<body>
<label for="content"></label>
<textarea id="content" name="content" placeholder="Your paste here"></textarea>
<nav>
<ul>
<li>
<label for="password">Password?</label>
<input id="password" type="text">
</li>
<li><label for="exp">Expiration?</label>
<input id="exp" type="date"></li>
<li>
<label for="type">Type</label>
<select id="type" name="type">
<option value="">Plaintext</option>
<option value="">Python</option>
</select>
</li>
</ul>
<button>
<svg height="26" viewBox="0 0 24 24" width="26" xmlns="http://www.w3.org/2000/svg">
<polyline points="9 11 12 14 22 4"></polyline>
<path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"></path>
</svg>
</button>
</nav>
<form>
<label for="content"></label>
<textarea id="content" name="content" placeholder="Your paste here"></textarea>
<nav>
<ul>
<li>
<label for="password">Password?</label>
<input id="password" type="text">
</li>
<li><label for="exp">Expiration?</label>
<input id="exp" type="date"></li>
<li>
<label for="type">Type</label>
<select id="type" name="type">
<option value="">Plaintext</option>
<option value="">Python</option>
</select>
</li>
</ul>
<button>
<svg height="26" viewBox="0 0 24 24" width="26" xmlns="http://www.w3.org/2000/svg">
<polyline points="9 11 12 14 22 4"></polyline>
<path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"></path>
</svg>
</button>
</nav>
</form>
</body>
</html>

View file

@ -1,6 +1,8 @@
package main
import "math/rand"
import (
"math/rand"
)
func generateUrl() string {
length := currentConfig.urlLength
@ -12,3 +14,22 @@ func generateUrl() string {
return string(b)
}
func generateSecret() string {
length := 32
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, length)
for i := range b {
b[i] = listChars[rand.Intn(len(listChars))]
}
return string(b)
}
func urlExist(url string) bool {
exist := connectDB().Exists(ctx, url).Val()
if exist == 1 {
return true
}
return false
}