-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
159 lines (132 loc) · 4.92 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
package main
import (
"crypto/md5"
"encoding/base64"
"fmt"
"net/http"
"strings"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func startDaemon(app *App, cfg appConfig) {
initLanguageChannels()
app.initChannels()
e := initHandlers(app, cfg.EnableInternalApis)
app.log.Printf("🚀 starting varnamd\nListening on %s", cfg.Address)
if cfg.EnableSSL {
if err := e.StartTLS(cfg.Address, cfg.CertFilePath, cfg.KeyFilePath); err != nil {
app.log.Fatal(err)
}
} else {
if err := e.Start(cfg.Address); err != nil {
app.log.Fatal(err)
}
}
}
func initHandlers(app *App, enableInternalApis bool) *echo.Echo {
e := echo.New()
e.GET("/tl/:langCode/:word", handleTransliteration)
e.GET("/rtl/:langCode/:word", handleReverseTransliteration)
e.GET("/atl/:langCode/:word", handleAdvancedTransliteration)
// e.GET("/meta/:langCode:", handleMetadata)
// e.GET("/download/:langCode/:downloadStart", handleDownload)
e.GET("/languages", handleLanguages)
e.GET("/languages/:langCode/download", handleLanguageDownload)
e.GET("/packs", handlePacks)
e.GET("/packs/:langCode", handlePacks)
e.GET("/packs/:langCode/:packIdentifier", handlePackInfo)
e.GET("/packs/:langCode/:packIdentifier/:packPageIdentifier", handlePackPageInfo)
e.GET("/packs/:langCode/:packIdentifier/:packPageIdentifier/download", handlePacksDownload)
e.GET("/status", handleStatus)
e.GET("/schemes/:schemeID", handleSchemeInfo)
e.GET("/schemes/:schemeID/definitions", handleSchemeDefinitions)
e.GET("/schemes/:schemeID/definitions/:letter", handleSchemeLetterDefinitions)
e.GET("/", handleIndex)
e.GET("/*", echo.WrapHandler(app.fs.FileServer()))
if enableInternalApis {
e.POST("/sync/download/:langCode/enable", handleEnableDownload)
e.POST("/sync/download/:langCode/disable", handleDisableDownload)
e.POST("/learn", authUser(handleLearn))
e.POST("/learn/upload/:langCode", authUser(handleLearnFileUpload))
e.POST("/train/:langCode", authUser(handleTrain))
e.POST("/train/bulk/:langCode", authUser(handleTrainBulk))
e.POST("/delete", authUser(handleDelete))
e.POST("/packs/download", handlePackDownloadRequest)
}
e.Use(middleware.Recover())
e.Use(middleware.RequestLoggerWithConfig(middleware.RequestLoggerConfig{
LogStatus: true,
LogURI: true,
LogLatency: true,
LogRemoteIP: true,
LogReferer: true,
LogUserAgent: true,
LogError: true,
BeforeNextFunc: func(c echo.Context) {
c.Set("customValueFromContext", 42)
},
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
remoteIpMasked := md5.Sum([]byte(c.RealIP()))
fmt.Printf(
"[%v] status: %v, latency_human: %s, referer: %v, remote_ip: %x, error: %v, user_agent: %s, uri: %v\n",
time.Now().Format(time.RFC3339Nano), v.Status, v.Latency.String(), v.Referer, remoteIpMasked, v.Error, v.UserAgent, v.URI,
)
return nil
},
}))
// Custom middleware to set sigleton to app's context.
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Set("app", app)
return next(c)
}
})
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodOptions},
}))
e.Use(middleware.SecureWithConfig(middleware.SecureConfig{
XSSProtection: "",
ContentTypeNosniff: "",
XFrameOptions: "",
HSTSMaxAge: 3600,
// ContentSecurityPolicy: "default-src 'self'",
}))
// rate limit requests per second (prevent handler exhaustion)
e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(20)))
return e
}
// authUser as a separate method to apply this middleware only for selected endpoints.
func authUser(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
var app = c.Get("app").(*App)
if authEnabled {
auth := strings.Split(c.Request().Header.Get("Authorization"), " ")
if len(auth) < 2 {
app.log.Printf("authorization header not found")
return echo.NewHTTPError(http.StatusUnauthorized, "authorization header not found")
}
if strings.ToLower(auth[0]) != "basic" {
app.log.Printf("authorization header not found")
return echo.NewHTTPError(http.StatusUnauthorized, "authorization details not found")
}
creds, err := base64.StdEncoding.DecodeString(auth[1])
if err != nil {
app.log.Printf("error decoding auth headers, error: %s", err.Error())
return echo.NewHTTPError(http.StatusUnauthorized, "authorization failed, failed to decode authstring")
}
authCreds := strings.Split(string(creds), ":")
user, ok := users[strings.TrimSpace(authCreds[0])]
if !ok {
app.log.Printf("user not found")
return echo.NewHTTPError(http.StatusUnauthorized, "authorization failed, user not found")
}
if user["password"] != strings.TrimSpace(authCreds[1]) {
app.log.Printf("password mismatch")
return echo.NewHTTPError(http.StatusUnauthorized, "authorization failed, password mismatch")
}
}
return next(c)
}
}