-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.go
executable file
·115 lines (92 loc) · 2.02 KB
/
db.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"strings"
"time"
_ "github.com/lib/pq"
)
var (
db *sql.DB
)
type dbHandler struct{}
// Hoge do this.
func Hoge() {
fmt.Println("hoge")
}
func (h dbHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "DB Page\n\n")
// connects to db server and create dummytable if not exists
err := initDB()
if err != nil {
log.Fatal(err)
}
// check url has /db/add/*** format and if so, insert record to the dummytable
if strings.HasPrefix(r.URL.Path, "/db/add/") {
addnew := strings.TrimPrefix(r.URL.Path, "/db/add/")
if addnew != "" {
insertOne(addnew)
fmt.Fprintf(w, "New item inserted. \n\n")
}
}
fmt.Fprintf(w, "Access /db/add/{some text} to insert new item. \n\n")
fmt.Fprintf(w, "Items from dummy table...\n\n")
// list records from the dummytable
items := listitems()
for c, obkey := range items {
fmt.Fprintln(w, c+1, ". ", obkey)
}
}
func initDB() error {
connInfo := "user=postgres dbname=postgres password=mypass host=db port=5432 sslmode=disable"
var err error
db, err = sql.Open("postgres", connInfo)
if err != nil {
log.Fatal(err)
}
for i := 0; i < 5; i++ {
time.Sleep(time.Duration(i) * time.Second)
if err = db.Ping(); err == nil {
break
}
log.Println(err)
}
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(
`create table if not exists dummytable (
id SERIAL,
dummytext TEXT NOT NULL,
CONSTRAINT dummytable_pkey PRIMARY KEY (id)
)`)
return err
}
func listitems() []string {
rows, err := db.Query("select dummytext from dummytable")
if err != nil {
log.Fatal(err)
}
var items []string
for rows.Next() {
var dummytext string
err := rows.Scan(&dummytext)
if err != nil {
log.Fatal(err)
}
items = append(items, dummytext)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
return items
}
func insertOne(text string) {
r, err := db.Exec("insert into dummytable(dummytext) values('" + text + "')")
if err != nil {
log.Fatal(err)
}
log.Println(r)
}