func DbInit() (*sql.DB, error) { logErr := logger.GetErr() db_info, err := DbConfig() if err != nil { log.Println("Db config error ", err) return nil, err } else { log.Println("Db config suc") } dbCmd := db_info.User + ":" + db_info.Cert + "@/" + db_info.Database + "?charset=utf8" // fmt.Println(dbCmd) db, err := sql.Open("mysql", dbCmd) if err != nil { logErr.Println("Db open failed ", err) return db, err } else { logSuc := logger.GetSuc() logSuc.Println("Db open success") } return db, nil }
func DbConfig() (Db, error) { dbInfo := Db{} logErr := logger.GetErr() logSuc := logger.GetSuc() f, err := os.Open("db.xml") defer f.Close() if err != nil { logErr.Println("db open error ", err) return dbInfo, err } xmlData, err := ioutil.ReadAll(f) if err != nil { logErr.Println("read xml error ", err) return dbInfo, err } err = xml.Unmarshal(xmlData, &dbInfo) if err != nil { logErr.Println("xml Unmarshal error ", err) return dbInfo, err } logSuc.Println("xml read succeed \n", dbInfo) return dbInfo, nil }
func GetDb() *sql.DB { db, err := DbInit() for i := 0; i < 20; i++ { if err != nil { logErr := logger.GetErr() logErr.Println("datebase open failed the ", i, "th times") } else { logSuc := logger.GetSuc() logSuc.Println("database open success the ", i, "th times") break } db, err = DbInit() } return db }
func register_User(user_info RegisterStruct) (string, error) { db := database.GetDb() defer db.Close() logErr := logger.GetErr() logInfo := logger.GetInfo() times := 0 rows, err := db.Query("select reg_times from userInfo where user_name = ? and if_use = 1", user_info.Username) if err != nil { logErr.Println(err) return "", err } for rows.Next() { err = rows.Scan(×) if err != nil { logErr.Println(err) return "", err } } if times != 0 { res := "This username is using." logInfo.Println(res) return res, nil } times++ stmt, err := db.Prepare("insert into userInfo(reg_times,user_name,user_cert) values(?,?,?)") if err != nil { logErr.Println(err) return "", err } _, err = stmt.Exec(times, user_info.Username, user_info.Psw1) if err != nil { logErr.Println(err) return "", err } return "", nil }
"fmt" "github.com/gorilla/securecookie" "html/template" "logger" "net/http" ) func Init() { ResultTypeInit() UserOnline = make(map[string]string) } var res_type_info ResultType var logInfo = logger.GetInfo() var logWar = logger.GetWar() var logErr = logger.GetErr() var logDeb = logger.GetSuc() func RegisterHandler(w http.ResponseWriter, r *http.Request) { fmt.Println(r.Method) if r.Method == "GET" { reg_Info := Reg_Type_Info{ IsHide: "none", Info: "", BackShow: false, } file, err := template.ParseFiles("../www/html/register.html") if err != nil {