projects
/
brackets.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Close over Env struct
[brackets.git]
/
main.go
diff --git
a/main.go
b/main.go
index
94fbd81
..
f0fb3ac
100644
(file)
--- a/
main.go
+++ b/
main.go
@@
-1,8
+1,7
@@
package main
import (
package main
import (
- "git.jacobcasper.com/brackets/client"
- "git.jacobcasper.com/brackets/db"
+ "git.jacobcasper.com/brackets/env"
_ "github.com/mattn/go-sqlite3"
"github.com/zmb3/spotify"
"log"
_ "github.com/mattn/go-sqlite3"
"github.com/zmb3/spotify"
"log"
@@
-12,25
+11,20
@@
import (
type handler func(http.ResponseWriter, *http.Request)
func main() {
type handler func(http.ResponseWriter, *http.Request)
func main() {
-
lockedDb, err := db
.New()
+
env, err := env
.New()
if err != nil {
if err != nil {
- log.Fatal("Could not open db: ", err.Error())
- }
-
- client, err := client.Get()
- if err != nil {
- log.Fatal("Could not get client: ", err.Error())
+ log.Fatal("Could not set up Env: ", err.Error())
}
http.HandleFunc(
"/artist/add",
}
http.HandleFunc(
"/artist/add",
- a
ddArtistHandler(lockedDb, client
),
+ a
rtistAddHandler(env
),
)
log.Fatal(http.ListenAndServe(":8080", nil))
}
)
log.Fatal(http.ListenAndServe(":8080", nil))
}
-func a
ddArtistHandler(db *db.DB, c *spotify.Client
) handler {
+func a
rtistAddHandler(env *env.Env
) handler {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
@@
-45,19
+39,19
@@
func addArtistHandler(db *db.DB, c *spotify.Client) handler {
return
}
return
}
- artist, err :=
c
.GetArtist(spotify.ID(artistId))
+ artist, err :=
env.C
.GetArtist(spotify.ID(artistId))
if err != nil {
log.Printf("Failed to retrieve artist %s: %s", artistId, err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if err != nil {
log.Printf("Failed to retrieve artist %s: %s", artistId, err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
-
d
b.Mu.Lock()
- defer
d
b.Mu.Unlock()
-
d
b.Db.Exec("INSERT INTO ARTIST (ID, NAME) VALUES (?, ?)", artist.ID, artist.Name)
+
env.D
b.Mu.Lock()
+ defer
env.D
b.Mu.Unlock()
+
env.D
b.Db.Exec("INSERT INTO ARTIST (ID, NAME) VALUES (?, ?)", artist.ID, artist.Name)
for _, genre := range artist.Genres {
for _, genre := range artist.Genres {
- result, err :=
d
b.Db.Exec("REPLACE INTO GENRE (NAME) VALUES (?)", genre)
+ result, err :=
env.D
b.Db.Exec("REPLACE INTO GENRE (NAME) VALUES (?)", genre)
if err != nil {
log.Printf("Failed to insert genre %s: %s", genre, err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
if err != nil {
log.Printf("Failed to insert genre %s: %s", genre, err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
@@
-71,7
+65,7
@@
func addArtistHandler(db *db.DB, c *spotify.Client) handler {
return
}
return
}
-
d
b.Db.Exec("INSERT INTO ARTIST_GENRE_XREF (ARTIST_ID, GENRE_ID) VALUES (?, ?)", artist.ID, genreId)
+
env.D
b.Db.Exec("INSERT INTO ARTIST_GENRE_XREF (ARTIST_ID, GENRE_ID) VALUES (?, ?)", artist.ID, genreId)
}
w.WriteHeader(http.StatusCreated)
}
}
w.WriteHeader(http.StatusCreated)
}