Compare commits

..

2 commits

3 changed files with 33 additions and 17 deletions

26
db.go
View file

@ -3,19 +3,20 @@ package main
import ( import (
"context" "context"
"github.com/redis/go-redis/v9" "github.com/redis/go-redis/v9"
"log"
"time" "time"
) )
var ctx = context.Background() var ctx = context.Background()
func connectDB() *redis.Client { func connectDB() *redis.Client {
db := redis.NewClient(&redis.Options{ localDb := redis.NewClient(&redis.Options{
Addr: currentConfig.redisAddr, Addr: currentConfig.redisAddr,
Username: currentConfig.redisUser, Username: currentConfig.redisUser,
Password: currentConfig.redisPassword, Password: currentConfig.redisPassword,
DB: currentConfig.redisDB, DB: currentConfig.redisDB,
}) })
return db return localDb
} }
func insertPaste(key string, content string, secret string, ttl time.Duration) { func insertPaste(key string, content string, secret string, ttl time.Duration) {
@ -28,15 +29,24 @@ func insertPaste(key string, content string, secret string, ttl time.Duration) {
content: content, content: content,
secret: secret, secret: secret,
} }
db := connectDB() err := db.HSet(ctx, key, "content", hash.content)
db.HSet(ctx, key, "content", hash.content) if err != nil {
db.HSet(ctx, key, "secret", hash.secret) log.Println(err)
}
err = db.HSet(ctx, key, "secret", hash.secret)
if ttl > -1 { if ttl > -1 {
connectDB().Do(ctx, key, ttl) db.Do(ctx, key, ttl)
} }
} }
func getContent(key string) string { func getContent(key string) string {
db := connectDB() content := db.HGet(ctx, key, "content").Val()
return db.HGet(ctx, key, "content").Val() return content
}
func deleteContent(key string) {
err := db.Del(ctx, key)
if err != nil {
log.Println(err)
}
} }

15
main.go
View file

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/redis/go-redis/v9"
"html/template" "html/template"
"io" "io"
"log" "log"
@ -10,6 +11,7 @@ import (
) )
var currentConfig config var currentConfig config
var db *redis.Client
type pasteView struct { type pasteView struct {
Content string Content string
@ -19,7 +21,6 @@ type pasteView struct {
func handleRequest(w http.ResponseWriter, r *http.Request) { func handleRequest(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path path := r.URL.Path
clearPath := strings.ReplaceAll(r.URL.Path, "/raw", "") clearPath := strings.ReplaceAll(r.URL.Path, "/raw", "")
db := connectDB()
switch r.Method { switch r.Method {
case "GET": case "GET":
if path == "/" { if path == "/" {
@ -31,14 +32,14 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
} else { } else {
if urlExist(clearPath) { if urlExist(clearPath) {
if strings.HasSuffix(path, "/raw") { if strings.HasSuffix(path, "/raw") {
pasteContent := db.HGet(ctx, clearPath, "content").Val() pasteContent := getContent(clearPath)
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
_, err := io.WriteString(w, pasteContent) _, err := io.WriteString(w, pasteContent)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
} else { } else {
pasteContent := db.HGet(ctx, path, "content").Val() pasteContent := getContent(path)
s := pasteView{Content: pasteContent, Key: strings.TrimPrefix(path, "/")} s := pasteView{Content: pasteContent, Key: strings.TrimPrefix(path, "/")}
t, err := template.ParseFiles("templates/paste.html") t, err := template.ParseFiles("templates/paste.html")
if err != nil { if err != nil {
@ -68,11 +69,8 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
urlItem := strings.Split(path, "/") urlItem := strings.Split(path, "/")
if urlExist("/" + urlItem[2]) { if urlExist("/" + urlItem[2]) {
secret := r.URL.Query().Get("secret") secret := r.URL.Query().Get("secret")
if secret == db.HGet(ctx, "/"+urlItem[2], "secret").Val() { if verifySecret("/"+urlItem[2], secret) {
err := db.Del(ctx, "/"+urlItem[2]) deleteContent("/" + urlItem[2])
if err != nil {
log.Println(err)
}
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
} else { } else {
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
@ -87,6 +85,7 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
} }
func main() { func main() {
db = connectDB()
currentConfig = getConfig() currentConfig = getConfig()
listen := currentConfig.host + ":" + currentConfig.port listen := currentConfig.host + ":" + currentConfig.port
http.HandleFunc("/", handleRequest) http.HandleFunc("/", handleRequest)

View file

@ -28,6 +28,13 @@ func generateSecret() string {
} }
func urlExist(url string) bool { func urlExist(url string) bool {
exist := connectDB().Exists(ctx, url).Val() exist := db.Exists(ctx, url).Val()
return exist == 1 return exist == 1
} }
func verifySecret(url string, secret string) bool {
if secret == db.HGet(ctx, url, "secret").Val() {
return true
}
return false
}