-
Notifications
You must be signed in to change notification settings - Fork 0
/
from_db_to_web.go
90 lines (78 loc) · 1.71 KB
/
from_db_to_web.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"path"
"syscall"
"time"
"github.com/kirillrdy/web/admin"
"github.com/kirillrdy/web/db"
"github.com/kirillrdy/web/postgresql"
_ "github.com/lib/pq"
)
var tables = struct {
people db.Table
movies db.Table
}{
"people",
"movies",
}
var people = struct {
id db.Column
first_name db.Column
last_name db.Column
}{
tables.people.Column("id"),
tables.people.Column("first_name"),
tables.people.Column("last_name"),
}
var movies = struct {
id db.Column
title db.Column
year db.Column
created_at db.Column
}{
tables.movies.Column("id"),
tables.movies.Column("title"),
tables.movies.Column("year"),
tables.movies.Column("created_at"),
}
func crash(err error) {
if err != nil {
log.Panic(err)
}
}
func main() {
homedir, err := os.UserHomeDir()
crash(err)
dbDir := path.Join(homedir, "example_db")
dbName := "movies"
postgres := postgresql.Server{DBDir: dbDir}
if _, err := os.Stat(dbDir); os.IsNotExist(err) {
crash(postgres.InitDB())
crash(postgres.Start())
time.Sleep(time.Second)
crash(postgres.CreateDB(dbName))
crash(exec.Command("psql", "-d", dbName, "-f", "schema.sql").Run())
crash(postgres.Stop())
}
crash(postgres.Start())
defer postgres.Stop()
connection, err := postgres.Connect(dbName)
crash(err)
if true {
for i := 0; i < 1000; i++ {
_, err := db.Insert().Into(tables.movies, movies.title, movies.year).Values("Shawshank Redeption", 1994).Execute(connection)
crash(err)
}
}
movies := admin.Resource{Table: tables.movies}
admin.AddResource(connection, movies)
go http.ListenAndServe(":3000", nil)
ch := make(chan os.Signal, 10)
signal.Notify(ch, syscall.SIGINT)
<-ch
}