-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
router.go
125 lines (100 loc) · 3.26 KB
/
router.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"fmt"
"net/http"
"time"
"github.com/rs/zerolog/hlog"
"github.com/rs/zerolog/log"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/cloudbox/autoscan/processor"
"github.com/cloudbox/autoscan/triggers/a_train"
"github.com/cloudbox/autoscan/triggers/lidarr"
"github.com/cloudbox/autoscan/triggers/manual"
"github.com/cloudbox/autoscan/triggers/radarr"
"github.com/cloudbox/autoscan/triggers/readarr"
"github.com/cloudbox/autoscan/triggers/sonarr"
)
func pattern(name string) string {
return fmt.Sprintf("/%s", name)
}
func createCredentials(c config) map[string]string {
creds := make(map[string]string)
creds[c.Auth.Username] = c.Auth.Password
return creds
}
func getRouter(c config, proc *processor.Processor) chi.Router {
r := chi.NewRouter()
// Middleware
r.Use(middleware.Recoverer)
// Logging-related middleware
r.Use(hlog.NewHandler(log.Logger))
r.Use(hlog.RequestIDHandler("id", "request-id"))
r.Use(hlog.URLHandler("url"))
r.Use(hlog.MethodHandler("method"))
r.Use(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
hlog.FromRequest(r).Debug().
Int("status", status).
Dur("duration", duration).
Msg("Request processed")
}))
// Health check
r.Get("/health", healthHandler)
// HTTP-Triggers
r.Route("/triggers", func(r chi.Router) {
// Use Basic Auth middleware if username and password are set.
if c.Auth.Username != "" && c.Auth.Password != "" {
r.Use(middleware.BasicAuth("Autoscan 1.x", createCredentials(c)))
}
// A-Train HTTP-trigger
r.Route("/a-train", func(r chi.Router) {
trigger, err := a_train.New(c.Triggers.ATrain)
if err != nil {
log.Fatal().Err(err).Str("trigger", "a-train").Msg("Failed initialising trigger")
}
r.Post("/{drive}", trigger(proc.Add).ServeHTTP)
})
// Mixed-style Manual HTTP-trigger
r.Route("/manual", func(r chi.Router) {
trigger, err := manual.New(c.Triggers.Manual)
if err != nil {
log.Fatal().Err(err).Str("trigger", "manual").Msg("Failed initialising trigger")
}
r.HandleFunc("/", trigger(proc.Add).ServeHTTP)
})
// OLD-style HTTP-triggers. Can be converted to the /{trigger}/{id} format in a 2.0 release.
for _, t := range c.Triggers.Lidarr {
trigger, err := lidarr.New(t)
if err != nil {
log.Fatal().Err(err).Str("trigger", t.Name).Msg("Failed initialising trigger")
}
r.Post(pattern(t.Name), trigger(proc.Add).ServeHTTP)
}
for _, t := range c.Triggers.Radarr {
trigger, err := radarr.New(t)
if err != nil {
log.Fatal().Err(err).Str("trigger", t.Name).Msg("Failed initialising trigger")
}
r.Post(pattern(t.Name), trigger(proc.Add).ServeHTTP)
}
for _, t := range c.Triggers.Readarr {
trigger, err := readarr.New(t)
if err != nil {
log.Fatal().Err(err).Str("trigger", t.Name).Msg("Failed initialising trigger")
}
r.Post(pattern(t.Name), trigger(proc.Add).ServeHTTP)
}
for _, t := range c.Triggers.Sonarr {
trigger, err := sonarr.New(t)
if err != nil {
log.Fatal().Err(err).Str("trigger", t.Name).Msg("Failed initialising trigger")
}
r.Post(pattern(t.Name), trigger(proc.Add).ServeHTTP)
}
})
return r
}
// Other Handlers
func healthHandler(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
}