2017-04-01 09:04:40 +00:00
|
|
|
package install
|
|
|
|
|
|
|
|
import (
|
2017-04-02 02:38:49 +00:00
|
|
|
"github.com/ouqiang/cron-scheduler/models"
|
|
|
|
"github.com/ouqiang/cron-scheduler/modules/app"
|
|
|
|
"github.com/ouqiang/cron-scheduler/modules/setting"
|
|
|
|
"github.com/ouqiang/cron-scheduler/modules/utils"
|
|
|
|
"gopkg.in/macaron.v1"
|
|
|
|
"strconv"
|
2017-04-01 09:04:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 系统安装
|
|
|
|
|
|
|
|
type InstallForm struct {
|
2017-04-02 02:38:49 +00:00
|
|
|
DbType string `binding:"IN(mysql)"`
|
|
|
|
DbHost string `binding:"Required"`
|
2017-04-03 02:20:08 +00:00
|
|
|
DbPort int `binding:"Required"` // todo 限制端口范围1-65535, 为什么规则未生效?
|
2017-04-02 02:38:49 +00:00
|
|
|
DbUsername string `binding:"Required"`
|
|
|
|
DbPassword string `binding:"Required"`
|
|
|
|
DbName string `binding:"Required"`
|
|
|
|
DbTablePrefix string
|
|
|
|
AdminUsername string `binding:"Required;MinSize(3)"`
|
|
|
|
AdminPassword string `binding:"Required;MinSize(6)"`
|
|
|
|
AdminEmail string `binding:"Email"`
|
2017-04-01 09:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 显示安装页面
|
2017-04-02 02:19:52 +00:00
|
|
|
func Show(ctx *macaron.Context) {
|
2017-04-02 02:38:49 +00:00
|
|
|
if app.Installed {
|
|
|
|
ctx.Redirect("/")
|
|
|
|
}
|
|
|
|
ctx.Data["Title"] = "安装"
|
|
|
|
ctx.Data["DisableNav"] = true
|
|
|
|
ctx.HTML(200, "install/show")
|
2017-04-01 09:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 安装,
|
|
|
|
func Install(ctx *macaron.Context, form InstallForm) string {
|
2017-04-02 02:38:49 +00:00
|
|
|
json := utils.Json{}
|
|
|
|
if app.Installed {
|
|
|
|
return json.Failure(utils.ResponseFailure, "系统已安装成功")
|
|
|
|
}
|
|
|
|
// 写入数据库配置
|
|
|
|
err := writeConfig(form)
|
|
|
|
if err != nil {
|
|
|
|
utils.RecordLog(err)
|
|
|
|
return json.Failure(utils.ResponseFailure, "数据库配置写入文件失败")
|
|
|
|
}
|
2017-04-01 09:04:40 +00:00
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
// 初始化Db
|
|
|
|
app.InitDb()
|
|
|
|
// 创建数据库表
|
|
|
|
migration := new(models.Migration)
|
|
|
|
err = migration.Exec(form.DbName)
|
|
|
|
if err != nil {
|
|
|
|
utils.RecordLog(err)
|
|
|
|
return json.Failure(utils.ResponseFailure, "创建数据库表失败")
|
|
|
|
}
|
2017-04-01 09:04:40 +00:00
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
// 创建管理员账号
|
|
|
|
err = createAdminUser(form)
|
|
|
|
if err != nil {
|
|
|
|
utils.RecordLog(err)
|
|
|
|
return json.Failure(utils.ResponseFailure, "创建管理员账号失败")
|
|
|
|
}
|
2017-04-01 09:04:40 +00:00
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
// 创建安装锁
|
|
|
|
err = app.CreateInstallLock()
|
|
|
|
if err != nil {
|
|
|
|
utils.RecordLog(err)
|
|
|
|
return json.Failure(utils.ResponseFailure, "创建文件安装锁失败")
|
|
|
|
}
|
2017-04-01 12:28:30 +00:00
|
|
|
|
2017-04-03 01:44:41 +00:00
|
|
|
app.Installed = true
|
2017-04-02 02:38:49 +00:00
|
|
|
// 初始化定时任务等
|
|
|
|
app.InitResource()
|
2017-04-01 12:28:30 +00:00
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
return json.Success("安装成功", nil)
|
2017-04-01 09:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 数据库配置写入文件
|
|
|
|
func writeConfig(form InstallForm) error {
|
2017-04-02 02:38:49 +00:00
|
|
|
dbConfig := map[string]map[string]string{
|
|
|
|
"db": map[string]string{
|
|
|
|
"engine": form.DbType,
|
|
|
|
"host": form.DbHost,
|
|
|
|
"port": strconv.Itoa(form.DbPort),
|
|
|
|
"user": form.DbUsername,
|
|
|
|
"password": form.DbPassword,
|
|
|
|
"database": form.DbName,
|
|
|
|
"prefix": form.DbTablePrefix,
|
|
|
|
"charset": "utf8",
|
|
|
|
},
|
|
|
|
}
|
2017-04-01 09:04:40 +00:00
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
return setting.Write(dbConfig, app.AppConfig)
|
2017-04-01 09:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 创建管理员账号
|
|
|
|
func createAdminUser(form InstallForm) error {
|
2017-04-02 02:38:49 +00:00
|
|
|
user := new(models.User)
|
|
|
|
user.Name = form.AdminUsername
|
|
|
|
user.Password = form.AdminPassword
|
|
|
|
user.Email = form.AdminEmail
|
|
|
|
user.IsAdmin = 1
|
|
|
|
_, err := user.Create()
|
2017-04-01 09:04:40 +00:00
|
|
|
|
2017-04-02 02:38:49 +00:00
|
|
|
return err
|
2017-04-02 02:19:52 +00:00
|
|
|
}
|