begin work for handle POST form

This commit is contained in:
Ada 2023-10-02 20:33:13 +02:00
parent d6f90977e1
commit 098f53ef67
3 changed files with 64 additions and 8 deletions

36
db.go Normal file
View file

@ -0,0 +1,36 @@
package main
import (
"context"
"github.com/redis/go-redis/v9"
"time"
)
var ctx = context.Background()
func connectDB() *redis.Client {
db := redis.NewClient(&redis.Options{
Addr: currentConfig.redisAddr,
Username: currentConfig.redisUser,
Password: currentConfig.redisPassword,
DB: currentConfig.redisDB,
})
return db
}
func insert_paste(key string, content string, secret string, ttl time.Duration) {
type dbSchema struct {
content string
secret string
}
hash := dbSchema{
content: content,
secret: secret,
}
connectDB().HSet(ctx, key, "content", hash.content)
connectDB().HSet(ctx, key, "secret", hash.secret)
if ttl > -1 {
connectDB().Do(ctx, key, ttl)
}
}

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"net/http" "net/http"
"strings" "strings"
) )
@ -9,6 +10,8 @@ var currentConfig config
func handleRequest(w http.ResponseWriter, r *http.Request) { func handleRequest(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path path := r.URL.Path
switch r.Method {
case "GET":
if path == "/" { if path == "/" {
http.ServeFile(w, r, "./static/index.html") http.ServeFile(w, r, "./static/index.html")
@ -18,6 +21,9 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
} else { } else {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
} }
case "POST":
fmt.Println("Post!")
}
} }
@ -28,6 +34,6 @@ func main() {
err := http.ListenAndServe(listen, nil) err := http.ListenAndServe(listen, nil)
if err != nil { if err != nil {
return fmt.Println(err)
} }
} }

14
utils.go Normal file
View file

@ -0,0 +1,14 @@
package main
import "math/rand"
func generateUrl() string {
length := currentConfig.urlLength
listChars := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, length)
for i := range b {
b[i] = listChars[rand.Intn(len(listChars))]
}
return string(b)
}