Finish refine the config things.
This commit is contained in:
parent
8aa0d11cbb
commit
a4f28cca30
@ -14,14 +14,14 @@ const (
|
|||||||
type Config interface {
|
type Config interface {
|
||||||
|
|
||||||
//是否已经安装
|
//是否已经安装
|
||||||
IsInstalled() bool
|
Installed() bool
|
||||||
//启动端口
|
//启动端口
|
||||||
GetServerPort() int
|
ServerPort() int
|
||||||
//获取mysql链接
|
//获取mysql链接
|
||||||
GetMysqlUrl() string
|
MysqlUrl() string
|
||||||
|
|
||||||
//文件存放路径
|
//文件存放路径
|
||||||
GetMatterPath() string
|
MatterPath() string
|
||||||
//完成安装过程,主要是要将配置写入到文件中
|
//完成安装过程,主要是要将配置写入到文件中
|
||||||
FinishInstall(mysqlPort int, mysqlHost string, mysqlSchema string, mysqlUsername string, mysqlPassword string)
|
FinishInstall(mysqlPort int, mysqlHost string, mysqlSchema string, mysqlUsername string, mysqlPassword string)
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ func (this *FootprintService) Trace(writer http.ResponseWriter, request *http.Re
|
|||||||
}
|
}
|
||||||
|
|
||||||
//有可能DB尚且没有配置 直接打印出内容,并且退出
|
//有可能DB尚且没有配置 直接打印出内容,并且退出
|
||||||
if core.CONFIG.IsInstalled() {
|
if core.CONFIG.Installed() {
|
||||||
user := this.findUser(writer, request)
|
user := this.findUser(writer, request)
|
||||||
userUuid := ""
|
userUuid := ""
|
||||||
if user != nil {
|
if user != nil {
|
||||||
|
@ -366,7 +366,7 @@ func (this *MatterDao) Cleanup() {
|
|||||||
db := core.CONTEXT.GetDB().Where("uuid is not null").Delete(Matter{})
|
db := core.CONTEXT.GetDB().Where("uuid is not null").Delete(Matter{})
|
||||||
this.PanicError(db.Error)
|
this.PanicError(db.Error)
|
||||||
|
|
||||||
err := os.RemoveAll(core.CONFIG.GetMatterPath())
|
err := os.RemoveAll(core.CONFIG.MatterPath())
|
||||||
this.PanicError(err)
|
this.PanicError(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,7 @@ func NewRootMatter(user *User) *Matter {
|
|||||||
//获取到用户文件的根目录。
|
//获取到用户文件的根目录。
|
||||||
func GetUserFileRootDir(username string) (rootDirPath string) {
|
func GetUserFileRootDir(username string) (rootDirPath string) {
|
||||||
|
|
||||||
rootDirPath = fmt.Sprintf("%s/%s/%s", core.CONFIG.GetMatterPath(), username, MATTER_ROOT)
|
rootDirPath = fmt.Sprintf("%s/%s/%s", core.CONFIG.MatterPath(), username, MATTER_ROOT)
|
||||||
|
|
||||||
return rootDirPath
|
return rootDirPath
|
||||||
}
|
}
|
||||||
@ -76,7 +76,7 @@ func GetUserFileRootDir(username string) (rootDirPath string) {
|
|||||||
//获取到用户缓存的根目录。
|
//获取到用户缓存的根目录。
|
||||||
func GetUserCacheRootDir(username string) (rootDirPath string) {
|
func GetUserCacheRootDir(username string) (rootDirPath string) {
|
||||||
|
|
||||||
rootDirPath = fmt.Sprintf("%s/%s/%s", core.CONFIG.GetMatterPath(), username, MATTER_CACHE)
|
rootDirPath = fmt.Sprintf("%s/%s/%s", core.CONFIG.MatterPath(), username, MATTER_CACHE)
|
||||||
|
|
||||||
return rootDirPath
|
return rootDirPath
|
||||||
}
|
}
|
||||||
|
@ -20,15 +20,15 @@ flush privileges;
|
|||||||
//依赖外部定义的变量。
|
//依赖外部定义的变量。
|
||||||
type TankConfig struct {
|
type TankConfig struct {
|
||||||
//默认监听端口号
|
//默认监听端口号
|
||||||
ServerPort int
|
serverPort int
|
||||||
//网站是否已经完成安装
|
//网站是否已经完成安装
|
||||||
Installed bool
|
installed bool
|
||||||
//上传的文件路径,要求不以/结尾。如果没有指定,默认在根目录下的matter文件夹中。eg: /var/www/matter
|
//上传的文件路径,要求不以/结尾。如果没有指定,默认在根目录下的matter文件夹中。eg: /var/www/matter
|
||||||
MatterPath string
|
matterPath string
|
||||||
//数据库连接信息。
|
//数据库连接信息。
|
||||||
MysqlUrl string
|
mysqlUrl string
|
||||||
//配置文件中的项
|
//配置文件中的项
|
||||||
Item *ConfigItem
|
item *ConfigItem
|
||||||
}
|
}
|
||||||
|
|
||||||
//和tank.json文件中的键值一一对应。
|
//和tank.json文件中的键值一一对应。
|
||||||
@ -108,7 +108,7 @@ func (this *TankConfig) Init() {
|
|||||||
}, nil)
|
}, nil)
|
||||||
|
|
||||||
//默认从6010端口启动
|
//默认从6010端口启动
|
||||||
this.ServerPort = 6010
|
this.serverPort = 6010
|
||||||
|
|
||||||
this.ReadFromConfigFile()
|
this.ReadFromConfigFile()
|
||||||
|
|
||||||
@ -122,64 +122,64 @@ func (this *TankConfig) ReadFromConfigFile() {
|
|||||||
content, err := ioutil.ReadFile(filePath)
|
content, err := ioutil.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.LOGGER.Warn("无法找到配置文件:%s 即将进入安装过程!", filePath)
|
core.LOGGER.Warn("无法找到配置文件:%s 即将进入安装过程!", filePath)
|
||||||
this.Installed = false
|
this.installed = false
|
||||||
} else {
|
} else {
|
||||||
this.Item = &ConfigItem{}
|
this.item = &ConfigItem{}
|
||||||
core.LOGGER.Warn("读取配置文件:%s", filePath)
|
core.LOGGER.Warn("读取配置文件:%s", filePath)
|
||||||
err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(content, this.Item)
|
err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(content, this.item)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.LOGGER.Error("配置文件格式错误! 即将进入安装过程!")
|
core.LOGGER.Error("配置文件格式错误! 即将进入安装过程!")
|
||||||
this.Installed = false
|
this.installed = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//验证项是否齐全
|
//验证项是否齐全
|
||||||
itemValidate := this.Item.validate()
|
itemValidate := this.item.validate()
|
||||||
if !itemValidate {
|
if !itemValidate {
|
||||||
core.LOGGER.Error("配置文件信息不齐全! 即将进入安装过程!")
|
core.LOGGER.Error("配置文件信息不齐全! 即将进入安装过程!")
|
||||||
this.Installed = false
|
this.installed = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//使用配置项中的文件路径
|
//使用配置项中的文件路径
|
||||||
if this.Item.MatterPath == "" {
|
if this.item.MatterPath == "" {
|
||||||
this.MatterPath = util.GetHomePath() + "/matter"
|
this.matterPath = util.GetHomePath() + "/matter"
|
||||||
} else {
|
} else {
|
||||||
this.MatterPath = this.Item.MatterPath
|
this.matterPath = this.item.MatterPath
|
||||||
}
|
}
|
||||||
util.MakeDirAll(this.MatterPath)
|
util.MakeDirAll(this.matterPath)
|
||||||
|
|
||||||
//使用配置项中的端口
|
//使用配置项中的端口
|
||||||
if this.Item.ServerPort != 0 {
|
if this.item.ServerPort != 0 {
|
||||||
this.ServerPort = this.Item.ServerPort
|
this.serverPort = this.item.ServerPort
|
||||||
}
|
}
|
||||||
|
|
||||||
this.MysqlUrl = util.GetMysqlUrl(this.Item.MysqlPort, this.Item.MysqlHost, this.Item.MysqlSchema, this.Item.MysqlUsername, this.Item.MysqlPassword)
|
this.mysqlUrl = util.GetMysqlUrl(this.item.MysqlPort, this.item.MysqlHost, this.item.MysqlSchema, this.item.MysqlUsername, this.item.MysqlPassword)
|
||||||
this.Installed = true
|
this.installed = true
|
||||||
|
|
||||||
core.LOGGER.Info("使用配置文件:%s", filePath)
|
core.LOGGER.Info("使用配置文件:%s", filePath)
|
||||||
core.LOGGER.Info("上传文件存放路径:%s", this.MatterPath)
|
core.LOGGER.Info("上传文件存放路径:%s", this.matterPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//是否已经安装
|
//是否已经安装
|
||||||
func (this *TankConfig) IsInstalled() bool {
|
func (this *TankConfig) Installed() bool {
|
||||||
return this.Installed
|
return this.installed
|
||||||
}
|
}
|
||||||
|
|
||||||
//启动端口
|
//启动端口
|
||||||
func (this *TankConfig) GetServerPort() int {
|
func (this *TankConfig) ServerPort() int {
|
||||||
return this.ServerPort
|
return this.serverPort
|
||||||
}
|
}
|
||||||
|
|
||||||
//获取mysql链接
|
//获取mysql链接
|
||||||
func (this *TankConfig) GetMysqlUrl() string {
|
func (this *TankConfig) MysqlUrl() string {
|
||||||
return this.MysqlUrl
|
return this.mysqlUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
//文件存放路径
|
//文件存放路径
|
||||||
func (this *TankConfig) GetMatterPath() string {
|
func (this *TankConfig) MatterPath() string {
|
||||||
return this.MatterPath
|
return this.matterPath
|
||||||
}
|
}
|
||||||
|
|
||||||
//完成安装过程,主要是要将配置写入到文件中
|
//完成安装过程,主要是要将配置写入到文件中
|
||||||
@ -187,9 +187,9 @@ func (this *TankConfig) FinishInstall(mysqlPort int, mysqlHost string, mysqlSche
|
|||||||
|
|
||||||
var configItem = &ConfigItem{
|
var configItem = &ConfigItem{
|
||||||
//默认监听端口号
|
//默认监听端口号
|
||||||
ServerPort: core.CONFIG.GetServerPort(),
|
ServerPort: core.CONFIG.ServerPort(),
|
||||||
//上传的文件路径,要求不以/结尾。如果没有指定,默认在根目录下的matter文件夹中。eg: /var/www/matter
|
//上传的文件路径,要求不以/结尾。如果没有指定,默认在根目录下的matter文件夹中。eg: /var/www/matter
|
||||||
MatterPath: core.CONFIG.GetMatterPath(),
|
MatterPath: core.CONFIG.MatterPath(),
|
||||||
//mysql相关配置。
|
//mysql相关配置。
|
||||||
//数据库端口
|
//数据库端口
|
||||||
MysqlPort: mysqlPort,
|
MysqlPort: mysqlPort,
|
||||||
|
@ -76,7 +76,7 @@ func (this *TankContext) ServeHTTP(writer http.ResponseWriter, request *http.Req
|
|||||||
func (this *TankContext) OpenDb() {
|
func (this *TankContext) OpenDb() {
|
||||||
|
|
||||||
var err error = nil
|
var err error = nil
|
||||||
this.db, err = gorm.Open("mysql", core.CONFIG.GetMysqlUrl())
|
this.db, err = gorm.Open("mysql", core.CONFIG.MysqlUrl())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
core.LOGGER.Panic("failed to connect mysql database")
|
core.LOGGER.Panic("failed to connect mysql database")
|
||||||
@ -204,7 +204,7 @@ func (this *TankContext) initBeans() {
|
|||||||
//系统如果安装好了就调用这个方法。
|
//系统如果安装好了就调用这个方法。
|
||||||
func (this *TankContext) InstallOk() {
|
func (this *TankContext) InstallOk() {
|
||||||
|
|
||||||
if core.CONFIG.IsInstalled() {
|
if core.CONFIG.Installed() {
|
||||||
this.OpenDb()
|
this.OpenDb()
|
||||||
|
|
||||||
for _, bean := range this.BeanMap {
|
for _, bean := range this.BeanMap {
|
||||||
|
@ -132,7 +132,7 @@ func (this *TankRouter) ServeHTTP(writer http.ResponseWriter, request *http.Requ
|
|||||||
writer.Header().Set("Cache-Control", "no-cache")
|
writer.Header().Set("Cache-Control", "no-cache")
|
||||||
writer.Header().Set("Expires", "0")
|
writer.Header().Set("Expires", "0")
|
||||||
|
|
||||||
if core.CONFIG.IsInstalled() {
|
if core.CONFIG.Installed() {
|
||||||
//已安装的模式
|
//已安装的模式
|
||||||
|
|
||||||
//统一处理用户的身份信息。
|
//统一处理用户的身份信息。
|
||||||
|
4
main.go
4
main.go
@ -30,9 +30,9 @@ func main() {
|
|||||||
|
|
||||||
//第四步。启动http服务
|
//第四步。启动http服务
|
||||||
http.Handle("/", core.CONTEXT)
|
http.Handle("/", core.CONTEXT)
|
||||||
core.LOGGER.Info("App started at http://localhost:%v", core.CONFIG.GetServerPort())
|
core.LOGGER.Info("App started at http://localhost:%v", core.CONFIG.ServerPort())
|
||||||
|
|
||||||
dotPort := fmt.Sprintf(":%v", core.CONFIG.GetServerPort())
|
dotPort := fmt.Sprintf(":%v", core.CONFIG.ServerPort())
|
||||||
err1 := http.ListenAndServe(dotPort, nil)
|
err1 := http.ListenAndServe(dotPort, nil)
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
log.Fatal("ListenAndServe: ", err1)
|
log.Fatal("ListenAndServe: ", err1)
|
||||||
|
Loading…
Reference in New Issue
Block a user