-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
44 lines (35 loc) · 797 Bytes
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"database/sql"
"fmt"
"log"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
)
type App struct {
Router *mux.Router
DB *sql.DB
}
func (app *App) Initialize(user, password, dbname string) {
connectionString :=
fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable", user, password, dbname)
var err error
app.DB, err = sql.Open("postgres", connectionString)
if err != nil {
log.Fatal(err)
}
if _, err := app.DB.Exec(tableCreationQuery); err != nil {
log.Fatal(err)
}
app.Router = mux.NewRouter()
}
func (app *App) Run(addr string) {}
const tableCreationQuery = `CREATE TABLE IF NOT EXISTS users
(
id SERIAL,
name TEXT NOT NULL,
company TEXT NOT NULL,
username TEXT NOT NULL,
password TEXT NOT NULL,
CONSTRAINT users_pkey PRIMARY KEY (id)
)`