diff --git a/Makefile b/Makefile index ca3cd9b..9a3b799 100644 --- a/Makefile +++ b/Makefile @@ -12,5 +12,8 @@ build: frontend/build/... go build -tags=binassets -o backend/build/cmdui backend/main.go +create_db: + sqlite3 backend/data/cmdui.db < backend/schema_sqlite.sql + cloc: cloc --exclude-dir=vendor,build,node_modules . diff --git a/backend/.gitignore b/backend/.gitignore index ef79479..8458ced 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1,4 +1,5 @@ vendor build +data api/assets/bindata_assetfs.go config.toml diff --git a/backend/Gopkg.lock b/backend/Gopkg.lock index d59ce96..5d19908 100644 --- a/backend/Gopkg.lock +++ b/backend/Gopkg.lock @@ -43,6 +43,12 @@ revision = "8c199fb6259ffc1af525cc3ad52ee60ba8359669" version = "v1.1" +[[projects]] + name = "github.com/mattn/go-sqlite3" + packages = ["."] + revision = "5160b48509cf5c877bc22c11c373f8c7738cdb38" + version = "v1.3.0" + [[projects]] name = "github.com/satori/go.uuid" packages = ["."] @@ -55,6 +61,12 @@ packages = ["ssh/terminal"] revision = "2509b142fb2b797aa7587dad548f113b2c0f20ce" +[[projects]] + branch = "master" + name = "golang.org/x/net" + packages = ["context"] + revision = "01c190206fbdffa42f334f4b2bf2220f50e64920" + [[projects]] branch = "master" name = "golang.org/x/sys" @@ -64,6 +76,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "6c201fcf68612a8ad33e6e620125ca3a88e8e4c34d7acc2b5ed0f452c3457a3f" + inputs-digest = "4db9692ffa1d302e9cb342da138f0606a659403be6d1131f8689b874610a1eff" solver-name = "gps-cdcl" solver-version = 1 diff --git a/backend/config.example.toml b/backend/config.example.toml index 704b578..45d9480 100644 --- a/backend/config.example.toml +++ b/backend/config.example.toml @@ -1,12 +1,16 @@ -log_dir = "/var/log/commands" +log_dir = "data/logs" [commands] base_path = "/usr/local/bin/runner" config_command = "/usr/local/bin/runner_commands --export=json" [database] -driver = "mysql" -spec = "root:@(localhost:3306)/commands?charset=utf8mb4&parseTime=true" +driver = "sqlite3" +spec = "data/cmdui.db" + +# MySQL example +# driver = "mysql" +# spec = "root:@(localhost:3306)/commands?charset=utf8mb4&parseTime=true" [server] host = "http://127.0.0.1" diff --git a/backend/db/connection.go b/backend/db/connection.go index 15aa73b..33c64f8 100644 --- a/backend/db/connection.go +++ b/backend/db/connection.go @@ -6,6 +6,7 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" + _ "github.com/mattn/go-sqlite3" uuid "github.com/satori/go.uuid" "github.com/localhots/cmdui/backend/config" diff --git a/backend/db/job.go b/backend/db/job.go index dd1d4a1..85bd653 100644 --- a/backend/db/job.go +++ b/backend/db/job.go @@ -127,16 +127,9 @@ func (r *Job) Create() error { r.State = string(JobStateCreated) _, err := db.NamedExec(` - INSERT INTO jobs - SET - id = :id, - command = :command, - args = :args, - flags = :flags, - user_id = :user_id, - state = :state, - created_at = :created_at - `, r) + INSERT INTO jobs (id, command, args, flags, user_id, state, created_at) + VALUES (:id, :command, :args, :flags, :user_id, :state, :created_at) + `, r) if err != nil { return errors.Annotate(err, "Failed to create a job") } diff --git a/backend/db/session.go b/backend/db/session.go index e192bd7..1250512 100644 --- a/backend/db/session.go +++ b/backend/db/session.go @@ -42,13 +42,9 @@ func FindSession(id string) (*Session, error) { func (s Session) Create() error { _, err := db.NamedExec(` - INSERT INTO sessions - SET - id = :id, - user_id = :user_id, - created_at = :created_at, - expires_at = :expires_at - `, s) + INSERT INTO sessions (id, user_id, created_at, expires_at) + VALUES (:id, :user_id, :created_at, :expires_at) + `, s) if err != nil { return errors.Annotate(err, "Failed to create a session") } diff --git a/backend/db/user.go b/backend/db/user.go index 1cca323..5c2b046 100644 --- a/backend/db/user.go +++ b/backend/db/user.go @@ -71,13 +71,8 @@ func findUser(query string, args ...interface{}) (*User, error) { func (u User) Create() error { _, err := db.NamedExec(` - INSERT INTO users - SET - id = :id, - github_id = :github_id, - github_login = :github_login, - github_name = :github_name, - github_picture = :github_picture + INSERT INTO users (id, github_id, github_login, github_name, github_picture) + VALUES (:id, :github_id, :github_login, :github_name, :github_picture) `, u) if err != nil { return errors.Annotate(err, "Failed to create a user") diff --git a/schema.sql b/backend/schema_mysql.sql similarity index 79% rename from schema.sql rename to backend/schema_mysql.sql index a7b7a3c..c33c953 100644 --- a/schema.sql +++ b/backend/schema_mysql.sql @@ -1,6 +1,6 @@ CREATE TABLE `jobs` ( - `id` char(36) NOT NULL DEFAULT '', - `command` varchar(255) NOT NULL DEFAULT '', + `id` char(36) NOT NULL, + `command` varchar(255) NOT NULL, `args` text NOT NULL, `flags` text NOT NULL, `user_id` char(36) DEFAULT NULL, @@ -12,15 +12,15 @@ CREATE TABLE `jobs` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE `sessions` ( - `id` char(36) NOT NULL DEFAULT '', - `user_id` char(36) NOT NULL DEFAULT '', + `id` char(36) NOT NULL, + `user_id` char(36) NOT NULL, `created_at` datetime NOT NULL, `expires_at` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE `users` ( - `id` char(36) NOT NULL DEFAULT '', + `id` char(36) NOT NULL, `github_id` int(11) unsigned NOT NULL, `github_login` varchar(255) NOT NULL DEFAULT '', `github_name` varchar(255) NOT NULL DEFAULT '', diff --git a/backend/schema_sqlite.sql b/backend/schema_sqlite.sql new file mode 100644 index 0000000..b3507c2 --- /dev/null +++ b/backend/schema_sqlite.sql @@ -0,0 +1,29 @@ +CREATE TABLE `jobs` ( + `id` CHAR(36) NOT NULL PRIMARY KEY, + `command` VARCHAR(255) NOT NULL, + `args` TEXT NOT NULL, + `flags` TEXT NOT NULL, + `user_id` CHAR(36) DEFAULT NULL, + `state` VARCHAR(20) NOT NULL, + `created_at` DATETIME DEFAULT NULL, + `started_at` DATETIME DEFAULT NULL, + `finished_at` DATETIME DEFAULT NULL +); +-- PRAGMA table_info(jobs); + +CREATE TABLE `sessions` ( + `id` CHAR(36) NOT NULL PRIMARY KEY, + `user_id` CHAR(36) NOT NULL, + `created_at` DATETIME NOT NULL, + `expires_at` DATETIME NOT NULL +); +-- PRAGMA table_info(sessions); + +CREATE TABLE `users` ( + `id` CHAR(36) NOT NULL PRIMARY KEY, + `github_id` UNSIGNED BIGINT NOT NULL, + `github_login` VARCHAR(255) NOT NULL DEFAULT '', + `github_name` VARCHAR(255) NOT NULL DEFAULT '', + `github_picture` VARCHAR(255) NOT NULL DEFAULT '' +); +-- PRAGMA table_info(users);