-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
140 lines (112 loc) · 3.09 KB
/
server.go
File metadata and controls
140 lines (112 loc) · 3.09 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
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/green-ecolution-backend/pkg/plugin"
)
type ServerConfig struct {
port int
client *GreenEcolutionClient
plugin plugin.Plugin
pluginFS fs.FS
version string
}
type Server struct {
cfg *ServerConfig
}
type ServerOption func(*ServerConfig)
func WithPort(port int) ServerOption {
return func(sc *ServerConfig) { sc.port = port }
}
func WithClient(client *GreenEcolutionClient) ServerOption {
return func(sc *ServerConfig) { sc.client = client }
}
func WithPlugin(plugin plugin.Plugin) ServerOption {
return func(sc *ServerConfig) { sc.plugin = plugin }
}
func WithPluginFS(fs fs.FS) ServerOption {
return func(sc *ServerConfig) { sc.pluginFS = fs }
}
func WithVersion(version string) ServerOption {
return func(sc *ServerConfig) { sc.version = version }
}
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("/*", s.handleFileSystem)
r.Post("/upload", s.handleCsvUpload)
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"))
}
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)
}
func (s *Server) handleCsvUpload(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
defer r.Body.Close()
r.ParseForm()
csvFile, fileHeader, err := r.FormFile("file")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("failed to read file: error: %s", err)))
return
}
csvConverter := NewCSVConverter(fileHeader, csvFile)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("unsupported file: error: %s", err)))
return
}
convertedTrees, err := csvConverter.Convert(ctx)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("failed to convert csv file: error: %s", err)))
return
}
syncTrees := NewSyncTrees(convertedTrees, s.cfg.client)
importedTrees, err := syncTrees.Sync(ctx)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("failed to sync trees to green ecolution backend: error: %s", err)))
return
}
encode := json.NewEncoder(w)
encode.Encode(importedTrees)
}