mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-09-16 07:56:24 -07:00
make db connection code more straightforward
This commit is contained in:
parent
ccc1fc6633
commit
5cace590bc
4 changed files with 71 additions and 76 deletions
|
@ -1,7 +1,7 @@
|
|||
-- Initial setup file for Gochan
|
||||
-- Deleted after setup is finished
|
||||
|
||||
CREATE DATABASE IF NOT EXISTS `DBNAME`;
|
||||
CREATE DATABASE `DBNAME`;
|
||||
USE `DBNAME`;
|
||||
|
||||
CREATE TABLE `DBPREFIXannouncements` (
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
//"syscall"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
|
@ -15,15 +13,8 @@ func main() {
|
|||
defer db.Close()
|
||||
initConfig()
|
||||
fmt.Println("Config file loaded. Connecting to database...")
|
||||
_,err := os.Stat("initialsetupdb.sql")
|
||||
//check if initialsetup file exists
|
||||
if err != nil {
|
||||
needs_initial_setup = false
|
||||
connectToSQLServer()
|
||||
} else {
|
||||
needs_initial_setup = true
|
||||
runInitialSetup()
|
||||
}
|
||||
connectToSQLServer()
|
||||
|
||||
fmt.Println("Loading and parsing templates...")
|
||||
initTemplates()
|
||||
fmt.Println("Initializing server...")
|
||||
|
|
|
@ -1,46 +0,0 @@
|
|||
// functions for setting up SQL tables and the base administrator account
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func runInitialSetup() {
|
||||
if !db_connected {
|
||||
connectToSQLServer()
|
||||
}
|
||||
loadInitialSetupFile()
|
||||
db.Close()
|
||||
connectToSQLServer()
|
||||
}
|
||||
|
||||
func loadInitialSetupFile() {
|
||||
initial_sql_bytes,err := ioutil.ReadFile("initialsetupdb.sql")
|
||||
initial_sql_str := string(initial_sql_bytes)
|
||||
fmt.Printf("Starting initial setup...")
|
||||
if err == nil {
|
||||
initial_sql_str = strings.Replace(initial_sql_str,"DBNAME",config.DBname, -1)
|
||||
initial_sql_str = strings.Replace(initial_sql_str,"DBPREFIX",config.DBprefix, -1)
|
||||
initial_sql_str += "\nINSERT INTO `"+config.DBname+"`.`"+config.DBprefix+"staff` (`username`, `password_checksum`, `salt`, `rank`) VALUES ('admin', '"+bcrypt_sum("password")+"', 'abc', 3);"
|
||||
initial_sql_arr := strings.Split(initial_sql_str, ";")
|
||||
for _,statement := range initial_sql_arr {
|
||||
if statement != "" {
|
||||
_,err := db.Exec(statement+";")
|
||||
if err != nil {
|
||||
fmt.Println("failed.")
|
||||
db.Exec("USE `"+config.DBname+"`;")
|
||||
error_log.Fatal(err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("complete.")
|
||||
db.Exec("USE `"+config.DBname+"`;")
|
||||
} else {
|
||||
error_log.Fatal("failed. Couldn't load initial sql file")
|
||||
}
|
||||
}
|
86
src/sql.go
86
src/sql.go
|
@ -5,8 +5,10 @@ import (
|
|||
"io"
|
||||
"fmt"
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
_ "github.com/ziutek/mymysql/godrv"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -68,28 +70,76 @@ func escapeQuotes(txt string) string {
|
|||
}
|
||||
|
||||
|
||||
|
||||
func connectToSQLServer() {
|
||||
// does the original initialsetupdb.sql (as opposed to .bak.sql) exist?
|
||||
var err error
|
||||
if needs_initial_setup {
|
||||
db, err = sql.Open("mymysql", config.DBhost+"*mysql/"+config.DBusername+"/"+config.DBpassword)
|
||||
if err != nil {
|
||||
fmt.Println("Failed to connect to the database, see log for details.")
|
||||
error_log.Fatal(err.Error())
|
||||
}
|
||||
_, err1 := os.Stat("initialsetupdb.sql")
|
||||
_, err2 := os.Stat("initialsetupdb.bak.sql")
|
||||
if err2 == nil {
|
||||
// the .bak.sql file exists
|
||||
os.Remove("initialsetupdb.sql")
|
||||
fmt.Println("complete.")
|
||||
needs_initial_setup = false
|
||||
return
|
||||
} else {
|
||||
db, err = sql.Open("mymysql", config.DBhost+"*"+config.DBname+"/"+config.DBusername+"/"+config.DBpassword)
|
||||
if err != nil {
|
||||
fmt.Println("Failed to connect to the database, see log for details.")
|
||||
error_log.Fatal(err.Error())
|
||||
if err1 != nil {
|
||||
// neither one exists
|
||||
fmt.Println("failed...initial setup file doesn't exist, please reinstall gochan.")
|
||||
error_log.Fatal("Initial setup file doesn't exist, exiting.")
|
||||
return
|
||||
}
|
||||
}
|
||||
_, err = db.Exec("USE `mysql`")
|
||||
if err == driver.ErrBadConn {
|
||||
fmt.Println("Error: failed connecting to the database.")
|
||||
err1 = nil
|
||||
err2 = nil
|
||||
|
||||
db, err = sql.Open("mymysql", config.DBhost+"*mysql/"+config.DBusername+"/"+config.DBpassword)
|
||||
if err != nil {
|
||||
fmt.Println("Failed to connect to the database, see log for details.")
|
||||
error_log.Fatal(err.Error())
|
||||
} else {
|
||||
db.Exec("USE `" + config.DBname + "`")
|
||||
}
|
||||
// read the initial setup sql file into a string
|
||||
initial_sql_bytes,err := ioutil.ReadFile("initialsetupdb.sql")
|
||||
if err != nil {
|
||||
fmt.Println("failed.")
|
||||
error_log.Fatal(err.Error())
|
||||
}
|
||||
initial_sql_str := string(initial_sql_bytes)
|
||||
initial_sql_bytes = nil
|
||||
fmt.Printf("Starting initial setup...")
|
||||
initial_sql_str = strings.Replace(initial_sql_str,"DBNAME",config.DBname, -1)
|
||||
initial_sql_str = strings.Replace(initial_sql_str,"DBPREFIX",config.DBprefix, -1)
|
||||
initial_sql_str += "\nINSERT INTO `"+config.DBname+"`.`"+config.DBprefix+"staff` (`username`, `password_checksum`, `salt`, `rank`) VALUES ('admin', '"+bcrypt_sum("password")+"', 'abc', 3);"
|
||||
initial_sql_arr := strings.Split(initial_sql_str, ";")
|
||||
initial_sql_str = ""
|
||||
|
||||
for _,statement := range initial_sql_arr {
|
||||
fmt.Println(statement)
|
||||
if statement != "" {
|
||||
_,err := db.Exec(statement+";")
|
||||
if err != nil {
|
||||
fmt.Println("failed.")
|
||||
error_log.Fatal(err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
initial_sql_arr = nil
|
||||
// rename initialsetupdb.sql to initialsetup.bak.sql
|
||||
err = ioutil.WriteFile("initialsetupdb.bak.sql", initial_sql_bytes, 0777)
|
||||
if err != nil {
|
||||
fmt.Println("failed")
|
||||
error_log.Fatal(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
err = os.Remove("initialsetupdb.bak.sql")
|
||||
if err != nil {
|
||||
fmt.Println("failed.")
|
||||
error_log.Fatal(err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Println("complete.")
|
||||
|
||||
needs_initial_setup = false
|
||||
db_connected = true
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue