Add something for install.
This commit is contained in:
parent
5611c5acb6
commit
ea1bd27aa8
@ -1,13 +1,18 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"time"
|
||||
"reflect"
|
||||
"math"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Time time.Time
|
||||
|
||||
type IBase interface {
|
||||
//返回其对应的数据库表名
|
||||
TableName() string
|
||||
}
|
||||
|
||||
type Base struct {
|
||||
Uuid string `gorm:"primary_key" json:"uuid"`
|
||||
Sort int64 `json:"sort"`
|
||||
@ -27,6 +32,10 @@ func (this *Base) Map() map[string]interface{} {
|
||||
return data
|
||||
}
|
||||
|
||||
func (Base) TableName() string {
|
||||
return TABLE_PREFIX + "base"
|
||||
}
|
||||
|
||||
//分页类
|
||||
type Pager struct {
|
||||
Page int `json:"page"`
|
||||
|
@ -1,11 +1,15 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jinzhu/gorm"
|
||||
"go/build"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
//安装程序的接口,只有安装阶段可以访问。
|
||||
type InstallController struct {
|
||||
BaseController
|
||||
uploadTokenDao *UploadTokenDao
|
||||
@ -60,14 +64,13 @@ func (this *InstallController) RegisterRoutes() map[string]func(writer http.Resp
|
||||
|
||||
//每个Controller需要主动注册自己的路由。
|
||||
routeMap["/api/install/verify"] = this.Wrap(this.Verify, USER_ROLE_GUEST)
|
||||
routeMap["/api/install/table/dashboard"] = this.Wrap(this.InstallTableDashboard, USER_ROLE_GUEST)
|
||||
routeMap["/api/install/table/info/list"] = this.Wrap(this.InstallTableInfoList, USER_ROLE_GUEST)
|
||||
|
||||
return routeMap
|
||||
}
|
||||
|
||||
//验证数据库连接
|
||||
func (this *InstallController) Verify(writer http.ResponseWriter, request *http.Request) *WebResult {
|
||||
|
||||
//获取数据库连接
|
||||
func (this *InstallController) openDbConnection(writer http.ResponseWriter, request *http.Request) *gorm.DB {
|
||||
mysqlPortStr := request.FormValue("mysqlPort")
|
||||
mysqlHost := request.FormValue("mysqlHost")
|
||||
mysqlSchema := request.FormValue("mysqlSchema")
|
||||
@ -82,23 +85,121 @@ func (this *InstallController) Verify(writer http.ResponseWriter, request *http.
|
||||
}
|
||||
|
||||
mysqlUrl := GetMysqlUrl(mysqlPort, mysqlHost, mysqlSchema, mysqlUsername, mysqlPassword)
|
||||
this.logger.Info("验证MySQL连接性 %s", mysqlUrl)
|
||||
|
||||
this.logger.Info("连接MySQL %s", mysqlUrl)
|
||||
|
||||
var err error = nil
|
||||
db, err := gorm.Open("mysql", mysqlUrl)
|
||||
this.PanicError(err)
|
||||
|
||||
return db
|
||||
|
||||
}
|
||||
|
||||
//关闭数据库连接
|
||||
func (this *InstallController) closeDbConnection(db *gorm.DB) {
|
||||
|
||||
if db != nil {
|
||||
err := db.Close()
|
||||
if err != nil {
|
||||
this.logger.Error("关闭数据库连接出错 %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//根据表名获取建表SQL语句
|
||||
func (this *InstallController) getCreateSQLFromFile(tableName string) string {
|
||||
|
||||
//1. 从当前安装目录db下去寻找建表文件。
|
||||
homePath := GetHomePath()
|
||||
filePath := homePath + "/db/" + tableName + ".sql"
|
||||
exists, err := PathExists(filePath)
|
||||
if err != nil {
|
||||
this.PanicServer("从安装目录判断建表语句文件是否存在时出错!")
|
||||
}
|
||||
|
||||
//2. 从GOPATH下面去找,因为可能是开发环境
|
||||
if !exists {
|
||||
|
||||
this.logger.Info("GOPATH = %s", build.Default.GOPATH)
|
||||
|
||||
filePath1 := filePath
|
||||
filePath = build.Default.GOPATH + "/src/tank/build/db/" + tableName + ".sql"
|
||||
exists, err = PathExists(filePath)
|
||||
if err != nil {
|
||||
this.PanicServer("从GOPATH判断建表语句文件是否存在时出错!")
|
||||
}
|
||||
|
||||
if !exists {
|
||||
this.PanicServer(fmt.Sprintf("%s 或 %s 均不存在,请检查你的安装情况。", filePath1, filePath))
|
||||
}
|
||||
}
|
||||
|
||||
//读取文件内容.
|
||||
bytes, err := ioutil.ReadFile(filePath)
|
||||
this.PanicError(err)
|
||||
|
||||
return string(bytes)
|
||||
}
|
||||
|
||||
//根据表名获取建表SQL语句
|
||||
func (this *InstallController) getCreateSQLFromDb(db *gorm.DB, base IBase) (bool, string) {
|
||||
|
||||
var hasTable = true
|
||||
var tableName = base.TableName()
|
||||
hasTable = db.HasTable(base)
|
||||
if !hasTable {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
// Scan
|
||||
type Result struct {
|
||||
Table string
|
||||
CreateTable string
|
||||
}
|
||||
|
||||
//读取建表语句。
|
||||
var result = &Result{}
|
||||
db1 := db.Exec("SHOW CREATE TABLE " + tableName).Scan(result)
|
||||
this.PanicError(db1.Error)
|
||||
|
||||
return true, result.CreateTable
|
||||
}
|
||||
|
||||
//验证数据库连接
|
||||
func (this *InstallController) Verify(writer http.ResponseWriter, request *http.Request) *WebResult {
|
||||
|
||||
db := this.openDbConnection(writer, request)
|
||||
defer this.closeDbConnection(db)
|
||||
|
||||
this.logger.Info("Ping一下数据库")
|
||||
err = db.DB().Ping()
|
||||
err := db.DB().Ping()
|
||||
this.PanicError(err)
|
||||
|
||||
return this.Success("OK")
|
||||
|
||||
}
|
||||
|
||||
//安装dashboard表
|
||||
func (this *InstallController) InstallTableDashboard(writer http.ResponseWriter, request *http.Request) *WebResult {
|
||||
//获取需要安装的数据库表
|
||||
func (this *InstallController) InstallTableInfoList(writer http.ResponseWriter, request *http.Request) *WebResult {
|
||||
|
||||
return this.Success("")
|
||||
var tableNames = []IBase{&Dashboard{}, &DownloadToken{}, &Footprint{}, &ImageCache{}, &Matter{}, &Preference{}, &Session{}, UploadToken{}, &User{}}
|
||||
var installTableInfos []*InstallTableInfo
|
||||
|
||||
db := this.openDbConnection(writer, request)
|
||||
defer this.closeDbConnection(db)
|
||||
|
||||
for _, iBase := range tableNames {
|
||||
|
||||
exist, sql := this.getCreateSQLFromDb(db, iBase)
|
||||
installTableInfos = append(installTableInfos, &InstallTableInfo{
|
||||
Name: iBase.TableName(),
|
||||
CreateSql: this.getCreateSQLFromFile(iBase.TableName()),
|
||||
TableExist: exist,
|
||||
ExistCreateSql: sql,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
return this.Success(installTableInfos)
|
||||
|
||||
}
|
||||
|
11
rest/install_model.go
Normal file
11
rest/install_model.go
Normal file
@ -0,0 +1,11 @@
|
||||
package rest
|
||||
|
||||
/**
|
||||
* 表名对应的表结构
|
||||
*/
|
||||
type InstallTableInfo struct {
|
||||
Name string `json:"name"`
|
||||
CreateSql string `json:"createSql"`
|
||||
TableExist bool `json:"tableExist"`
|
||||
ExistCreateSql string `json:"existCreateSql"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user