-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
101 lines (87 loc) · 2.26 KB
/
handler.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
// Copyright (c) Autovia GmbH
// SPDX-License-Identifier: Apache-2.0
package structs
import (
"context"
"log"
"net/http"
)
type Public struct {
*App
H func(e *App, w http.ResponseWriter, r *http.Request) error
}
func (p Public) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("PublicHandler %v", r.URL)
type contextKey string
u := contextKey("user")
ctx := context.WithValue(r.Context(), u, "test")
newReq := r.WithContext(ctx)
err := p.H(p.App, w, newReq)
if err != nil {
log.Print(err)
HandleError(w, err)
return
}
}
type Authorization struct {
*App
H func(a *App, w http.ResponseWriter, r *http.Request) error
}
func (auth Authorization) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("AuthorizationHandler %v", r.URL)
tokenIsValid, token, err := auth.App.AuthManager.Authorize(r)
if tokenIsValid && len(token) > 0 {
ctx := context.WithValue(r.Context(), "token", token)
err = auth.H(auth.App, w, r.WithContext(ctx))
if err != nil {
log.Print(err)
HandleError(w, err)
}
return
}
HandleError(w, RespondError(http.StatusUnauthorized, err))
}
type KubeClient struct {
*App
H func(a *App, c *Client, w http.ResponseWriter, r *http.Request) error
}
func (kc KubeClient) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("KubeClient %v", r.URL)
tokenIsValid, token, err := kc.App.AuthManager.Authorize(r)
if tokenIsValid && len(token) > 0 {
client, err := kc.App.NewKubeClient(token)
if err != nil {
log.Print(err)
HandleError(w, err)
}
err = kc.H(kc.App, client, w, r)
if err != nil {
log.Print(err)
HandleError(w, err)
}
return
}
HandleError(w, RespondError(http.StatusUnauthorized, err))
}
type ApiClient struct {
*App
H func(a *App, c *Client, w http.ResponseWriter, r *http.Request) error
}
func (ac ApiClient) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("ApiClient %v", r.URL)
tokenIsValid, token, err := ac.App.AuthManager.Authorize(r)
if tokenIsValid && len(token) > 0 {
client, err := ac.App.NewApiClient(token)
if err != nil {
log.Print(err)
HandleError(w, err)
}
err = ac.H(ac.App, client, w, r)
if err != nil {
log.Print(err)
HandleError(w, err)
}
return
}
HandleError(w, RespondError(http.StatusUnauthorized, err))
}