-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroutes.go
More file actions
41 lines (35 loc) · 995 Bytes
/
routes.go
File metadata and controls
41 lines (35 loc) · 995 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 (
"os"
"strings"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
func (tm *TaskManager) configureWebhooks(r chi.Router) {
webhookRouter := func(key, hash string) {
r.With(middleware.WithValue(keyNameContextKey, key)).Post("/wh/"+string(hash), tm.handleRunTask)
tm.logger.Debug("Configured webhook", "key", key)
}
for key, hash := range tm.config.WebhookSecrets {
webhookRouter(key, hash)
}
for key, hashPath := range tm.config.WebhookSecretFiles {
binaryHash, err := os.ReadFile(hashPath)
if err != nil {
panic(err)
}
hash := strings.TrimSpace(string(binaryHash))
webhookRouter(key, hash)
}
}
func (tm *TaskManager) ConfigureRoutes(r chi.Router) {
tm.configureWebhooks(r)
if tm.config.APIKeyNames != nil {
tm.logger.Debug("Configuring API key based route")
r.Route("/tasks/"+tm.taskName, func(r chi.Router) {
r.Use(tm.authorize)
r.Post("/", tm.handleRunTask)
r.Get("/{taskID}", tm.getTaskResult)
})
}
}