-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
63 lines (52 loc) · 1.68 KB
/
Copy pathmain.go
File metadata and controls
63 lines (52 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//go:build js && wasm
package main
import (
"log/slog"
"net/http"
"urban-dict/pkg"
"codeberg.org/darckfast/workers-go/platform/cloudflare/durableobjects"
"codeberg.org/darckfast/workers-go/platform/cloudflare/fetch"
"github.com/julienschmidt/httprouter"
)
func main() {
router := httprouter.New()
router.Handler("GET", "/api/urban", http.HandlerFunc(pkg.Handler))
router.HandlerFunc("OPTIONS", "/api/urban", func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
if origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
}
})
fetch.ServeNonBlock(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
stub, err := durableobjects.NewDurableObjectNamespace("RUSTY_LIMITER")
if err != nil {
slog.Error("error retrieving RUSTY_LIMITER", "err", err)
} else {
id := r.Header.Get("cf-connecting-ip")
if id == "" {
id = "127.0.0.1"
}
id += r.Header.Get("x-streamelements-channel")
id += r.Header.Get("nightbot-channel")
id += r.Header.Get("x-fossabot-channelid")
id += r.Header.Get("moobot-channel-id")
do := stub.GetByName(id)
dummy_req, _ := http.NewRequest("GET", "http://dummy?max_reqs=20", nil)
rs, err := do.Fetch(dummy_req)
if err != nil {
slog.Error("error checking rate-limit", "err", err)
w.Write([]byte("ops, something went wrong"))
return
}
if rs.StatusCode == 429 {
slog.Warn("rate-limit exceeded", "id", id, "cooldown", rs.Header.Get("retry-after"))
w.WriteHeader(429)
w.Write([]byte("slowdown, we are getting overloaded"))
w.Header().Set("retry-after", rs.Header.Get("retry-after"))
return
}
}
router.ServeHTTP(w, r)
}))
<-make(chan struct{})
}