From b421971b61943e02adf236fbc656be56d7ad19e9 Mon Sep 17 00:00:00 2001 From: Jeremy Latt Date: Sat, 1 Mar 2014 15:02:24 -0800 Subject: [PATCH] move db init/open functions into a single file --- ergonomadic.go | 25 +------------------------ irc/database.go | 32 ++++++++++++++++++++++++++++++++ irc/server.go | 8 +------- 3 files changed, 34 insertions(+), 31 deletions(-) create mode 100644 irc/database.go diff --git a/ergonomadic.go b/ergonomadic.go index e04f9e8a..f47f695d 100644 --- a/ergonomadic.go +++ b/ergonomadic.go @@ -2,12 +2,10 @@ package main import ( "code.google.com/p/go.crypto/bcrypt" - "database/sql" "encoding/base64" "flag" "fmt" "github.com/jlatt/ergonomadic/irc" - _ "github.com/mattn/go-sqlite3" "log" "os" ) @@ -21,27 +19,6 @@ func genPasswd(passwd string) { fmt.Println(encoded) } -func initDB(config *irc.Config) { - os.Remove(config.Database()) - - db, err := sql.Open("sqlite3", config.Database()) - if err != nil { - log.Fatal(err) - } - defer db.Close() - - _, err = db.Exec(` - CREATE TABLE channel ( - name TEXT NOT NULL UNIQUE, - flags TEXT NOT NULL, - key TEXT NOT NULL, - topic TEXT NOT NULL, - user_limit INTEGER DEFAULT 0)`) - if err != nil { - log.Fatal(err) - } -} - func main() { conf := flag.String("conf", "ergonomadic.json", "ergonomadic config file") initdb := flag.Bool("initdb", false, "initialize database") @@ -59,7 +36,7 @@ func main() { } if *initdb { - initDB(config) + irc.InitDB(config.Database()) return } diff --git a/irc/database.go b/irc/database.go new file mode 100644 index 00000000..1ff3bd67 --- /dev/null +++ b/irc/database.go @@ -0,0 +1,32 @@ +package irc + +import ( + "database/sql" + _ "github.com/mattn/go-sqlite3" + "log" + "os" +) + +func InitDB(path string) { + os.Remove(path) + db := OpenDB(path) + defer db.Close() + _, err := db.Exec(` + CREATE TABLE channel ( + name TEXT NOT NULL UNIQUE, + flags TEXT NOT NULL, + key TEXT NOT NULL, + topic TEXT NOT NULL, + user_limit INTEGER DEFAULT 0)`) + if err != nil { + log.Fatal(err) + } +} + +func OpenDB(path string) *sql.DB { + db, err := sql.Open("sqlite3", path) + if err != nil { + log.Fatal(err) + } + return db +} diff --git a/irc/server.go b/irc/server.go index 49c5f4ad..3a4cf69b 100644 --- a/irc/server.go +++ b/irc/server.go @@ -7,7 +7,6 @@ import ( "database/sql" "encoding/binary" "fmt" - _ "github.com/mattn/go-sqlite3" "log" "net" "os" @@ -36,17 +35,12 @@ type Server struct { } func NewServer(config *Config) *Server { - db, err := sql.Open("sqlite3", config.Database()) - if err != nil { - log.Fatal(err) - } - server := &Server{ channels: make(ChannelNameMap), clients: make(ClientNameMap), commands: make(chan Command, 16), ctime: time.Now(), - db: db, + db: OpenDB(config.Database()), idle: make(chan *Client, 16), motdFile: config.MOTD, name: config.Name,