代码拉取完成,页面将自动刷新
/*
Copyright 2016 Wenhui Shen <www.webx.top>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package setup
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/admpub/nging/application/handler"
"github.com/admpub/nging/application/library/config"
"github.com/admpub/nging/application/library/cron"
"github.com/admpub/nging/application/model"
"github.com/lunny/log"
"github.com/webx-top/com"
"github.com/webx-top/db/lib/factory"
"github.com/webx-top/echo"
)
func init() {
handler.Register(func(e *echo.Echo) {
e.Route("GET,POST", `/setup`, Setup)
})
}
func Setup(ctx echo.Context) error {
var err error
lockFile := filepath.Join(com.SelfDir(), `installed.lock`)
if info, err := os.Stat(lockFile); err == nil && info.IsDir() == false {
return ctx.String(ctx.T(`已经安装过了。如要重新安装,请先删除%s`, lockFile))
}
sqlFile := filepath.Join(filepath.Dir(config.DefaultCLIConfig.Conf), `install.sql`)
if !com.FileExists(sqlFile) {
return ctx.String(ctx.T(`找不到文件%s,无法安装`, sqlFile))
}
if ctx.IsPost() {
adminUser := ctx.Form(`adminUser`)
adminPass := ctx.Form(`adminPass`)
adminEmail := ctx.Form(`adminEmail`)
if len(adminUser) == 0 {
err = errors.New(ctx.T(`管理员用户名不能为空`))
goto DIE
}
if !com.IsUsername(adminUser) {
err = errors.New(ctx.T(`管理员名不能包含特殊字符(只能由字母、数字、下划线和汉字组成)`))
goto DIE
}
if len(adminPass) < 8 {
err = errors.New(ctx.T(`管理员密码不能少于8个字符`))
goto DIE
}
if len(adminEmail) == 0 {
err = errors.New(ctx.T(`管理员邮箱不能为空`))
goto DIE
}
if !ctx.ValidateField(`adminEmail`, adminEmail, `email`) {
err = errors.New(ctx.T(`管理员邮箱格式不正确`))
goto DIE
}
err = ctx.MustBind(&config.DefaultConfig.DB)
config.DefaultConfig.DB.Database = strings.Replace(config.DefaultConfig.DB.Database, "'", "", -1)
config.DefaultConfig.DB.Database = strings.Replace(config.DefaultConfig.DB.Database, "`", "", -1)
if err != nil {
goto DIE
}
//连接数据库
err = config.ConnectDB()
if err != nil {
err = createDatabase(err)
}
if err != nil {
goto DIE
}
//创建数据库数据
var sqlStr string
err = com.SeekFileLines(sqlFile, func(line string) error {
if strings.HasPrefix(line, `--`) {
return nil
}
line = strings.TrimSpace(line)
sqlStr += line
if strings.HasSuffix(line, `;`) && len(sqlStr) > 0 {
_, err := factory.NewParam().SetCollection(sqlStr).Exec()
sqlStr = ``
if err != nil {
return err
}
}
return nil
})
if err != nil {
goto DIE
}
if err2 := cron.InitJobs(); err2 != nil {
log.Error(err2)
}
m := model.NewUser(ctx)
err = m.Register(adminUser, adminPass, adminEmail)
if err != nil {
goto DIE
}
//保存数据库账号到配置文件
err = config.DefaultConfig.SaveToFile()
if err != nil {
goto DIE
}
//生成锁文件
err = ioutil.WriteFile(lockFile, []byte(time.Now().Format(`2006-01-02 15:04:05`)), os.ModePerm)
if err != nil {
goto DIE
}
handler.SendOk(ctx, ctx.T(`安装成功`))
return ctx.Redirect(`/`)
}
DIE:
return ctx.Render(`setup`, handler.Err(ctx, err))
}
func createDatabase(err error) error {
switch config.DefaultConfig.DB.Type {
case `mysql`:
if strings.Contains(err.Error(), `Unknown database`) {
dbName := config.DefaultConfig.DB.Database
config.DefaultConfig.DB.Database = ``
err2 := config.ConnectDB()
if err2 != nil {
break
}
sqlStr := "CREATE DATABASE `" + dbName + "`"
_, err = factory.NewParam().SetCollection(sqlStr).Exec()
if err != nil {
break
}
config.DefaultConfig.DB.Database = dbName
err = config.ConnectDB()
}
}
return err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。