begin work for handle POST form

This commit is contained in:
Ada 2023-10-02 20:33:13 +02:00
parent b66d655780
commit a3d7b2b9da
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)
}
}

22
main.go
View file

@ -1,6 +1,7 @@
package main
import (
"fmt"
"net/http"
"strings"
)
@ -9,14 +10,19 @@ var currentConfig config
func handleRequest(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if path == "/" {
http.ServeFile(w, r, "./static/index.html")
switch r.Method {
case "GET":
if path == "/" {
http.ServeFile(w, r, "./static/index.html")
} else if strings.HasPrefix(path, "/static/") {
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
} else {
w.WriteHeader(http.StatusNotFound)
} else if strings.HasPrefix(path, "/static/") {
fs := http.FileServer(http.Dir("./static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
} else {
w.WriteHeader(http.StatusNotFound)
}
case "POST":
fmt.Println("Post!")
}
}
@ -28,6 +34,6 @@ func main() {
err := http.ListenAndServe(listen, 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)
}