-
Notifications
You must be signed in to change notification settings - Fork 57
/
lsdserver.go
133 lines (109 loc) · 3.24 KB
/
lsdserver.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
// Copyright 2017 European Digital Reading Lab. All rights reserved.
// Licensed to the Readium Foundation under one or more contributor license agreements.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
package main
import (
"database/sql"
"fmt"
"log"
"os"
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
auth "github.com/abbot/go-http-auth"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
_ "github.com/microsoft/go-mssqldb"
"github.com/readium/readium-lcp-server/config"
licensestatuses "github.com/readium/readium-lcp-server/license_statuses"
"github.com/readium/readium-lcp-server/logging"
lsdserver "github.com/readium/readium-lcp-server/lsdserver/server"
"github.com/readium/readium-lcp-server/transactions"
)
func main() {
var config_file string
var readonly bool = false
var err error
if config_file = os.Getenv("READIUM_LSDSERVER_CONFIG"); config_file == "" {
config_file = "config.yaml"
}
config.ReadConfig(config_file)
log.Println("Config from " + config_file)
readonly = config.Config.LsdServer.ReadOnly
err = config.SetPublicUrls()
if err != nil {
panic(err)
}
driver, cnxn := config.GetDatabase(config.Config.LsdServer.Database)
log.Println("Database driver " + driver)
db, err := sql.Open(driver, cnxn)
if err != nil {
panic(err)
}
if driver == "sqlite3" && !strings.Contains(cnxn, "_journal") {
_, err = db.Exec("PRAGMA journal_mode = WAL")
if err != nil {
panic(err)
}
}
hist, err := licensestatuses.Open(db)
if err != nil {
panic(err)
}
trns, err := transactions.Open(db)
if err != nil {
panic(err)
}
authFile := config.Config.LsdServer.AuthFile
if authFile == "" {
panic("Must have passwords file")
}
_, err = os.Stat(authFile)
if err != nil {
panic(err)
}
htpasswd := auth.HtpasswdFileProvider(authFile)
authenticator := auth.NewBasicAuthenticator("Basic Realm", htpasswd)
// the server will behave strangely, to test the resilience of LCP compliant apps
goofyMode := config.Config.GoofyMode
// if the logging key is set, logs will be sent to a file and/or Slack channel for test purposes
err = logging.Init(config.Config.Logging)
if err != nil {
panic(err)
}
HandleSignals()
parsedPort := strconv.Itoa(config.Config.LsdServer.Port)
s := lsdserver.New(":"+parsedPort, readonly, goofyMode, &hist, &trns, authenticator)
if readonly {
log.Println("License status server running in readonly mode on port " + parsedPort)
} else {
log.Println("License status server running on port " + parsedPort)
}
log.Println("Public base URL=" + config.Config.LsdServer.PublicBaseUrl)
if err := s.ListenAndServe(); err != nil {
log.Println("Error " + err.Error())
}
}
func HandleSignals() {
sigChan := make(chan os.Signal)
go func() {
stacktrace := make([]byte, 1<<20)
for sig := range sigChan {
switch sig {
case syscall.SIGQUIT:
length := runtime.Stack(stacktrace, true)
fmt.Println(string(stacktrace[:length]))
case syscall.SIGINT:
fallthrough
case syscall.SIGTERM:
fmt.Println("Shutting down...")
os.Exit(0)
}
}
}()
signal.Notify(sigChan, syscall.SIGQUIT, syscall.SIGINT, syscall.SIGTERM)
}