Пример #1
0
func (InstallController) SetupOptions(ctx echo.Context) error {
	var (
		noEmailConf = false
		noQiniuConf = false
	)

	if config.ConfigFile.MustValue("email", "smtp_username") == "" {
		noEmailConf = true
	}

	if config.ConfigFile.MustValue("qiniu", "access_key") == "" {
		noQiniuConf = true
	}

	if !noEmailConf && !noQiniuConf {
		return ctx.Redirect(http.StatusSeeOther, "/")
	}

	if ctx.Request().Method() == "POST" {
		config.ConfigFile.SetSectionComments("email", "用于注册发送激活码等")
		emailFields := []string{"smtp_host", "smtp_port", "smtp_username", "smtp_password", "from_email"}
		for _, field := range emailFields {
			if field == "smtp_port" && ctx.FormValue("smtp_port") == "" {
				config.ConfigFile.SetValue("email", field, "25")
			} else {
				config.ConfigFile.SetValue("email", field, ctx.FormValue(field))
			}
		}

		config.ConfigFile.SetSectionComments("qiniu", "图片存储在七牛云,如果没有可以通过 https://portal.qiniu.com/signup?code=3lfz4at7pxfma 免费申请")
		qiniuFields := []string{"access_key", "secret_key", "bucket_name"}
		for _, field := range qiniuFields {
			config.ConfigFile.SetValue("qiniu", field, ctx.FormValue(field))
		}

		config.SaveConfigFile()

		return renderInstall(ctx, "install/setup-options.html", map[string]interface{}{"success": true})
	}

	data := map[string]interface{}{
		"no_email_conf": noEmailConf,
		"no_qiniu_conf": noQiniuConf,
	}
	return renderInstall(ctx, "install/setup-options.html", data)
}
Пример #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
}