Gisle Aune
5 years ago
5 changed files with 128 additions and 3 deletions
-
4.gitignore
-
17Gopkg.lock
-
15cmd/lucifer-server/main.go
-
57database/sqlite/init.go
-
38internal/config/config.go
@ -1,3 +1,5 @@ |
|||
lucifer-server |
|||
!cmd/**/* |
|||
vendor |
|||
vendor |
|||
/config.yaml |
|||
/sqlite3.db |
@ -1,5 +1,20 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"log" |
|||
|
|||
"git.aiterp.net/lucifer/lucifer/database/sqlite" |
|||
"git.aiterp.net/lucifer/lucifer/internal/config" |
|||
) |
|||
|
|||
func main() { |
|||
conf, err := config.Load("./config.yaml", "/etc/lucifer/lucifer.yaml") |
|||
if err != nil { |
|||
log.Fatalln("Failed to load configuration:", err) |
|||
} |
|||
|
|||
err = sqlite.Initialize(conf.DB.FileName) |
|||
if err != nil { |
|||
log.Fatalln("Failed to set up database:", err) |
|||
} |
|||
} |
@ -1,5 +1,60 @@ |
|||
package sqlite |
|||
|
|||
func Initialize() { |
|||
import ( |
|||
"strings" |
|||
|
|||
"github.com/jmoiron/sqlx" |
|||
|
|||
// sqlite driver
|
|||
_ "github.com/mattn/go-sqlite3" |
|||
) |
|||
|
|||
var db *sqlx.DB |
|||
|
|||
// Initialize sets up the sqlite database.
|
|||
func Initialize(filename string) (err error) { |
|||
if db != nil { |
|||
panic("sqlite.Initialize called twice!") |
|||
} |
|||
|
|||
db, err = sqlx.Open("sqlite3", filename) |
|||
if err != nil { |
|||
db = nil |
|||
return err |
|||
} |
|||
|
|||
err = db.Ping() |
|||
if err != nil { |
|||
db.Close() |
|||
db = nil |
|||
return err |
|||
} |
|||
|
|||
tableDefs := strings.Split(tableDefStr, ";") |
|||
for _, tableDef := range tableDefs { |
|||
_, err := db.Exec(strings.Trim(tableDef, "\n ")) |
|||
if err != nil { |
|||
db.Close() |
|||
db = nil |
|||
return err |
|||
} |
|||
} |
|||
|
|||
return nil |
|||
} |
|||
|
|||
const tableDefStr = ` |
|||
|
|||
CREATE TABLE IF NOT EXISTS "user" ( |
|||
id INTEGER PRIMARY KEY AUTOINCREMENT, |
|||
name VARCHAR(64) NOT NULL UNIQUE, |
|||
hash VARCHAR(128) NOT NULL |
|||
); |
|||
|
|||
CREATE TABLE IF NOT EXISTS "session" ( |
|||
id CHAR(64) PRIMARY KEY, |
|||
user_id INTEGER NOT NULL, |
|||
expire_date DATE NOT NULL |
|||
); |
|||
|
|||
` |
@ -0,0 +1,38 @@ |
|||
package config |
|||
|
|||
import ( |
|||
"os" |
|||
|
|||
"gopkg.in/yaml.v2" |
|||
) |
|||
|
|||
// Config is application configuration.
|
|||
type Config struct { |
|||
DB struct { |
|||
FileName string `yaml:"file_name"` |
|||
} `yaml:"db"` |
|||
} |
|||
|
|||
// Load loads the first valid config file from the list of file paths.
|
|||
func Load(filePaths ...string) (Config, error) { |
|||
file, err := os.Open(filePaths[0]) |
|||
if err != nil { |
|||
if len(filePaths) > 1 { |
|||
return Load(filePaths[1:]...) |
|||
} |
|||
|
|||
return Config{}, err |
|||
} |
|||
|
|||
config := Config{} |
|||
err = yaml.NewDecoder(file).Decode(&config) |
|||
if err != nil { |
|||
if len(filePaths) > 1 { |
|||
return Load(filePaths[1:]...) |
|||
} |
|||
|
|||
return Config{}, err |
|||
} |
|||
|
|||
return config, nil |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue