begin work for handle POST form
This commit is contained in:
parent
d6f90977e1
commit
098f53ef67
3 changed files with 64 additions and 8 deletions
36
db.go
Normal file
36
db.go
Normal 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
22
main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -9,14 +10,19 @@ 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
|
||||||
if path == "/" {
|
switch r.Method {
|
||||||
http.ServeFile(w, r, "./static/index.html")
|
case "GET":
|
||||||
|
if path == "/" {
|
||||||
|
http.ServeFile(w, r, "./static/index.html")
|
||||||
|
|
||||||
} else if strings.HasPrefix(path, "/static/") {
|
} else if strings.HasPrefix(path, "/static/") {
|
||||||
fs := http.FileServer(http.Dir("./static"))
|
fs := http.FileServer(http.Dir("./static"))
|
||||||
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
http.Handle("/static/", http.StripPrefix("/static/", fs))
|
||||||
} 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
14
utils.go
Normal 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)
|
||||||
|
}
|
Loading…
Reference in a new issue