gocron/routers/install/install.go

157 lines
4.5 KiB
Go
Raw Normal View History

2017-04-01 09:04:40 +00:00
package install
import (
2017-04-07 01:22:00 +00:00
"github.com/ouqiang/gocron/models"
"github.com/ouqiang/gocron/modules/app"
"github.com/ouqiang/gocron/modules/setting"
"github.com/ouqiang/gocron/modules/utils"
2017-04-02 02:38:49 +00:00
"gopkg.in/macaron.v1"
"strconv"
2017-04-25 09:22:54 +00:00
"fmt"
2017-05-01 05:59:52 +00:00
"github.com/ouqiang/gocron/service"
2017-05-05 08:31:24 +00:00
"github.com/go-macaron/binding"
2017-04-01 09:04:40 +00:00
)
// 系统安装
type InstallForm struct {
2017-04-13 09:35:59 +00:00
DbType string `binding:"In(mysql)"`
DbHost string `binding:"Required;MaxSize(50)"`
2017-04-07 01:13:36 +00:00
DbPort int `binding:"Required;Range(1,65535)"`
2017-04-13 09:35:59 +00:00
DbUsername string `binding:"Required;MaxSize(50)"`
DbPassword string `binding:"Required;MaxSize(30)"`
DbName string `binding:"Required;MaxSize(50)"`
2017-04-28 03:54:46 +00:00
DbTablePrefix string `binding:"MaxSize(20)"`
AdminUsername string `binding:"Required;MinSize(3)"`
AdminPassword string `binding:"Required;MinSize(6)"`
ConfirmAdminPassword string `binding:"Required;MinSize(6)"`
2017-04-13 09:35:59 +00:00
AdminEmail string `binding:"Required;Email;MaxSize(50)"`
2017-04-01 09:04:40 +00:00
}
2017-05-05 08:31:24 +00:00
func (f InstallForm) Error(ctx *macaron.Context, errs binding.Errors) {
if len(errs) == 0 {
return
}
json := utils.JsonResponse{}
content := json.CommonFailure("表单验证失败, 请检测输入")
ctx.Resp.Write([]byte(content))
}
2017-04-07 01:13:36 +00:00
func Create(ctx *macaron.Context) {
2017-04-02 02:38:49 +00:00
if app.Installed {
ctx.Redirect("/")
}
ctx.Data["Title"] = "安装"
ctx.Data["DisableNav"] = true
2017-04-07 01:13:36 +00:00
ctx.HTML(200, "install/create")
2017-04-01 09:04:40 +00:00
}
2017-04-07 01:13:36 +00:00
// 安装
func Store(ctx *macaron.Context, form InstallForm) string {
2017-04-13 09:35:59 +00:00
json := utils.JsonResponse{}
2017-04-02 02:38:49 +00:00
if app.Installed {
2017-04-13 09:35:59 +00:00
return json.CommonFailure("系统已安装!")
}
2017-04-28 03:54:46 +00:00
if form.AdminPassword != form.ConfirmAdminPassword {
return json.CommonFailure("两次输入密码不匹配")
}
err := testDbConnection(form)
if err != nil {
2017-04-13 09:35:59 +00:00
return json.CommonFailure("数据库连接失败", err)
2017-04-02 02:38:49 +00:00
}
// 写入数据库配置
err = writeConfig(form)
2017-04-02 02:38:49 +00:00
if err != nil {
2017-04-13 09:35:59 +00:00
return json.CommonFailure("数据库配置写入文件失败", err)
2017-04-02 02:38:49 +00:00
}
2017-04-01 09:04:40 +00:00
appConfig, err := setting.Read(app.AppConfig)
if err != nil {
return json.CommonFailure("读取应用配置失败", err)
}
app.Setting = appConfig
2017-05-01 05:59:52 +00:00
models.Db = models.CreateDb()
2017-04-02 02:38:49 +00:00
// 创建数据库表
migration := new(models.Migration)
err = migration.Exec(form.DbName)
if err != nil {
2017-04-25 09:22:54 +00:00
return json.CommonFailure(fmt.Sprintf("创建数据库表失败-%s", err.Error()), err)
2017-04-02 02:38:49 +00:00
}
2017-04-01 09:04:40 +00:00
2017-04-02 02:38:49 +00:00
// 创建管理员账号
err = createAdminUser(form)
if err != nil {
2017-04-13 09:35:59 +00:00
return json.CommonFailure("创建管理员账号失败", err)
2017-04-02 02:38:49 +00:00
}
2017-04-01 09:04:40 +00:00
2017-04-02 02:38:49 +00:00
// 创建安装锁
err = app.CreateInstallLock()
if err != nil {
2017-04-13 09:35:59 +00:00
return json.CommonFailure("创建文件安装锁失败", err)
2017-04-02 02:38:49 +00:00
}
app.Installed = true
2017-05-01 05:59:52 +00:00
// 初始化定时任务
serviceTask := new(service.Task)
serviceTask.Initialize()
2017-04-02 02:38:49 +00:00
return json.Success("安装成功", nil)
2017-04-01 09:04:40 +00:00
}
// 配置写入文件
2017-04-01 09:04:40 +00:00
func writeConfig(form InstallForm) error {
2017-05-23 07:16:39 +00:00
dbConfig := []string{
"db.engine", form.DbType,
"db.host", form.DbHost,
"db.port", strconv.Itoa(form.DbPort),
"db.user", form.DbUsername,
"db.password",form.DbPassword,
"db.database", form.DbName,
"db.prefix", form.DbTablePrefix,
"db.charset", "utf8",
"allow_ips", "",
"app.name", "定时任务管理系统", // 应用名称
"delay.task.enable", "false", // 是否开启延时任务
"delay.task.slots", "3600", // 时间轮槽数量
"delay.task.tick", "1s", // 时间轮每次转动的时间
"api.key", "",
"api.secret", "",
2017-04-02 02:38:49 +00:00
}
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
}
// 测试数据库连接
func testDbConnection(form InstallForm) error {
var dbConfig map[string]string = make(map[string]string)
dbConfig["engine"] = form.DbType
dbConfig["host"] = form.DbHost
dbConfig["port"] = strconv.Itoa(form.DbPort)
dbConfig["user"] = form.DbUsername
dbConfig["password"] = form.DbPassword
dbConfig["charset"] = "utf8"
db, err := models.CreateTmpDb(dbConfig)
2017-04-03 07:27:19 +00:00
if err != nil {
return err
}
2017-04-03 07:27:19 +00:00
defer db.Close()
err = db.Ping()
return err
}