Example #1
0
// DoInstall 执行安装,包括站点简单配置,安装数据库(创建数据库、表,填充基本数据)等
func (self InstallController) DoInstall(ctx echo.Context) error {
	if db.MasterDB == nil {
		return ctx.Redirect(http.StatusSeeOther, "/install")
	}

	if logic.DefaultInstall.IsTableExist(ctx) {
		if logic.DefaultInstall.HadRootUser(ctx) {
			return ctx.Redirect(http.StatusSeeOther, "/")
		}
	}

	step := goutils.MustInt(ctx.QueryParam("step"), 1)
	data := map[string]interface{}{
		"user_name":   "admin",
		"admin_email": "",
		"step":        step,
	}

	if step == 2 {
		username := ctx.FormValue("user_name")
		email := ctx.FormValue("admin_email")
		password1 := ctx.FormValue("admin_password")
		password2 := ctx.FormValue("admin_password2")

		if username == "" || email == "" {
			data["err"] = "用户名和邮箱不能留空"
			return renderInstall(ctx, "install/install.html", data)
		}

		data["user_name"] = username
		data["admin_email"] = email

		if password1 != password2 {
			data["err"] = "两次输入的密码不一致"
			return renderInstall(ctx, "install/install.html", data)
		}

		err := logic.DefaultInstall.CreateTable(ctx)
		if err != nil {
			data["err"] = "创建数据表失败!"
			return renderInstall(ctx, "install/install.html", data)
		}

		err = logic.DefaultInstall.InitTable(ctx)
		if err != nil {
			data["err"] = "初始化数据表失败!"
			return renderInstall(ctx, "install/install.html", data)
		}

		if password1 == "" {
			password1 = goutils.RandString(12)
			data["passwd"] = password1
		}

		// 创建管理员
		form := url.Values{
			"username": {username},
			"email":    {email},
			"passwd":   {password1},
			"is_root":  {"true"},
			"status":   {strconv.Itoa(model.UserStatusAudit)},
		}
		errMsg, err := logic.DefaultUser.CreateUser(ctx, form)
		if err != nil {
			data["err"] = errMsg
			return renderInstall(ctx, "install/install.html", data)
		}

		data["step"] = 3

		data["os"] = runtime.GOOS

		// 为了保证程序正常,需要重启
		go self.reload()
	}
	return renderInstall(ctx, "install/install.html", data)
}
Example #2
0
func (InstallController) genConfig(ctx echo.Context) error {
	env := ctx.FormValue("env")

	config.ConfigFile.SetSectionComments("global", "")
	config.ConfigFile.SetValue("global", "env", env)

	var (
		logLevel     = "DEBUG"
		domain       = global.App.Host + ":" + global.App.Port
		xormLogLevel = "0"
		xormShowSql  = "true"
	)
	if env == "pro" {
		logLevel = "INFO"
		domain = "studygolang.com"
		xormLogLevel = "1"
		xormShowSql = "false"
	}

	config.ConfigFile.SetValue("global", "log_level", logLevel)
	config.ConfigFile.SetValue("global", "domain", domain)
	config.ConfigFile.SetValue("global", "cookie_secret", goutils.RandString(10))
	config.ConfigFile.SetValue("global", "data_path", "data/max_online_num")

	config.ConfigFile.SetSectionComments("listen", "")
	config.ConfigFile.SetValue("listen", "host", "")
	config.ConfigFile.SetValue("listen", "port", global.App.Port)

	dbname := ctx.FormValue("dbname")
	uname := ctx.FormValue("uname")
	pwd := ctx.FormValue("pwd")
	dbhost := ctx.FormValue("dbhost")
	dbport := ctx.FormValue("dbport")

	config.ConfigFile.SetSectionComments("mysql", "")
	config.ConfigFile.SetValue("mysql", "host", dbhost)
	config.ConfigFile.SetValue("mysql", "port", dbport)
	config.ConfigFile.SetValue("mysql", "user", uname)
	config.ConfigFile.SetValue("mysql", "password", pwd)
	config.ConfigFile.SetValue("mysql", "dbname", dbname)
	config.ConfigFile.SetValue("mysql", "charset", "utf8")
	config.ConfigFile.SetKeyComments("mysql", "max_idle", "最大空闲连接数")
	config.ConfigFile.SetValue("mysql", "max_idle", "2")
	config.ConfigFile.SetKeyComments("mysql", "max_conn", "最大打开连接数")
	config.ConfigFile.SetValue("mysql", "max_conn", "10")

	config.ConfigFile.SetSectionComments("xorm", "")
	config.ConfigFile.SetValue("xorm", "show_sql", xormShowSql)
	config.ConfigFile.SetKeyComments("xorm", "log_level", "0-debug, 1-info, 2-warning, 3-error, 4-off, 5-unknow")
	config.ConfigFile.SetValue("xorm", "log_level", xormLogLevel)

	config.ConfigFile.SetSectionComments("security", "")
	config.ConfigFile.SetKeyComments("security", "unsubscribe_token_key", "退订邮件使用的 token key")
	config.ConfigFile.SetValue("security", "unsubscribe_token_key", goutils.RandString(18))
	config.ConfigFile.SetKeyComments("security", "activate_sign_salt", "注册激活邮件使用的 sign salt")
	config.ConfigFile.SetValue("security", "activate_sign_salt", goutils.RandString(18))

	config.ConfigFile.SetSectionComments("sensitive", "过滤广告")
	config.ConfigFile.SetKeyComments("sensitive", "title", "标题关键词")
	config.ConfigFile.SetValue("sensitive", "title", "")
	config.ConfigFile.SetKeyComments("sensitive", "content", "内容关键词")
	config.ConfigFile.SetValue("sensitive", "content", "")

	config.ConfigFile.SetSectionComments("search", "搜索配置")
	config.ConfigFile.SetValue("search", "engine_url", "")

	// 校验数据库配置是否正确有效
	if err := db.TestDB(); err != nil {
		return err
	}

	config.SaveConfigFile()
	return nil
}