-
Notifications
You must be signed in to change notification settings - Fork 1
/
mquery.go
226 lines (198 loc) · 6.18 KB
/
mquery.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
// Copyright 2023 Tomas Machalek <tomas.machalek@gmail.com>
// Copyright 2023 Martin Zimandl <martin.zimandl@gmail.com>
// Copyright 2023 Institute of the Czech National Corpus,
// Faculty of Arts, Charles University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"encoding/gob"
"flag"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/czcorpus/cnc-gokit/collections"
"github.com/czcorpus/cnc-gokit/logging"
"github.com/czcorpus/mquery-common/concordance"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"mquery/cnf"
"mquery/docs"
"mquery/general"
"mquery/merror"
"mquery/rdb"
"mquery/rdb/results"
)
const (
redisConnectionTestTimeout = 120 * time.Second
)
var (
version string
buildDate string
gitCommit string
)
func init() {
gob.Register(rdb.CorpusInfoArgs{})
gob.Register(rdb.FreqDistribArgs{})
gob.Register(rdb.CollocationsArgs{})
gob.Register(rdb.TermFrequencyArgs{})
gob.Register(rdb.ConcordanceArgs{})
gob.Register(rdb.CalcCollFreqDataArgs{})
gob.Register(rdb.TextTypeNormsArgs{})
gob.Register(results.CollFreqData{})
gob.Register(results.Collocations{})
gob.Register(results.ConcSize{})
gob.Register(results.Concordance{})
gob.Register(results.CorpusInfo{})
gob.Register(results.FreqDistrib{})
gob.Register(results.TextTypeNorms{})
gob.Register(&concordance.Token{})
gob.Register(&concordance.Struct{})
gob.Register(&concordance.CloseStruct{})
gob.Register(merror.InputError{})
gob.Register(merror.InternalError{})
gob.Register(merror.RecoveredError{})
gob.Register(merror.TimeoutError{})
gob.Register(rdb.ErrorResult{})
}
type service interface {
Start(ctx context.Context)
Stop(ctx context.Context) error
}
func getEnv(name string) string {
for _, p := range os.Environ() {
items := strings.Split(p, "=")
if len(items) == 2 && items[0] == name {
return items[1]
}
}
return ""
}
func getRequestOrigin(ctx *gin.Context) string {
currOrigin, ok := ctx.Request.Header["Origin"]
if ok {
return currOrigin[0]
}
return ""
}
func additionalLogEvents() gin.HandlerFunc {
return func(ctx *gin.Context) {
logging.AddLogEvent(ctx, "userAgent", ctx.Request.UserAgent())
logging.AddLogEvent(ctx, "corpusId", ctx.Param("corpusId"))
ctx.Next()
}
}
func CORSMiddleware(conf *cnf.Conf) gin.HandlerFunc {
return func(ctx *gin.Context) {
if strings.HasSuffix(ctx.Request.URL.Path, "/openapi") {
ctx.Header("Access-Control-Allow-Origin", "*")
ctx.Header("Access-Control-Allow-Methods", "GET")
ctx.Header("Access-Control-Allow-Headers", "Content-Type")
} else {
var allowedOrigin string
currOrigin := getRequestOrigin(ctx)
for _, origin := range conf.CorsAllowedOrigins {
if currOrigin == origin {
allowedOrigin = origin
break
}
}
if allowedOrigin != "" {
ctx.Writer.Header().Set("Access-Control-Allow-Origin", allowedOrigin)
ctx.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
ctx.Writer.Header().Set(
"Access-Control-Allow-Headers",
"Content-Type, Content-Length, Accept-Encoding, Authorization, Accept, Origin, Cache-Control, X-Requested-With",
)
ctx.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
}
if ctx.Request.Method == "OPTIONS" {
ctx.AbortWithStatus(204)
return
}
}
ctx.Next()
}
}
func AuthRequired(conf *cnf.Conf) gin.HandlerFunc {
return func(ctx *gin.Context) {
if len(conf.AuthHeaderName) > 0 && !collections.SliceContains(conf.AuthTokens, ctx.GetHeader(conf.AuthHeaderName)) {
ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
}
ctx.Next()
}
}
func cleanVersionInfo(v string) string {
return strings.TrimLeft(strings.Trim(v, "'"), "v")
}
// @title MQuery - query and analyze corpus data
// @description Retrieves concordances, frequency information and collocations from language corpora
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /
// @externalDocs.description OpenAPI
// @externalDocs.url https://swagger.io/resources/open-api/
func main() {
version := general.VersionInfo{
Version: cleanVersionInfo(version),
BuildDate: cleanVersionInfo(buildDate),
GitCommit: cleanVersionInfo(gitCommit),
}
docs.SwaggerInfo.Version = version.Version
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "MQUERY - A specialized corpus querying server\n\n")
fmt.Fprintf(os.Stderr, "Usage:\n\t%s [options] server [config.json]\n\t", filepath.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "Usage:\n\t%s [options] worker [config.json]\n\t", filepath.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "%s [options] version\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
flag.Parse()
action := flag.Arg(0)
if action == "version" {
fmt.Printf("mquery %s\nbuild date: %s\nlast commit: %s\n", version.Version, version.BuildDate, version.GitCommit)
return
}
conf := cnf.LoadConfig(flag.Arg(1))
if action == "worker" {
if conf.Logging.Path != "" {
conf.Logging.Path = filepath.Join(filepath.Dir(conf.Logging.Path), "worker.log")
}
logging.SetupLogging(conf.Logging)
log.Logger = log.Logger.With().Str("worker", getWorkerID()).Logger()
} else if action == "test" {
cnf.ValidateAndDefaults(conf)
log.Info().Msg("config OK")
return
} else {
logging.SetupLogging(conf.Logging)
}
if err := conf.LoadSubconfigs(); err != nil {
log.Fatal().Err(err).Msg("Failed to load subconfig(s)")
return
}
log.Info().Msg("Starting MQuery")
cnf.ValidateAndDefaults(conf)
switch action {
case "server":
runApiServer(conf)
case "worker":
runWorker(conf)
default:
log.Fatal().Msgf("Unknown action %s", action)
}
}