From 47cf67c73544e4c4787b269d95f5a028077c0e30 Mon Sep 17 00:00:00 2001 From: boyska Date: Wed, 26 Sep 2018 15:59:50 +0200 Subject: [PATCH] initial commit, dont expect it to work --- .gitignore | 1 + go.mod | 12 +++++++++++ main.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..22d0d82 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4f3bd1b --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module panel + +require ( + github.com/go-pg/pg v6.15.0+incompatible + github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect + github.com/kr/pretty v0.1.0 // indirect + github.com/onsi/gomega v1.4.2 // indirect + github.com/pkg/errors v0.8.0 + github.com/vmihailenco/sasl v0.0.0-20180913092844-58bfd2104008 // indirect + golang.org/x/crypto v0.0.0-20180910181607-0e37d006457b // indirect + gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect +) diff --git a/main.go b/main.go new file mode 100644 index 0000000..02fd958 --- /dev/null +++ b/main.go @@ -0,0 +1,58 @@ +package main + +import ( + "fmt" + + "github.com/go-pg/pg" + "github.com/pkg/errors" +) + +type DB struct { + PgDB *pg.DB + Schema string +} + +func Setup(db *DB) error { + _, err := db.PgDB.Exec(`CREATE SCHEMA IF NOT EXISTS users`) + if err != nil { + return errors.Wrap(err, "Error creating schema") + } + + _, err = db.PgDB.Exec(`CREATE TABLE IF NOT EXISTS users.users ( + uid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + username varchar(64) UNIQUE NOT NULL, + email varchar(100) UNIQUE NOT NULL, + password varchar(256) NOT NULL + )`) + if err != nil { + return errors.Wrap(err, "Error creating users table") + } + return nil +} + +func UserAdd(db *DB, username, email, pwhash string) error { + stmt, err := db.PgDB.Prepare("INSERT INTO users.users VALUES ($1, $2, $3)") + if err != nil { + panic("Bad statement in UserAdd") + } + _, err = stmt.Exec(username, email, pwhash) + if err != nil { + return errors.Wrapf(err, "Error adding user `%s`", email) + } + return nil +} + +func main() { + fmt.Println("vim-go") + pgdb, err := pg.Connect(&pg.Options{ + User: "panel", + Password: "", + Database: "feeds", + }) + if err != nil { + os.Fprintln(os.Stderr, err) + os.Exit(1) + } + db := DB{PgDB: db} + db.Setup() +}