-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
133 lines (110 loc) · 3.02 KB
/
main.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"github.com/cockroachdb/cockroach-go/v2/crdb"
_ "github.com/lib/pq"
)
const (
cockroachDBConnString = "postgresql://root@crdb-cockroachdb-1:26257/pssql?sslmode=disable"
postgreSQLConnString = "postgresql://animesh:[email protected]:26257/pssql?sslmode=verify-full"
)
type Item struct {
ID int
Name string
Description string
}
func main() {
http.HandleFunc("/cockroachdb/create", createItemCockroachDB)
http.HandleFunc("/cockroachdb/read", readItemsCockroachDB)
http.HandleFunc("/postgresql/create", createItemPostgreSQL)
http.HandleFunc("/postgresql/read", readItemsPostgreSQL)
fmt.Println("Server is running on :8080...")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func createItemCockroachDB(w http.ResponseWriter, r *http.Request) {
db, err := sql.Open("postgres", cockroachDBConnString)
fmt.Println(cockroachDBConnString)
if err != nil {
log.Fatal(err)
}
defer db.Close()
item := Item{
Name: "Item 1",
Description: "Description of Item 1",
}
err = crdb.ExecuteTx(r.Context(), db, nil, func(tx *sql.Tx) error {
_, err := tx.Exec("INSERT INTO items (name, description) VALUES ($1, $2)", item.Name, item.Description)
return err
})
if err != nil {
log.Fatal(err)
}
w.Write([]byte("Item created in CockroachDB"))
}
func readItemsCockroachDB(w http.ResponseWriter, r *http.Request) {
db, err := sql.Open("postgres", cockroachDBConnString)
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query("SELECT id, name, description FROM items")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var items []Item
for rows.Next() {
var item Item
err := rows.Scan(&item.ID, &item.Name, &item.Description)
if err != nil {
log.Fatal(err)
}
items = append(items, item)
}
for _, item := range items {
fmt.Fprintf(w, "ID: %d, Name: %s, Description: %s\n", item.ID, item.Name, item.Description)
}
}
func createItemPostgreSQL(w http.ResponseWriter, r *http.Request) {
db, err := sql.Open("postgres", postgreSQLConnString)
if err != nil {
log.Fatal(err)
}
defer db.Close()
item := Item{
Name: "Item 2",
Description: "Description of Item 2",
}
_, err = db.Exec("INSERT INTO items (name, description) VALUES ($1, $2)", item.Name, item.Description)
if err != nil {
log.Fatal(err)
}
w.Write([]byte("Item created in PostgreSQL"))
}
func readItemsPostgreSQL(w http.ResponseWriter, r *http.Request) {
db, err := sql.Open("postgres", postgreSQLConnString)
if err != nil {
log.Fatal(err)
}
defer db.Close()
rows, err := db.Query("SELECT id, name, description FROM items")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var items []Item
for rows.Next() {
var item Item
err := rows.Scan(&item.ID, &item.Name, &item.Description)
if err != nil {
log.Fatal(err)
}
items = append(items, item)
}
for _, item := range items {
fmt.Fprintf(w, "ID: %d, Name: %s, Description: %s\n", item.ID, item.Name, item.Description)
}
}