You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.4 KiB
65 lines
1.4 KiB
package core
|
|
|
|
import (
|
|
"aufs/db"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
_ "github.com/logoove/sqlite"
|
|
)
|
|
|
|
// 输出的结构
|
|
type ScJson struct {
|
|
Message string `json:"message"`
|
|
Status int `json:"status"`
|
|
Data []db.StServerInfo `json:"data"`
|
|
}
|
|
|
|
// 系统设置
|
|
func Settdb(w http.ResponseWriter, r *http.Request) {
|
|
// 检查数据表是否存在
|
|
scdb, err := sqlx.Open("sqlite", "./ups.db")
|
|
if err != nil {
|
|
fmt.Printf("Failed to open database: %v\n", err)
|
|
return
|
|
}
|
|
defer scdb.Close()
|
|
// 如果表sc_server不存在,则创建
|
|
_, err = scdb.Exec("SELECT name FROM sqlite_master WHERE type='table' AND name='sc_server'")
|
|
if err != nil {
|
|
// cuowu
|
|
log.Fatalf("table has exists:%s", err)
|
|
}
|
|
//如果表存在
|
|
sclst := db.GetlScList()
|
|
//
|
|
scresp := ScJson{
|
|
Message: "success",
|
|
Status: 200,
|
|
Data: sclst,
|
|
}
|
|
uCorsHadler(w, r)
|
|
json.NewEncoder(w).Encode(scresp)
|
|
|
|
// 表不存在,创建表
|
|
// sts := `
|
|
// CREATE TABLE IF NOT EXISTS sc_server (
|
|
// id INTEGER PRIMARY KEY NOT NULL,
|
|
// scname TEXT NOT NULL,
|
|
// addr TEXT NOT NULL,
|
|
// port TEXT NOT NULL,
|
|
// token TEXT NOT NULL,
|
|
// status INTEGER NOT NULL
|
|
// );`
|
|
// 执行建表语句
|
|
// _, err = scdb.Exec(sts)
|
|
// if err != nil {
|
|
// fmt.Printf("Failed to create database table: %v\n", err)
|
|
// return
|
|
// }
|
|
// fmt.Printf("Successfully created database table! \n")
|
|
|
|
}
|
|
|