forked from msbeethoven/la-radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (128 loc) · 3.9 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
package main
import (
"database/sql"
"log"
"net/http"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
// "timeconversion"
"fmt"
"radio/blah"
)
type Song struct {
SongId int `json:"song_id"`
Title string `json:"title"`
Artist string `json:"artist"`
Year int `json:"year"`
Album string `json:"album"`
Genre string `json:"genre"`
Duration time.Duration `json:"duration"`
AudioPathFile string `json:"audio_path_file"`
}
var db *sql.DB
func main() {
fmt.Println(blah.Okay) //you had this package in the blah.go file called "bleh" first and it needed to be bleh.Okay
var err error
// db, err = sql.Open("postgres", "postgres://postgres:postgres@localhost/radio?sslmode=disable")
db, err = sql.Open("postgres", "host=localhost port=5431 user=postgres dbname=radio sslmode=disable")
if err != nil {
log.Fatal(err)
}
router := gin.Default()
//CORS Exceptions
config := cors.DefaultConfig()
config.AllowOrigins = []string{"*"} // You can set this to the origins you want to allow, or use "*" to allow all origins.
config.AllowMethods = []string{"GET", "POST", "PUT", "DELETE"}
router.Use(cors.New(config))
// Router requests
router.GET("/songs", getSongs)
//router.POST("/songs", createTrack)
router.Run("localhost:8081")
}
func getSongs(c *gin.Context) {
c.Header("Content-Type", "application/json")
//Query database
rows, err := db.Query("SELECT song_id, title, artist, album, year FROM songs")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var songs []Song
for rows.Next() {
var s Song
//var durationString string
err := rows.Scan(&s.SongId, &s.Title, &s.Artist, &s.Album, &s.Year)
if err != nil {
log.Fatal(err)
}
// s.Duration, err = handlers.parseDurationFromHHMMSS(durationString)
// if err != nil {
// log.Fatal(err)
// }
songs = append(songs, s)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
c.IndentedJSON(http.StatusOK, songs)
}
// import (
// //"encoding/json"
// "fmt"
// "github.com/gorilla/mux"
// "github.com/jinzhu/gorm"
// //"github.com/jinzhu/gorm/dialects/postgres"
// _ "github.com/lib/pq"
// "log"
// "net/http"
// "time"
// )
// func GetAllSongs(w http.ResponseWriter, r *http.Request) {
// w.Header().Add("Content-Type", "application/json")
// w.WriteHeader(http.StatusOK)
// db, err := gorm.Open("postgres", "host=localhost port=8080 user=your-username dbname=radio sslmode=disable")
// if err != nil {
// panic(err)
// }
// defer db.Close()
// var songs []Song
// db.Find(&songs) //finding all, everything in db, hence plural
// for _, song := range songs {
// fmt.Printf("SongId: %d, Title: %s, Artist: %.2f\n", song.SongId, song.Title, song.Artist)
// }
// }
// func main() {
// router := mux.NewRouter()
// router.HandleFunc("/songs", GetAllSongs)
// // router.HandleFunc("/songs", func(w http.ResponseWriter, r *http.Request) {
// // json.NewEncoder(w).Encode("Hello songs")
// // })
// log.Println("Radio is starting!")
// http.ListenAndServe(":8080", router)
// }
// type Song struct {
// SongId int `json:"song_id"`
// Title string `json:"title"`
// Artist string `json:"artist"`
// Year int `json:"year"`
// Album string `json:"album"`
// Genre string `json:"genre"`
// Duration time.Duration `json:"duration"`
// AudioPathFile string `json:"audio_path_file"`
// }
// func main() {
// http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
// })
// }
// func main() {
// http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("Hello, World!"))
// })
// http.ListenAndServe(":8080", nil)
// r := routes.SetupRoutes()
// r.Run(":8080")
// }