-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
149 lines (123 loc) · 3.18 KB
/
server.go
File metadata and controls
149 lines (123 loc) · 3.18 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
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
package main
import (
"context"
"encoding/json"
"fmt"
"io/fs"
"log/slog"
"net/http"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/green-ecolution/backend/pkg/plugin"
)
type ServerConfig struct {
port int
plugin plugin.Plugin
pluginFS fs.FS
version string
}
type Server struct {
cfg *ServerConfig
}
type ServerOption func(*ServerConfig)
func WithPort(port int) ServerOption {
return func(cfg *ServerConfig) {
cfg.port = port
}
}
func WithVersion(version string) ServerOption {
return func(cfg *ServerConfig) {
cfg.version = version
}
}
func WithPluginFS(pluginFS fs.FS) ServerOption {
return func(cfg *ServerConfig) {
cfg.pluginFS = pluginFS
}
}
func WithPlugin(plugin plugin.Plugin) ServerOption {
return func(cfg *ServerConfig) {
cfg.plugin = plugin
}
}
var defaultServerConfig = &ServerConfig{
port: 8080,
version: "develop",
}
func NewServer(opts ...ServerOption) *Server {
cfg := defaultServerConfig
for _, opt := range opts {
opt(cfg)
}
return &Server{
cfg: cfg,
}
}
func (s *Server) Run(ctx context.Context) error {
r := chi.NewRouter()
r.Get("/", s.handleHelloWorld)
r.Get("/info", s.handleGetInfo)
r.Post("/sync", s.handleExecSync)
r.Post("/reset", s.handleExecReset)
r.Get("/*", s.handleFileSystem)
server := http.Server{
Addr: fmt.Sprintf(":%d", s.cfg.port),
Handler: r,
}
go func() {
<-ctx.Done()
timeoutCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := server.Shutdown(timeoutCtx); err != nil {
slog.Error("failed to shutdown http server", "error", err)
}
}()
return server.ListenAndServe()
}
func (s *Server) handleHelloWorld(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("Hello World"))
}
type info struct {
SyncInterval string `json:"sync_interval"`
LastSync time.Time `json:"last_sync"`
PluginSlug string `json:"slug"`
Version string `json:"version"`
TotalManagedTrees int `json:"total_managed_trees"`
Description string `json:"description"`
}
func (s *Server) handleGetInfo(w http.ResponseWriter, _ *http.Request) {
cfg := ParseConfig()
infoResp := info{
SyncInterval: cfg.SyncInterval.String(),
LastSync: syncTrees.lastSync,
PluginSlug: cfg.PluginSlug,
Version: version,
TotalManagedTrees: syncTrees.managedTrees,
Description: description,
}
encode := json.NewEncoder(w)
encode.Encode(infoResp)
}
func (s *Server) handleExecSync(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if err := syncTrees.Sync(ctx); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (s *Server) handleExecReset(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if err := syncTrees.Reset(ctx); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (s *Server) handleFileSystem(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
fs := http.StripPrefix(pathPrefix, http.FileServerFS(s.cfg.pluginFS))
fs.ServeHTTP(w, r)
}