aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorNiall Sheridan <nsheridan@gmail.com>2016-08-08 23:39:46 +0100
committerNiall Sheridan <nsheridan@gmail.com>2016-08-09 22:07:58 +0100
commit030ff273473f0a5620ba276a370e5119f57179df (patch)
tree035f1d5f629a228f5d4a170583e92726dbefb604 /cmd
parent66a7d51577c83da7cc3cf385a188799fe885cd3a (diff)
SQLite DB support
Diffstat (limited to 'cmd')
-rw-r--r--cmd/cashierd/main.go4
-rw-r--r--cmd/dbinit/dbinit.go35
2 files changed, 28 insertions, 11 deletions
diff --git a/cmd/cashierd/main.go b/cmd/cashierd/main.go
index b92bd9e..bc7cba4 100644
--- a/cmd/cashierd/main.go
+++ b/cmd/cashierd/main.go
@@ -297,8 +297,8 @@ func certStore(config string) (store.CertStorer, error) {
var err error
engine := strings.Split(config, ":")[0]
switch engine {
- case "mysql":
- cstore, err = store.NewMySQLStore(config)
+ case "mysql", "sqlite":
+ cstore, err = store.NewSQLStore(config)
case "mongo":
cstore, err = store.NewMongoStore(config)
case "mem":
diff --git a/cmd/dbinit/dbinit.go b/cmd/dbinit/dbinit.go
index bfe37c3..1183452 100644
--- a/cmd/dbinit/dbinit.go
+++ b/cmd/dbinit/dbinit.go
@@ -10,6 +10,7 @@ import (
mgo "gopkg.in/mgo.v2"
"github.com/go-sql-driver/mysql"
+ _ "github.com/mattn/go-sqlite3"
)
var (
@@ -18,18 +19,13 @@ var (
adminPasswd = flag.String("admin_password", "", "Admin password")
dbUser = flag.String("db_user", "user", "Database user")
dbPasswd = flag.String("db_password", "passwd", "Admin password")
- dbType = flag.String("db_type", "mysql", "Database engine (\"mysql\" or \"mongo\")")
+ dbType = flag.String("db_type", "mysql", "Database engine (\"mysql\", \"sqlite\" or \"mongo\")")
+ sqliteDB = flag.String("db_path", "cashier.db", "Path to SQLite database")
authDB = flag.String("authdb", "admin", "Admin database (mongo)")
certsDB = "certs"
issuedTable = "issued_certs"
-)
-
-func initMySQL() {
- var createTableStmt = []string{
- `CREATE DATABASE IF NOT EXISTS ` + certsDB + ` DEFAULT CHARACTER SET = 'utf8' DEFAULT COLLATE 'utf8_general_ci';`,
- `USE ` + certsDB + `;`,
- `CREATE TABLE IF NOT EXISTS ` + issuedTable + ` (
+ createTable = `CREATE TABLE IF NOT EXISTS ` + issuedTable + ` (
key_id VARCHAR(255) NOT NULL,
principals VARCHAR(255) NULL,
created_at DATETIME NULL,
@@ -37,7 +33,26 @@ func initMySQL() {
revoked BOOLEAN DEFAULT NULL,
raw_key TEXT NULL,
PRIMARY KEY (key_id)
- );`,
+ );`
+)
+
+func initSQLite() {
+ db, err := sql.Open("sqlite3", *sqliteDB)
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer db.Close()
+
+ if _, err = db.Exec(createTable); err != nil {
+ log.Fatal(err)
+ }
+}
+
+func initMySQL() {
+ var createTableStmt = []string{
+ `CREATE DATABASE IF NOT EXISTS ` + certsDB + ` DEFAULT CHARACTER SET = 'utf8' DEFAULT COLLATE 'utf8_general_ci';`,
+ `USE ` + certsDB + `;`,
+ createTable,
`GRANT ALL PRIVILEGES ON certs.* TO '` + *dbUser + `'@'%' IDENTIFIED BY '` + *dbPasswd + `';`,
}
@@ -103,6 +118,8 @@ func main() {
initMySQL()
case "mongo":
initMongo()
+ case "sqlite":
+ initSQLite()
default:
log.Fatalf("Invalid database type")
}