github.com/kotovmak/go-admin@v1.1.1/plugins/admin/controller/install.go (about)

     1  package controller
     2  
     3  import (
     4  	"bytes"
     5  	"database/sql"
     6  	"net/http"
     7  
     8  	"github.com/kotovmak/go-admin/context"
     9  	"github.com/kotovmak/go-admin/plugins/admin/modules/response"
    10  )
    11  
    12  // ShowInstall show install page.
    13  func (h *Handler) ShowInstall(ctx *context.Context) {
    14  
    15  	buffer := new(bytes.Buffer)
    16  	//template.GetInstallPage(buffer)
    17  
    18  	//rs, _ := mysql.Query("show tables;")
    19  	//fmt.Println(rs[0]["Tables_in_godmin"])
    20  
    21  	//rs2, _ := mysql.Query("show columns from users")
    22  	//fmt.Println(rs2[0]["Field"])
    23  
    24  	ctx.HTML(http.StatusOK, buffer.String())
    25  }
    26  
    27  // CheckDatabase check the database connection.
    28  func (h *Handler) CheckDatabase(ctx *context.Context) {
    29  
    30  	ip := ctx.FormValue("h")
    31  	port := ctx.FormValue("po")
    32  	username := ctx.FormValue("u")
    33  	password := ctx.FormValue("pa")
    34  	databaseName := ctx.FormValue("db")
    35  
    36  	SqlDB, err := sql.Open("mysql", username+":"+password+"@tcp("+ip+":"+port+")/"+databaseName+"?charset=utf8mb4")
    37  	if SqlDB != nil {
    38  		if SqlDB.Ping() != nil {
    39  			response.Error(ctx, "请检查参数是否设置正确")
    40  			return
    41  		}
    42  	}
    43  
    44  	defer func() {
    45  		_ = SqlDB.Close()
    46  	}()
    47  
    48  	if err != nil {
    49  		response.Error(ctx, "请检查参数是否设置正确")
    50  		return
    51  
    52  	}
    53  
    54  	//db.InitDB(username, password, port, ip, databaseName, 100, 100)
    55  
    56  	tables := make([]map[string]interface{}, 0)
    57  
    58  	list := "["
    59  
    60  	for i := 0; i < len(tables); i++ {
    61  		if i != len(tables)-1 {
    62  			list += `"` + tables[i]["Tables_in_godmin"].(string) + `",`
    63  		} else {
    64  			list += `"` + tables[i]["Tables_in_godmin"].(string) + `"`
    65  		}
    66  	}
    67  	list += "]"
    68  
    69  	response.OkWithData(ctx, map[string]interface{}{
    70  		"list": list,
    71  	})
    72  }