Compare commits
3 commits
098f53ef67
...
73c06c3636
Author | SHA1 | Date | |
---|---|---|---|
73c06c3636 | |||
85dff8deca | |||
dc68e4a8d4 |
4 changed files with 65 additions and 29 deletions
2
db.go
2
db.go
|
@ -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
|
||||
|
|
15
main.go
15
main.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
@ -18,11 +19,23 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
|
|||
} else if strings.HasPrefix(path, "/static/") {
|
||||
fs := http.FileServer(http.Dir("./static"))
|
||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||
} else {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
<link href="/static/style.css" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<form>
|
||||
<label for="content"></label>
|
||||
<textarea id="content" name="content" placeholder="Your paste here"></textarea>
|
||||
<nav>
|
||||
|
@ -34,5 +35,6 @@
|
|||
</svg>
|
||||
</button>
|
||||
</nav>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
23
utils.go
23
utils.go
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue