-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathmain.go
121 lines (104 loc) · 3.58 KB
/
main.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
// Copyright 2023 Ant Group Co., Ltd.
//
// 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 (
"flag"
"fmt"
"io"
"os"
"time"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/secretflow/scql/pkg/audit"
"github.com/secretflow/scql/pkg/executor"
"github.com/secretflow/scql/pkg/scdb/config"
"github.com/secretflow/scql/pkg/scdb/server"
"github.com/secretflow/scql/pkg/scdb/storage"
"github.com/secretflow/scql/pkg/util/logutil"
"gopkg.in/natefinch/lumberjack.v2"
)
const (
defaultConfigPath = "cmd/scdbserver/config.yml"
)
const (
LogFileName = "logs/scdbserver.log"
LogOptionMaxSizeInMegaBytes = 500
LogOptionMaxBackupsCount = 10
LogOptionMaxAgeInDays = 0
LogOptionCompress = false
)
var version = "scql version"
func main() {
confFile := flag.String("config", defaultConfigPath, "Path to scdb server configuration file")
showVersion := flag.Bool("version", false, "Print version information")
flag.Parse()
if *showVersion {
fmt.Println(version)
return
}
log.SetReportCaller(true)
log.SetFormatter(logutil.NewCustomMonitorFormatter("2006-01-02 15:04:05.123"))
rollingLogger := &lumberjack.Logger{
Filename: LogFileName,
MaxSize: LogOptionMaxSizeInMegaBytes, // megabytes
MaxBackups: LogOptionMaxBackupsCount,
MaxAge: LogOptionMaxAgeInDays, //days
Compress: LogOptionCompress,
}
mout := io.MultiWriter(os.Stdout, rollingLogger)
log.SetOutput(mout)
gin.SetMode(gin.ReleaseMode)
log.Infof("SCDB version: %s", version)
log.Infof("Starting to read config file: %s", *confFile)
cfg, err := config.NewConfig(*confFile)
if err != nil {
log.Fatalf("Failed to create config from %s: %v", *confFile, err)
}
// set log level if defined
if cfg.LogLevel != "" {
if lvl, err := log.ParseLevel(cfg.LogLevel); err == nil {
log.SetLevel(lvl)
}
}
if cfg.EnableAuditLogger {
if err := audit.InitAudit(&cfg.AuditConfig); err != nil {
log.Fatalf("Failed to init audit with error: %v", err)
}
}
log.Info("Starting to connect to database and do bootstrap if necessary...")
storage.InitPasswordValidation(cfg.PasswordCheck)
store, err := server.NewDbConnWithBootstrap(&cfg.Storage)
if err != nil {
log.Fatalf("Failed to connect to database and bootstrap it: %v", err)
}
engineClient := executor.NewEngineClient(cfg.Engine.ClientMode, cfg.Engine.ClientTimeout*time.Second, nil, cfg.Engine.ContentType, cfg.Engine.Protocol)
svr, err := server.NewServer(cfg, store, engineClient)
if err != nil {
log.Fatalf("Failed to create scdb server: %v", err)
}
if cfg.Protocol == "https" {
if cfg.TlsConfig.CertFile == "" || cfg.TlsConfig.KeyFile == "" {
log.Fatalf("Could't start https service without cert file")
}
log.Info("Starting to serve request with https...")
if err := svr.ListenAndServeTLS(cfg.TlsConfig.CertFile, cfg.TlsConfig.KeyFile); err != nil {
log.Fatalf("Something bad happens to server: %v", err)
}
} else {
log.Info("Starting to serve request with http...")
if err := svr.ListenAndServe(); err != nil {
log.Fatalf("Something bad happens to server: %v", err)
}
}
}