Compare commits

..

No commits in common. "6567a7c0cd3fbe3f28da8a383dfe2bce4b983c1b" and "8dc45aeb01d3a0bbfd1da293894647027b7d5a17" have entirely different histories.

3 changed files with 17 additions and 33 deletions

26
db.go
View file

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

15
main.go
View file

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

View file

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