-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
64 lines (49 loc) · 1.25 KB
/
main.go
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
64
// imis is an in-memory image server designed for use with NotSoBot
package main
import (
"flag"
"log"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
// https://github.com/deansheather was here
// request timeout length
const timeout = 15 * time.Second
const (
defaultHost = "0.0.0.0:3000"
defaultToken = "orange_juice"
)
var (
host = defaultHost
token = defaultToken
)
func init() {
flag.StringVar(&host, "host", defaultHost, "address in 0.0.0.0:0 form")
flag.StringVar(&token, "token", defaultToken, "authorization header for protected endpoints")
}
func main() {
flag.Parse()
handler := buildRouter()
log.Fatalln(http.ListenAndServe(host, handler))
}
func buildRouter() http.Handler {
r := chi.NewRouter()
s := Server{
Cache: make(map[string]Blob),
}
// builtin middlewares
r.Use(middleware.GetHead)
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(timeout))
// custom middlewares
auth := AuthorizationMiddleware(token)
r.With(auth).Post("/objects/{key}", s.Upload)
r.With(auth).Get("/objects", s.List)
r.Get("/objects/{key}", s.Get)
return r
}