-
Notifications
You must be signed in to change notification settings - Fork 5
/
webhandler.go
111 lines (94 loc) · 2.85 KB
/
webhandler.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
// SPDX-FileCopyrightText: Copyright (c) 2023-2024, CIQ, Inc. All rights reserved
// SPDX-License-Identifier: Apache-2.0
package pluginsrv
import (
"bytes"
"io"
"net/http"
"path/filepath"
"go.ciq.dev/beskar/internal/pkg/log"
"go.ciq.dev/beskar/internal/pkg/repository"
eventv1 "go.ciq.dev/beskar/pkg/api/event/v1"
pluginv1 "go.ciq.dev/beskar/pkg/api/plugin/v1"
"google.golang.org/protobuf/proto"
)
type webHandler[H repository.Handler] struct {
pluginInfo *pluginv1.Info
manager *repository.Manager[H]
}
func IsTLS(w http.ResponseWriter, r *http.Request) bool {
if r.TLS == nil {
w.WriteHeader(http.StatusUnauthorized)
return false
}
return true
}
// IsTLSMiddleware is a middleware that checks if the request is TLS. This is a convenience wrapper around IsTLS.
func IsTLSMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !IsTLS(w, r) {
return
}
next.ServeHTTP(w, r)
})
}
func (wh *webHandler[H]) event(w http.ResponseWriter, r *http.Request) {
if wh.manager == nil {
w.WriteHeader(http.StatusNotImplemented)
return
}
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusNotImplemented)
return
}
ctx := r.Context()
logger := log.GetContextLogger(ctx)
buf := new(bytes.Buffer)
_, err := io.Copy(buf, r.Body)
if err != nil {
logger.ErrorContext(ctx, "manifest copy", "error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
event := new(eventv1.EventPayload)
if err := proto.Unmarshal(buf.Bytes(), event); err != nil {
logger.ErrorContext(ctx, "unmarshal event", "error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
repositoryName := filepath.Dir(event.Repository)
if repositoryName == "" {
logger.ErrorContext(ctx, "empty repository name")
w.WriteHeader(http.StatusInternalServerError)
return
}
logger.InfoContext(ctx, "process event", "action", event.Action.String(), "repository", repositoryName)
switch event.Action {
case eventv1.Action_ACTION_PUT, eventv1.Action_ACTION_DELETE:
err = wh.manager.Get(ctx, repositoryName).QueueEvent(event, true)
if err != nil {
logger.ErrorContext(ctx, "process put/delete event", "repository", repositoryName, "error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
case eventv1.Action_ACTION_START:
_ = wh.manager.Get(ctx, repositoryName)
case eventv1.Action_ACTION_STOP:
if wh.manager.Has(repositoryName) {
wh.manager.Get(ctx, repositoryName).Stop()
}
}
}
func (wh *webHandler[H]) info(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusNotImplemented)
return
}
w.Header().Set("Content-Type", "application/octet-stream")
data, err := proto.Marshal(wh.pluginInfo)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
_, _ = w.Write(data)
}