-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscollex.go
217 lines (188 loc) · 5.96 KB
/
scollex.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
// Copyright 2023 Tomas Machalek <[email protected]>
// 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"
"database/sql"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/czcorpus/cnc-gokit/cors"
"github.com/czcorpus/cnc-gokit/logging"
"github.com/czcorpus/cnc-gokit/uniresp"
"github.com/czcorpus/scollex/cnf"
"github.com/czcorpus/scollex/engine"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
var (
version string
buildDate string
gitCommit string
)
// VersionInfo provides a detailed information about the actual build
type VersionInfo struct {
Version string `json:"version"`
BuildDate string `json:"buildDate"`
GitCommit string `json:"gitCommit"`
}
func init() {
}
func runApiServer(
conf *cnf.Conf,
syscallChan chan os.Signal,
exitEvent chan os.Signal,
sqlDB *sql.DB,
) {
if !conf.Logging.Level.IsDebugMode() {
gin.SetMode(gin.ReleaseMode)
}
engine := gin.New()
engine.Use(gin.Recovery())
engine.Use(logging.GinMiddleware())
engine.Use(uniresp.AlwaysJSONContentType())
engine.Use(cors.CORSMiddleware(conf.CorsAllowedOrigins))
engine.NoMethod(uniresp.NoMethodHandler)
engine.NoRoute(uniresp.NotFoundHandler)
fcollActions := NewActions(&conf.Corpora, sqlDB)
engine.GET(
"/query/:corpusId/noun-modified-by", fcollActions.NounsModifiedBy)
engine.GET(
"/query/:corpusId/modifiers-of", fcollActions.ModifiersOf)
engine.GET(
"/query/:corpusId/verbs-subject", fcollActions.VerbsSubject)
engine.GET(
"/query/:corpusId/verbs-object", fcollActions.VerbsObject)
log.Info().Msgf("starting to listen at %s:%d", conf.ListenAddress, conf.ListenPort)
srv := &http.Server{
Handler: engine,
Addr: fmt.Sprintf("%s:%d", conf.ListenAddress, conf.ListenPort),
WriteTimeout: time.Duration(conf.ServerWriteTimeoutSecs) * time.Second,
ReadTimeout: time.Duration(conf.ServerReadTimeoutSecs) * time.Second,
}
go func() {
err := srv.ListenAndServe()
if err != nil {
log.Error().Err(err).Msg("")
}
syscallChan <- syscall.SIGTERM
}()
<-exitEvent
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := srv.Shutdown(ctx)
if err != nil {
log.Info().Err(err).Msg("Shutdown request error")
}
}
func main() {
version := VersionInfo{
Version: version,
BuildDate: buildDate,
GitCommit: gitCommit,
}
generalUsage := func() {
fmt.Fprintf(os.Stderr, "SCollEx - a Syntactic Collocations explorer\n\n")
fmt.Fprintf(os.Stderr, "Usage:\t%s [options] start [config.json]\n", filepath.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "\t%s [options] import [config.json] [corpus ID] [path to vertical file]\n", filepath.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "\t%s [options] test [config.json]\n", filepath.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "\t%s [options] version\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
startCmd := flag.NewFlagSet("start", flag.ExitOnError)
startCmd.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage:\t%s [options] start [config.json]\n", filepath.Base(os.Args[0]))
startCmd.PrintDefaults()
}
importCmd := flag.NewFlagSet("import", flag.ExitOnError)
forceOverwriteTbl := importCmd.Bool("f", false, "Drop target tables in case they already exist")
coOccSpan := importCmd.Int("colloc-flags-with-span", 2, "Defines window size for calculating coocurrences")
action := os.Args[1]
if action == "version" {
fmt.Printf("scollex %s\nbuild date: %s\nlast commit: %s\n", version.Version, version.BuildDate, version.GitCommit)
return
}
switch action {
case "start":
startCmd.Parse(os.Args[2:])
conf := cnf.LoadConfig(startCmd.Arg(0))
if action == "test" {
cnf.ValidateAndDefaults(conf)
log.Info().Msg("config OK")
return
} else {
logging.SetupLogging(conf.Logging)
}
log.Info().Msg("Starting SCollEx")
cnf.ValidateAndDefaults(conf)
syscallChan := make(chan os.Signal, 1)
signal.Notify(syscallChan, os.Interrupt)
signal.Notify(syscallChan, syscall.SIGTERM)
exitEvent := make(chan os.Signal)
go func() {
evt := <-syscallChan
exitEvent <- evt
close(exitEvent)
}()
sqlDB, err := engine.Open(conf.DB)
if err != nil {
log.Fatal().Err(err).Msg("failed to open database connection")
}
runApiServer(conf, syscallChan, exitEvent, sqlDB)
case "import":
importCmd.Parse(os.Args[2:])
conf := cnf.LoadConfig(importCmd.Arg(0))
sqlDB, err := engine.Open(conf.DB)
if err != nil {
log.Fatal().Err(err).Msg("failed to open database connection")
}
corpProps := conf.Corpora.GetCorpusProps(importCmd.Arg(1))
if corpProps == nil {
log.Fatal().Msgf("corpus `%s` not installed", importCmd.Arg(1))
return
}
cdb := engine.NewCollDatabase(sqlDB, importCmd.Arg(1))
err = cdb.InitializeDB(sqlDB, *forceOverwriteTbl)
if err != nil {
log.Fatal().Err(err).Msg("failed to initialize database tables")
}
log.Info().Msgf("Testing whether the table %s is ready", cdb.TableName())
err = cdb.TestTableReady()
if err != nil {
log.Fatal().
Err(err).
Str("dbHost", conf.DB.Host).
Int("dbPort", conf.DB.Port).
Str("dbName", conf.DB.Name).
Msg("...target db table NOT READY")
return
} else {
log.Info().Msg("... table READY")
}
err = engine.RunPg(importCmd.Arg(1), importCmd.Arg(2), *coOccSpan, &corpProps.Syntax, sqlDB)
if err != nil {
log.Fatal().Err(err).Msg("failed to process")
return
}
default:
generalUsage()
}
}