-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcommon.go
More file actions
61 lines (52 loc) · 1.95 KB
/
common.go
File metadata and controls
61 lines (52 loc) · 1.95 KB
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
// Copyright (c) 2019-2024 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
package handler
import (
"net/http"
"github.com/redhat-developer/web-terminal-exec/pkg/activity"
"github.com/redhat-developer/web-terminal-exec/pkg/constants"
"github.com/redhat-developer/web-terminal-exec/pkg/errors"
"github.com/redhat-developer/web-terminal-exec/pkg/operations"
)
type Router struct {
ActivityManager activity.ActivityManager
ClientProvider operations.ClientProvider
}
func (s *Router) HTTPSHandler() http.Handler {
mux := http.NewServeMux()
handle := func(path string, handler http.Handler, middlewares ...middleware) {
loggingMiddleware := logRequestMiddleware{path}
composedHandler := handler
// Note this will work in reverse order (i.e. the middleware specified last will be used first)
for _, m := range middlewares {
composedHandler = m.addMiddleware(composedHandler)
}
composedHandler = loggingMiddleware.addMiddleware(composedHandler)
mux.Handle(path, composedHandler)
}
handleFunc := func(path string, handler http.HandlerFunc, middlewares ...middleware) {
handle(path, handler, middlewares...)
}
// Serve /activity/tick endpoint
handleFunc(constants.ActivityTickEndpoint, s.handleActivityTick, &authMiddleware{s.ClientProvider})
// Serve /exec/init endpoint
handleFunc(constants.ExecInitEndpoint, s.handleExecInit, &authMiddleware{s.ClientProvider})
// Serve /healthz endpoint
handleFunc(constants.HealthzEndpoint, s.handleHealthCheck)
return http.Handler(mux)
}
func handleError(w http.ResponseWriter, err error) {
switch t := err.(type) {
case *errors.HTTPError:
http.Error(w, t.Message, t.StatusCode)
default:
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}