-
Notifications
You must be signed in to change notification settings - Fork 35
/
router.go
167 lines (143 loc) · 4.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package handlers
import (
"fmt"
htmltemplate "html/template"
"net/http"
"text/template"
visualizer "github.com/jenkins-x/jx-pipelines-visualizer"
"github.com/jenkins-x/jx-pipelines-visualizer/web/handlers/functions"
"github.com/Masterminds/sprig/v3"
"github.com/gorilla/mux"
jxclient "github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned"
jenkinsv1 "github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned/typed/jenkins.io/v1"
"github.com/sirupsen/logrus"
sse "github.com/subchord/go-sse"
tknclient "github.com/tektoncd/pipeline/pkg/client/clientset/versioned"
"github.com/unrolled/render"
"github.com/urfave/negroni/v2"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
type Router struct {
Store *visualizer.Store
KConfig *rest.Config
PAInterface jenkinsv1.PipelineActivityInterface
Namespace string
ArchivedLogsURLTemplate string
Logger *logrus.Logger
render *render.Render
}
func (r Router) Handler() (http.Handler, error) {
r.render = render.New(render.Options{
Directory: "web/templates",
Layout: "layout",
IsDevelopment: true,
Funcs: []htmltemplate.FuncMap{
sprig.HtmlFuncMap(),
htmltemplate.FuncMap{
"pipelinePullRequestURL": functions.PipelinePullRequestURL,
"vdate": functions.VDate,
"isAvailable": functions.IsAvailable,
},
},
})
router := mux.NewRouter()
router.StrictSlash(true)
kClient, err := kubernetes.NewForConfig(r.KConfig)
if err != nil {
return nil, err
}
jxClient, err := jxclient.NewForConfig(r.KConfig)
if err != nil {
return nil, err
}
tknClient, err := tknclient.NewForConfig(r.KConfig)
if err != nil {
return nil, err
}
var archivedLogsURLTemplate *template.Template
if len(r.ArchivedLogsURLTemplate) > 0 {
archivedLogsURLTemplate, err = template.New("archivedLogsURL").Funcs(sprig.TxtFuncMap()).Parse(r.ArchivedLogsURLTemplate)
if err != nil {
return nil, err
}
}
router.Handle("/", &HomeHandler{
Store: r.Store,
Render: r.render,
Logger: r.Logger,
})
router.Handle("/healthz", healthzHandler())
router.Handle("/{owner}", &OwnerHandler{
Store: r.Store,
Render: r.render,
Logger: r.Logger,
})
router.Handle("/{owner}/{repo}", &RepositoryHandler{
Store: r.Store,
Render: r.render,
Logger: r.Logger,
})
router.Handle("/{owner}/{repo}/{branch}", &BranchHandler{
Store: r.Store,
Render: r.render,
Logger: r.Logger,
})
router.Handle("/{owner}/{repo}/{branch}/{build:[0-9]+}", &PipelineHandler{
PAInterface: r.PAInterface,
Render: r.render,
Logger: r.Logger,
})
router.Handle("/{owner}/{repo}/{branch}/{build:[0-9]+}/logs", &LogsHandler{
PAInterface: r.PAInterface,
BuildLogsURLTemplate: archivedLogsURLTemplate,
Logger: r.Logger,
})
router.Handle("/{owner}/{repo}/{branch}/{build:[0-9]+}/logs/live", &LiveLogsHandler{
KubeClient: kClient,
JXClient: jxClient,
TektonClient: tknClient,
Namespace: r.Namespace,
Broker: sse.NewBroker(nil),
Logger: r.Logger,
})
router.Handle("/namespaces/{namespace}/pipelineruns/{pipelineRun}", &PipelineRunHandler{
TektonClient: tknClient,
PAInterface: r.PAInterface,
Store: r.Store,
Render: r.render,
Logger: r.Logger,
})
router.Handle("/teams/{team}/projects/{owner}/{repo}/{branch}/{build:[0-9]+}", jxuiCompatibilityHandler(r.Namespace))
handler := negroni.New(
negroni.NewRecovery(),
&negroni.Static{
Dir: http.Dir("web/static"),
Prefix: "/static",
IndexFile: "index.html",
},
negroni.Wrap(router),
)
return handler, nil
}
func jxuiCompatibilityHandler(namespace string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
team := vars["team"]
owner := vars["owner"]
repo := vars["repo"]
branch := vars["branch"]
build := vars["build"]
if team != namespace {
http.NotFound(w, r)
return
}
redirectURL := fmt.Sprintf("/%s/%s/%s/%s", owner, repo, branch, build)
http.Redirect(w, r, redirectURL, http.StatusMovedPermanently)
})
}
func healthzHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
}