From bb78e335d6aa0510bcc2685e3b58d1b97ca6e42c Mon Sep 17 00:00:00 2001 From: totanvix Date: Fri, 23 Jun 2023 02:10:06 +0700 Subject: [PATCH] feat: add trip --- api/index.go | 3 +++ api/trip/index.go | 44 +++++++++++++++++++++++++++++++++++++++ go.mod | 2 +- go.sum | 2 ++ utils/middleware/index.go | 12 +++++++++++ vercel.json | 4 ++++ 6 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 api/trip/index.go diff --git a/api/index.go b/api/index.go index 6b6226c..b070ccf 100644 --- a/api/index.go +++ b/api/index.go @@ -3,6 +3,7 @@ package api import ( "net/http" "zeril-bot/api/hook" + "zeril-bot/api/trip" "zeril-bot/api/url" "zeril-bot/utils/middleware" @@ -20,6 +21,8 @@ func Handler(wri http.ResponseWriter, req *http.Request) { r.Get("/", func(w http.ResponseWriter, r *http.Request) {}) r.Post("/api/hook", hook.Handler) r.Get("/url", url.Handler) + r.Get("/trip", trip.Handler) + r.Post("/trip", trip.Handler) r.ServeHTTP(wri, req) } diff --git a/api/trip/index.go b/api/trip/index.go new file mode 100644 index 0000000..d8b9f40 --- /dev/null +++ b/api/trip/index.go @@ -0,0 +1,44 @@ +package trip + +import ( + "encoding/json" + "net/http" + "zeril-bot/utils/redis" +) + +type data struct { + Url string `json:"url"` +} + +func Handler(w http.ResponseWriter, r *http.Request) { + method := r.Method + + if method == http.MethodPost { + var d data + // Try to decode the request body into the struct. If there is an error, + // respond to the client with the error message and a 400 status code. + err := json.NewDecoder(r.Body).Decode(&d) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + redis.Set("trip", d.Url, 0) + Response(w, d.Url) + + } else { + url := redis.Get("trip") + Response(w, url.Val()) + } +} + +func Response(w http.ResponseWriter, url string) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + resp := make(map[string]string) + resp["url"] = url + + jsonResp, _ := json.Marshal(resp) + w.Write(jsonResp) + return +} diff --git a/go.mod b/go.mod index e3ea7cf..1712f68 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/matoous/go-nanoid v1.5.0 github.com/mmcdole/gofeed v1.2.1 github.com/oliamb/cutter v0.2.2 - github.com/redis/go-redis/v9 v9.0.3 + github.com/redis/go-redis/v9 v9.0.5 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e ) diff --git a/go.sum b/go.sum index a72826a..1b82a35 100644 --- a/go.sum +++ b/go.sum @@ -41,6 +41,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/redis/go-redis/v9 v9.0.3 h1:+7mmR26M0IvyLxGZUHxu4GiBkJkVDid0Un+j4ScYu4k= github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk= +github.com/redis/go-redis/v9 v9.0.5 h1:CuQcn5HIEeK7BgElubPP8CGtE0KakrnbBSTLjathl5o= +github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk= github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 h1:pntxY8Ary0t43dCZ5dqY4YTJCObLY1kIXl0uzMv+7DE= github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= diff --git a/utils/middleware/index.go b/utils/middleware/index.go index 9df04b1..80c119d 100644 --- a/utils/middleware/index.go +++ b/utils/middleware/index.go @@ -15,6 +15,13 @@ import ( func PreRequest(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + + if path == "/trip" { + next.ServeHTTP(w, r) + return + } + var data structs.HookData err := json.NewDecoder(r.Body).Decode(&data) @@ -57,6 +64,11 @@ func PreRequest(next http.Handler) http.Handler { func Recoverer(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { defer func() { + path := r.URL.Path + if path == "/trip" { + return + } + resp := make(map[string]string) w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) diff --git a/vercel.json b/vercel.json index 6c356d7..c2934a6 100644 --- a/vercel.json +++ b/vercel.json @@ -4,6 +4,10 @@ "src": "/url/(.*)", "dest": "/api/url/index.go" }, + { + "src": "/trip/(.*)", + "dest": "/api/trip/index.go" + }, { "src": "/(.*)", "dest": "/api/index.go"