forked from b3log/pipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfs.go
191 lines (159 loc) · 5.87 KB
/
confs.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
// Pipe - A small and beautiful blogging platform written in golang.
// Copyright (C) 2017-2019, b3log.org & hacpai.com
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package util defines variety of utilities.
package model
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/b3log/pipe/log"
"github.com/jinzhu/gorm"
"github.com/b3log/pipe/util"
)
// Logger
var logger = log.NewLogger(os.Stdout)
// Version of Pipe.
const Version = "1.8.6"
// Conf of Pipe.
var Conf *Configuration
// UserAgent represents HTTP client user agent.
var UserAgent = "Pipe/" + Version + "; +https://github.com/b3log/pipe"
// Models represents all models..
var Models = []interface{}{
&User{}, &Article{}, &Comment{}, &Navigation{}, &Tag{},
&Category{}, &Archive{}, &Setting{}, &Correlation{},
}
// Table prefix.
const tablePrefix = "b3_pipe_"
// ZeroPushTime represents zero push time.
var ZeroPushTime, _ = time.Parse("2006-01-02 15:04:05", "2006-01-02 15:04:05")
// Configuration (pipe.json).
type Configuration struct {
Server string // server scheme, host and port
StaticServer string // static resources server scheme, host and port
StaticResourceVersion string // version of static resources
OpenRegister bool // whether open register
LogLevel string // logging level: trace/debug/info/warn/error/fatal
ShowSQL bool // whether print sql in log
SessionSecret string // HTTP session secret
SessionMaxAge int // HTTP session max age (in seciond)
RuntimeMode string // runtime mode (dev/prod)
SQLite string // SQLite database file path
MySQL string // MySQL connection URL
StaticRoot string // static resources file root path
Port string // listen port
AxiosBaseURL string // axio base URL
MockServer string // mock server
}
// LoadConf loads the configurations. Command-line arguments will override configuration file.
func LoadConf() {
version := flag.Bool("version", false, "prints current pipe version")
confPath := flag.String("conf", "pipe.json", "path of pipe.json")
confServer := flag.String("server", "", "this will override Conf.Server if specified")
confStaticServer := flag.String("static_server", "", "this will override Conf.StaticServer if specified")
confStaticResourceVer := flag.String("static_resource_ver", "", "this will override Conf.StaticResourceVersion if specified")
confOpenRegister := flag.Bool("open_register", true, "this will override Conf.OpenRegister if specified")
confLogLevel := flag.String("log_level", "", "this will override Conf.LogLevel if specified")
confShowSQL := flag.Bool("show_sql", false, "this will override Conf.ShowSQL if specified")
confRuntimeMode := flag.String("runtime_mode", "", "this will override Conf.RuntimeMode if specified")
confSQLite := flag.String("sqlite", "", "this will override Conf.SQLite if specified")
confMySQL := flag.String("mysql", "", "this will override Conf.MySQL if specified")
confStaticRoot := flag.String("static_root", "", "this will override Conf.StaticRoot if specified")
confPort := flag.String("port", "", "this will override Conf.Port if specified")
s2m := flag.Bool("s2m", false, "dumps SQLite data to MySQL SQL script file")
flag.Parse()
if *version {
fmt.Println(Version)
os.Exit(0)
}
bytes, err := ioutil.ReadFile(*confPath)
if nil != err {
logger.Fatal("loads configuration file [" + *confPath + "] failed: " + err.Error())
}
Conf = &Configuration{}
if err = json.Unmarshal(bytes, Conf); nil != err {
logger.Fatal("parses [pipe.json] failed: ", err)
}
log.SetLevel(Conf.LogLevel)
if "" != *confLogLevel {
Conf.LogLevel = *confLogLevel
log.SetLevel(*confLogLevel)
}
if !*confOpenRegister {
Conf.OpenRegister = false
}
if *confShowSQL {
Conf.ShowSQL = true
}
home, err := util.UserHome()
if nil != err {
logger.Fatal("can't find user home directory: " + err.Error())
}
logger.Debugf("${home} [%s]", home)
if "" != *confRuntimeMode {
Conf.RuntimeMode = *confRuntimeMode
}
if "" != *confServer {
Conf.Server = *confServer
}
if "" != *confStaticServer {
Conf.StaticServer = *confStaticServer
}
if "" == Conf.StaticServer {
Conf.StaticServer = Conf.Server
}
time := strconv.FormatInt(time.Now().UnixNano(), 10)
logger.Debugf("${time} [%s]", time)
Conf.StaticResourceVersion = strings.Replace(Conf.StaticResourceVersion, "${time}", time, 1)
if "" != *confStaticResourceVer {
Conf.StaticResourceVersion = *confStaticResourceVer
}
Conf.SQLite = strings.Replace(Conf.SQLite, "${home}", home, 1)
if "" != *confSQLite {
Conf.SQLite = *confSQLite
}
if "" != *confMySQL {
Conf.MySQL = *confMySQL
}
Conf.StaticRoot = ""
if "" != *confStaticRoot {
Conf.StaticRoot = *confStaticRoot
Conf.StaticRoot = filepath.Dir(Conf.StaticRoot)
}
if "" != *confPort {
Conf.Port = *confPort
}
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
return tablePrefix + defaultTableName
}
if *s2m {
if "" == Conf.SQLite {
logger.Fatal("please specify -sqlite")
}
if "" == Conf.MySQL {
logger.Fatal("please specify -mysql")
}
sqlite2MySQL(Conf.SQLite, Conf.MySQL)
os.Exit(0)
}
logger.Debugf("configurations [%#v]", Conf)
}