Skip to content

Commit

Permalink
feat: add trip
Browse files Browse the repository at this point in the history
  • Loading branch information
totanvix committed Jun 22, 2023
1 parent c5360f5 commit bb78e33
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
}
44 changes: 44 additions & 0 deletions api/trip/index.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
12 changes: 12 additions & 0 deletions utils/middleware/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"src": "/url/(.*)",
"dest": "/api/url/index.go"
},
{
"src": "/trip/(.*)",
"dest": "/api/trip/index.go"
},
{
"src": "/(.*)",
"dest": "/api/index.go"
Expand Down

0 comments on commit bb78e33

Please sign in to comment.