-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathserver.go
293 lines (248 loc) · 7.51 KB
/
server.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Package server represents the webserver that powers S&D.
package server
import (
"context"
"errors"
"fmt"
"io/ioutil"
"mime"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/BigJk/snd/rpc/bind"
"github.com/BigJk/snd/database"
"github.com/labstack/echo/v4/middleware"
"github.com/patrickmn/go-cache"
"gopkg.in/olahol/melody.v1"
"github.com/BigJk/nra"
"github.com/BigJk/snd"
"github.com/BigJk/snd/log"
"github.com/BigJk/snd/printing"
"github.com/BigJk/snd/rpc"
"github.com/labstack/echo/v4"
)
// To fix issue on Windows with reporting .js files as text/plain
// we force the content type for .js to 'application/javascript'.
//
// https://github.com/labstack/echo/issues/1038
func init() {
_ = mime.AddExtensionType(".js", "application/javascript")
http.DefaultClient.Timeout = time.Second * 2
}
type proxyCacheEntry struct {
ContentType string
Data []byte
}
// Option represent an configuration option for the server.
type Option func(s *Server) error
// Server represents an instance of the S&D server.
type Server struct {
sync.RWMutex
debug bool
db database.Database
e *echo.Echo
m *melody.Melody
cache *cache.Cache
printers printing.PossiblePrinter
additionalRpc map[string]interface{}
}
// New creates a new instance of the S&D server.
func New(db database.Database, options ...Option) (*Server, error) {
s := &Server{
db: db,
e: echo.New(),
m: melody.New(),
cache: cache.New(time.Minute*10, time.Minute),
printers: map[string]printing.Printer{},
additionalRpc: map[string]interface{}{},
}
for i := range options {
if err := options[i](s); err != nil {
return nil, err
}
}
return s, nil
}
// WithDebug sets the debug state of the Server.
func WithDebug(value bool) Option {
return func(s *Server) error {
s.debug = value
return nil
}
}
// WithPrinter registers a printer in the Server.
func WithPrinter(printer printing.Printer) Option {
return func(s *Server) error {
s.printers[printer.Name()] = printer
return nil
}
}
// WithAdditionalRPC adds an RPC function to the Server.
func WithAdditionalRPC(fnName string, fn interface{}) Option {
return func(s *Server) error {
s.additionalRpc[fnName] = fn
return nil
}
}
// Close closes the server and all its connections.
func (s *Server) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
errServer := s.e.Shutdown(ctx)
errDB := s.db.Close()
cancel()
if errServer != nil {
return errServer
}
return errDB
}
// Start starts the server with the given bind address.
//
// Examples:
// - ":7232" will accept all connections on port 7232
// - "127.0.0.1:7232" will only accept local connections on port 7232
func (s *Server) Start(bindAddr string) error {
// Create default settings if not existing
if _, err := s.db.GetSettings(); err != nil {
if err := s.db.SaveSettings(snd.Settings{
PrinterWidth: 384,
PrinterType: "Preview Printing",
PrinterEndpoint: "window",
Stylesheets: []string{},
SpellcheckerLanguages: []string{"en-US"},
AIEnabled: false,
AIAlwaysAllow: false,
AIProvider: "OpenRouter.ai",
AIMaxTokens: 4000,
AIContextWindow: 6000,
}); err != nil {
return err
}
}
// Register rpc routes
api := s.e.Group("/api")
extern := api.Group("/extern")
rpc.RegisterVersion(api)
rpc.RegisterKeyValue(api, s.db)
rpc.RegisterImageUtilities(api)
rpc.RegisterSettings(api, s.db)
rpc.RegisterTemplate(api, extern, s.db)
rpc.RegisterGenerator(api, extern, s.db)
rpc.RegisterEntry(api, s.db)
rpc.RegisterSources(api, s.db)
rpc.RegisterPrint(api, extern, s.db, s.printers)
rpc.RegisterSync(api, s.m, s.db)
rpc.RegisterGit(api, s.db)
rpc.RegisterCloud(api, s.db)
rpc.RegisterAI(api, s.db)
rpc.RegisterFileBrowser(api)
// Expose function list
api.GET("/functions", func(c echo.Context) error {
funcs := bind.Functions()
resp := make(map[string]any)
addr := bindAddr
if addr[0] == ':' {
addr = "127.0.0.1" + addr
}
for _, f := range funcs {
resp[f.Name] = map[string]any{
"route": fmt.Sprintf("https://%s/api/%s", addr, f.Name),
"args": f.Args,
"method": "POST",
}
}
return c.JSONPretty(http.StatusOK, resp, " ")
})
// Register additional routes
for k, v := range s.additionalRpc {
api.POST(fmt.Sprintf("/%s", k), echo.WrapHandler(nra.MustBind(v)))
}
// Makes it possible to check in frontend if an
// additional function has been registered.
api.POST("/hasExt", echo.WrapHandler(nra.MustBind(func(name string) error {
if _, ok := s.additionalRpc[name]; ok {
return nil
}
return errors.New("function not available")
})))
// Register proxy route so that the iframes that are used
// in the frontend can proxy images and other data that they
// otherwise couldn't access because of CORB
s.e.GET("/proxy/*", func(c echo.Context) error {
reqUrl := c.Request().RequestURI[len("/index/"):]
if !strings.HasPrefix(reqUrl, "http") {
return c.NoContent(http.StatusBadRequest)
}
hit, ok := s.cache.Get(reqUrl)
if ok {
log.Info("proxy url from cache", log.WithValue("url", reqUrl))
entry := hit.(*proxyCacheEntry)
return c.Blob(http.StatusOK, entry.ContentType, entry.Data)
}
log.Info("proxy url", log.WithValue("url", reqUrl))
resp, err := http.Get(reqUrl)
if err != nil {
return c.NoContent(http.StatusBadRequest)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return c.NoContent(http.StatusBadRequest)
}
if resp.StatusCode == http.StatusOK {
s.cache.SetDefault(reqUrl, &proxyCacheEntry{
ContentType: resp.Header.Get("Content-Type"),
Data: data,
})
}
return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data)
})
// Same as proxy route but without caching. Can be used for
// dynamic API requests.
s.e.GET("/fetch/*", func(c echo.Context) error {
reqUrl := c.Request().RequestURI[len("/fetch/"):]
if !strings.HasPrefix(reqUrl, "http") {
return c.NoContent(http.StatusBadRequest)
}
log.Info("proxy url fetch", log.WithValue("url", reqUrl))
resp, err := http.Get(reqUrl)
if err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return c.JSON(http.StatusBadRequest, err.Error())
}
return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data)
})
api.GET("/ws", func(c echo.Context) error {
return s.m.HandleRequest(c.Response().Writer, c.Request())
})
// Make frontend and static directory public
if s.debug { // If debug is enabled pass frontend requests to vite dev server.
viteUrl, err := url.Parse("http://127.0.0.1:3000")
if err != nil {
return err
}
s.e.Use(middleware.ProxyWithConfig(middleware.ProxyConfig{Skipper: func(c echo.Context) bool {
return strings.HasPrefix(c.Request().URL.Path, "/api") ||
strings.HasPrefix(c.Request().URL.Path, "/proxy") ||
strings.HasPrefix(c.Request().URL.Path, "/fetch") ||
strings.HasPrefix(c.Request().URL.Path, "/static")
}, Balancer: middleware.NewRoundRobinBalancer([]*middleware.ProxyTarget{{URL: viteUrl}})}))
} else {
s.e.Static("/", "./frontend/dist")
}
s.e.Static("/static", "./static")
s.e.Use(middleware.CORS())
s.e.HideBanner = true
s.e.HidePort = true
// Save all logs
log.AddHook(func(e log.Entry) {
_ = s.db.AddLog(e)
})
log.Info("Server started", log.WithValue("bind", bindAddr))
return s.e.Start(bindAddr)
}