-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
266 lines (239 loc) · 6.72 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"encoding/json"
"fmt"
"os"
"log"
"net/http"
"os/signal"
"time"
"context"
"syscall"
"github.com/gocql/gocql"
"github.com/gorilla/mux"
"github.com/kelseyhightower/envconfig"
)
type Albums struct {
Name string `json:"name"`
Image []Image `json:"image"`
}
type Image struct {
Name string `json:"name"`
}
type Conf struct {
DbHost string `envconfig:"DB_HOST"`
}
var albums []Albums
var cluster *gocql.ClusterConfig
func init() {
db := &Conf{}
err := envconfig.Process("", db)
if err != nil {
fmt.Println("Error in envconfig")
log.Fatal(err.Error())
}
fmt.Println("DB HOST", db.DbHost)
cluster = gocql.NewCluster(db.DbHost)
cluster.Keyspace = "system"
cluster.Timeout = time.Second * 20
cluster.ConnectTimeout = time.Second * 20
//cluster.DisableInitialHostLookup = true
Session, err := cluster.CreateSession()
defer Session.Close()
if err != nil {
fmt.Println("Create session failed")
panic(err)
}
fmt.Println("Cassandra init done")
//Create Keyspace
if err := Session.Query(`CREATE KEYSPACE IF NOT EXISTS albumspace WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };`).Exec(); err != nil {
fmt.Println(err)
} else {
fmt.Println("Keyspace created")
}
cluster.Keyspace = "albumspace"
session, err := cluster.CreateSession()
defer session.Close()
if err != nil {
log.Fatal("createSession:", err)
}
//Create Table albumtable
if err := session.Query(`CREATE TABLE IF NOT EXISTS albumtable(albname TEXT PRIMARY KEY, imagelist LIST<TEXT>);`).Exec(); err != nil {
fmt.Println(err)
} else {
fmt.Println("Table albumtable created")
}
}
//Show all albums
func showAlbum(w http.ResponseWriter, r *http.Request) {
Session, err := cluster.CreateSession()
defer Session.Close()
if err != nil {
fmt.Println("Create session failed")
panic(err)
}
fmt.Fprintf(w, "Displaying album names:\n")
//CQL Operation
iter := Session.Query("SELECT albname FROM albumtable;").Iter()
var data string
for iter.Scan(&data) {
json.NewEncoder(w).Encode(data)
}
if err := iter.Close(); err != nil {
log.Fatal(err)
}
}
//Create a new album
func addAlbum(w http.ResponseWriter, r *http.Request) {
Session, err := cluster.CreateSession()
defer Session.Close()
if err != nil {
fmt.Println("Create session failed")
panic(err)
}
w.Header().Set("Content-Type", "application/json")
param := mux.Vars(r)
if err := Session.Query(`INSERT INTO albumtable (albname) VALUES (?) IF NOT EXISTS;`, param["album"]).Exec(); err != nil {
fmt.Println(err)
} else {
fmt.Fprintf(w, "New album added")
}
}
//Delete an existing album
func deleteAlbum(w http.ResponseWriter, r *http.Request) {
Session, err := cluster.CreateSession()
defer Session.Close()
if err != nil {
fmt.Println("Create session failed")
panic(err)
}
w.Header().Set("Content-Type", "application/json")
param := mux.Vars(r)
//CQL Operation
if err := Session.Query(`DELETE FROM albumtable WHERE albname=? IF EXISTS;`, param["album"]).Exec(); err != nil {
fmt.Println(err)
} else {
fmt.Fprintf(w, "Album deleted")
}
}
//Show all images in an album
func showImagesInAlbum(w http.ResponseWriter, r *http.Request) {
Session, err := cluster.CreateSession()
defer Session.Close()
if err != nil {
fmt.Println("Create session failed")
panic(err)
}
w.Header().Set("Content-Type", "application/json")
param := mux.Vars(r)
iter := Session.Query("SELECT imagelist FROM albumtable WHERE albname=?;", param["album"]).Iter()
var data []string
for iter.Scan(&data) {
json.NewEncoder(w).Encode(data)
}
if err := iter.Close(); err != nil {
log.Fatal(err)
}
}
//Show a particular image inside an album
func showImage(w http.ResponseWriter, r *http.Request) {
Session, err := cluster.CreateSession()
defer Session.Close()
if err != nil {
fmt.Println("Create session failed")
panic(err)
}
w.Header().Set("Content-Type", "application/json")
param := mux.Vars(r)
iter := Session.Query("SELECT imagelist FROM albumtable WHERE albname='?';", param["image"]).Iter()
var data []string
for iter.Scan(&data) {
for _, img := range data {
if img == param["image"] {
json.NewEncoder(w).Encode(img)
}
}
}
if err := iter.Close(); err != nil {
log.Fatal(err)
}
}
//Create an image in an album
func addImage(w http.ResponseWriter, r *http.Request) {
Session, err := cluster.CreateSession()
defer Session.Close()
if err != nil {
fmt.Println("Create session failed")
panic(err)
}
w.Header().Set("Content-Type", "application/json")
param := mux.Vars(r)
//CQL Operation
if err := Session.Query(`UPDATE albumtable SET imagelist=imagelist+ ? WHERE albname=?;`, []string{param["image"]}, param["album"]).Exec(); err != nil {
fmt.Println(err)
} else {
fmt.Fprintf(w, "New image added")
}
}
//Delete an image in an album
func deleteImage(w http.ResponseWriter, r *http.Request) {
Session, err := cluster.CreateSession()
defer Session.Close()
if err != nil {
fmt.Println("Create session failed")
panic(err)
}
w.Header().Set("Content-Type", "application/json")
param := mux.Vars(r)
//CQL Operation
if err := Session.Query(`UPDATE albumtable SET imagelist=imagelist-? WHERE albname=?;`, []string{param["image"]}, param["album"]).Exec(); err != nil {
fmt.Println(err)
} else {
fmt.Fprintf(w, "Image deleted")
}
}
func main() {
//Initialize Router
myRouter := mux.NewRouter().StrictSlash(true)
//Show all albums
myRouter.HandleFunc("/", showAlbum).Methods(http.MethodGet)
//Create a new album
myRouter.HandleFunc("/{album}", addAlbum).Methods(http.MethodPost)
//Delete an existing album
myRouter.HandleFunc("/{album}", deleteAlbum).Methods(http.MethodDelete)
//Show all images in an album
myRouter.HandleFunc("/{album}", showImagesInAlbum).Methods(http.MethodGet)
//Show a particular image inside an album
myRouter.HandleFunc("/{album}/{image}", showImage).Methods(http.MethodGet)
//Create an image in an album
myRouter.HandleFunc("/{album}/{image}", addImage).Methods(http.MethodPost)
//Delete an image in an album
myRouter.HandleFunc("/{album}/{image}", deleteImage).Methods(http.MethodDelete)
srv := &http.Server{
Handler: myRouter,
Addr: ":5000",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
// Start Server
go func() {
log.Println("Starting Server")
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
// Graceful Shutdown
waitForShutdown(srv)
}
func waitForShutdown(srv *http.Server) {
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
// Block until we receive our signal.
<-interruptChan
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
srv.Shutdown(ctx)
log.Println("Shutting down")
os.Exit(0)
}