1
0
Fork 0
forked from boyska/feedpanel
feedpanel/main.go
2018-09-26 16:53:06 +02:00

62 lines
1.3 KiB
Go

package main
import (
"fmt"
"os"
"github.com/go-pg/pg"
"github.com/pkg/errors"
)
type DB struct {
PgDB *pg.DB
Schema string
}
func (db *DB) Setup() error {
_, err := db.PgDB.Exec(`CREATE SCHEMA IF NOT EXISTS users`)
if err != nil {
return errors.Wrap(err, "Error creating schema")
}
fmt.Println("schema created")
_, 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")
}
fmt.Println("table created")
return nil
}
func (db *DB) UserAdd(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() {
pgdb := pg.Connect(&pg.Options{
User: "panel",
Password: "panelpass",
Database: "feeds",
})
db := DB{PgDB: pgdb}
err := db.Setup()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Println("All done")
os.Exit(0)
}