-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
41 lines (34 loc) · 712 Bytes
/
main.go
File metadata and controls
41 lines (34 loc) · 712 Bytes
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
package main
import (
"encoding/json"
"log"
"net/http"
"time"
ratelimiter "github.com/cwinters8/rate-limiter"
)
func hello(w http.ResponseWriter, req *http.Request) {
msg, err := json.Marshal(struct {
Message string `json:"message"`
Timestamp time.Time `json:"timestamp"`
}{
Message: "Hello, world!",
Timestamp: time.Now(),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(msg)
}
func setup() error {
mux := http.NewServeMux()
mux.HandleFunc("/", hello)
limiter := ratelimiter.NewLimiter(10000, 1)
return http.ListenAndServe(":8080", limiter.Limit(mux))
}
func main() {
err := setup()
if err != nil {
log.Fatal(err)
}
}