Skip to content

Commit f7334fa

Browse files
authored
Create msova_server.go
1 parent bd36703 commit f7334fa

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

msova_server.go

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/aws/session"
9+
"github.com/aws/aws-sdk-go/service/dynamodb"
10+
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
11+
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
12+
"github.com/gorilla/mux"
13+
"net/http"
14+
"os"
15+
"strconv"
16+
)
17+
18+
type Artist struct {
19+
Name string `json:"name"`
20+
HREF string `json:"href"`
21+
ID string `json:"id"`
22+
}
23+
24+
type Album struct {
25+
Name string `json:"name"`
26+
Artists []Artist `json:"artists"`
27+
ID string `json:"id"`
28+
HREF string `json:"href"`
29+
}
30+
31+
type Track struct {
32+
Name string `json:"name"`
33+
Artists []Artist `json:"artists"`
34+
Album Album `json:"album"`
35+
TrackNum int `json:"track_number"`
36+
ID string `json:"id"`
37+
HREF string `json:"href"`
38+
}
39+
40+
type Playlist struct {
41+
TableID string `json:"unique_id"`
42+
Name string `json:"name"`
43+
Description string `json:"description"`
44+
TrackList struct {
45+
Items []struct {
46+
Song Track `json:"track"`
47+
} `json:"items"`
48+
} `json:"tracks"`
49+
//TrackList Tracks `json:"tracks"`
50+
PlaylistID string `json:"id"`
51+
HREF string `json:"href"`
52+
Followers struct {
53+
Total int `json:"total"`
54+
} `json:"followers"`
55+
}
56+
57+
type Item struct {
58+
Playlists []Playlist `json:"items"`
59+
}
60+
61+
var svc *dynamodb.DynamoDB
62+
63+
func main() {
64+
//Router setup
65+
//Initialize
66+
router := mux.NewRouter()
67+
//Route handlers (endpoints)
68+
router.HandleFunc("/msova/all", getAll).Methods("GET")
69+
router.HandleFunc("/msova/status", getStatus).Methods("GET")
70+
//Create a DynamoDB session
71+
sess, err := session.NewSession(&aws.Config{
72+
Region: aws.String("us-east-1")},
73+
)
74+
if err != nil {
75+
fmt.Println("Error creating session:")
76+
fmt.Println(err.Error())
77+
os.Exit(1)
78+
}
79+
// Create DynamoDB client
80+
svc = dynamodb.New(sess)
81+
//Run server
82+
if err := http.ListenAndServe(":8080", router); err != nil {
83+
fmt.Println(err)
84+
}
85+
}
86+
87+
//Gets JSON representation of the entire table
88+
func getAll(w http.ResponseWriter, r *http.Request) {
89+
//Define what you want to get
90+
proj := expression.NamesList(expression.Name("name"),
91+
expression.Name("description"), expression.Name("followers"), expression.Name("tracks"),
92+
expression.Name("id"), expression.Name("href"), expression.Name("unique_id"))
93+
94+
expr, err := expression.NewBuilder().WithProjection(proj).Build()
95+
if err != nil {
96+
fmt.Println("Got error building expression:")
97+
fmt.Println(err.Error())
98+
os.Exit(1)
99+
}
100+
101+
params := &dynamodb.ScanInput{
102+
ExpressionAttributeNames: expr.Names(),
103+
ExpressionAttributeValues: expr.Values(),
104+
ProjectionExpression: expr.Projection(),
105+
TableName: aws.String("SpotifyGrab"),
106+
}
107+
108+
result, err := svc.Scan(params)
109+
if err != nil {
110+
fmt.Println(err.Error())
111+
return
112+
}
113+
114+
var playlists []Playlist
115+
err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &playlists)
116+
117+
ba, err := json.MarshalIndent(playlists, "", " ")
118+
w.Header().Set("Content-Type", "application/json")
119+
_, _ = w.Write(ba)
120+
}
121+
122+
//Gets status of the table
123+
func getStatus(w http.ResponseWriter, r *http.Request) {
124+
result, err := svc.Scan(&dynamodb.ScanInput{
125+
TableName: aws.String("SpotifyGrab"),
126+
})
127+
128+
if err != nil {
129+
fmt.Println(err.Error())
130+
return
131+
}
132+
133+
var playlists []Playlist
134+
err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &playlists)
135+
136+
//Create JSON-formatted string with table name and item count
137+
info := []byte(`{"table":"SpotifyGrab","recordCount": `)
138+
info = append(info, []byte(strconv.Itoa(len(playlists)))...)
139+
info = append(info, []byte(`}`)...)
140+
var prettyJson bytes.Buffer
141+
_ = json.Indent(&prettyJson, info, "", " ")
142+
143+
w.Header().Set("Content-Type", "application/json")
144+
_, _ = w.Write(prettyJson.Bytes())
145+
}

0 commit comments

Comments
 (0)