Skip to content

Commit

Permalink
Merge pull request #1 from zembrodt/develop
Browse files Browse the repository at this point in the history
Rename app to Showtunes API
  • Loading branch information
zembrodt authored Dec 9, 2021
2 parents 01a37f1 + 88dd172 commit a58e287
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 35 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Music Display API
# Showtunes API

Go server used by the Music Display app to make API requests where a client secret is needed.
Go server used by the Showtunes app to make API requests where a client secret is needed.

Server is deployed to https://music-display-api.herokuapp.com/ for development purposes.
## Requests
* */v1/auth/tokens* (**POST**): Retrieve a Spotify auth token
* Expects *code* and *redirect_uri*
* Returns a new auth token
* */v1/auth/tokens* (**PUT**): Refresh a Spotify auth token
* Expects refresh token as *code*
* Returns an updated auth token
* */v1/color* (**GET**): Retrieve the dominant color of a given album's cover art
* Expects *url* - must be to an image hosted by Spotify (*i.scdn.co*)
* Returns the dominant color in hex
8 changes: 4 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"flag"
"fmt"
"github.com/spf13/viper"
musicapi "github.com/zembrodt/music-display-api"
"github.com/zembrodt/music-display-api/controller"
"github.com/zembrodt/music-display-api/util/global"
"github.com/zembrodt/showtunes-api"
"github.com/zembrodt/showtunes-api/controller"
"github.com/zembrodt/showtunes-api/util/global"
"log"
"os"
"path/filepath"
Expand All @@ -30,7 +30,7 @@ func main() {
flag.Parse()

if displayVersion {
fmt.Printf("%s v%s\n",musicapi.Name, musicapi.Version)
fmt.Printf("%s v%s\n", showtunes.Name, showtunes.Version)
os.Exit(0)
}

Expand Down
6 changes: 3 additions & 3 deletions controller/auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ var scopes = []string{
"user-modify-playback-state",
}

func (c *MusicAPIController) createAuthHandlers() {
func (c *ShowtunesAPIController) createAuthHandlers() {
c.handleFunc(pathToken, c.getAuthTokens, http.MethodPost)
c.handleFunc(pathToken, c.updateAuthToken, http.MethodPut)
}

func (c *MusicAPIController) getAuthTokens(w http.ResponseWriter, r *http.Request) {
func (c *ShowtunesAPIController) getAuthTokens(w http.ResponseWriter, r *http.Request) {
// Get request parameters
code := r.FormValue(codeKey)
redirectUri := r.FormValue(redirectUriKey)
Expand All @@ -48,7 +48,7 @@ func (c *MusicAPIController) getAuthTokens(w http.ResponseWriter, r *http.Reques
respondWithJSON(w, http.StatusOK, token)
}

func (c *MusicAPIController) updateAuthToken(w http.ResponseWriter, r *http.Request) {
func (c *ShowtunesAPIController) updateAuthToken(w http.ResponseWriter, r *http.Request) {
// Get request parameters
code := r.FormValue(codeKey)

Expand Down
4 changes: 2 additions & 2 deletions controller/color_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ func validDomains() map[string]bool {
}
}

func (c *MusicAPIController) createColorHandlers() {
func (c *ShowtunesAPIController) createColorHandlers() {
c.handleFunc(pathColor, c.getDominateColor, http.MethodGet)
}

func (c *MusicAPIController) getDominateColor(w http.ResponseWriter, r *http.Request) {
func (c *ShowtunesAPIController) getDominateColor(w http.ResponseWriter, r *http.Request) {
urls, ok := r.URL.Query()[urlKey]
if !ok || len(urls[0]) < 1 {
respondWithError(w, http.StatusBadRequest, "Request does not contain id")
Expand Down
28 changes: 15 additions & 13 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
musicapi "github.com/zembrodt/music-display-api"
"github.com/zembrodt/showtunes-api"
"golang.org/x/oauth2"
"golang.org/x/oauth2/spotify"
"log"
Expand All @@ -24,6 +24,7 @@ type httpResponse struct {

type pingResponse struct {
Response httpResponse `json:"response"`
Name string `json:"name"`
Version string `json:"version"`
ApiRoot string `json:"apiRoot"`
}
Expand All @@ -47,7 +48,7 @@ const (
pingPath = "/ping"
)

type MusicAPIController struct {
type ShowtunesAPIController struct {
router *mux.Router
routerApi *mux.Router
resourcesPath string
Expand All @@ -56,10 +57,10 @@ type MusicAPIController struct {
conf *oauth2.Config
}

func New(clientId, clientSecret string) *MusicAPIController {
func New(clientId, clientSecret string) *ShowtunesAPIController {
r := mux.NewRouter()
rApi := r.PathPrefix(musicapi.APIRoot).Subrouter()
controller := &MusicAPIController{
rApi := r.PathPrefix(showtunes.APIRoot).Subrouter()
controller := &ShowtunesAPIController{
router: r,
routerApi: rApi,
clientId: clientId,
Expand All @@ -86,7 +87,7 @@ func New(clientId, clientSecret string) *MusicAPIController {
return controller
}

func (c *MusicAPIController) Start(address string, port int) {
func (c *ShowtunesAPIController) Start(address string, port int) {
srv := &http.Server{
Addr: fmt.Sprintf("%s:%d", address, port),
WriteTimeout: time.Second * 15,
Expand Down Expand Up @@ -121,35 +122,36 @@ func (c *MusicAPIController) Start(address string, port int) {
os.Exit(0)
}

func (c *MusicAPIController) createGeneralHandlers() {
func (c *ShowtunesAPIController) createGeneralHandlers() {
c.handleGeneralFunc(pingPath, c.ping, http.MethodGet)
}

func (c *MusicAPIController) ping(w http.ResponseWriter, r *http.Request) {
func (c *ShowtunesAPIController) ping(w http.ResponseWriter, r *http.Request) {
respondWithJSON(w, http.StatusOK, pingResponse{
Response: httpResponse{
Code: http.StatusOK,
Message: jsonKeySuccess,
},
Version: musicapi.Version,
ApiRoot: musicapi.APIRoot,
Name: showtunes.Name,
Version: showtunes.Version,
ApiRoot: showtunes.APIRoot,
})
}

// Wrapper to call HandleFunc on the Mux securedRouter and track API endpoints
// Defaults to use all security middleware
// Used for all routes that are prepended with API root
func (c *MusicAPIController) handleFunc(path string, f http.HandlerFunc, methods ...string) {
func (c *ShowtunesAPIController) handleFunc(path string, f http.HandlerFunc, methods ...string) {
c.handleFuncRouter(path, f, c.routerApi, methods...)
}

// Wrapper used for all paths beginning at root
func (c *MusicAPIController) handleGeneralFunc(path string, f http.HandlerFunc, methods ...string) {
func (c *ShowtunesAPIController) handleGeneralFunc(path string, f http.HandlerFunc, methods ...string) {
c.handleFuncRouter(path, f, c.router, methods...)
}

// Wrapper for mux.Router.HandleFunc
func (c *MusicAPIController) handleFuncRouter(path string, f http.HandlerFunc, r *mux.Router, methods ...string) {
func (c *ShowtunesAPIController) handleFuncRouter(path string, f http.HandlerFunc, r *mux.Router, methods ...string) {
methods = append(methods, http.MethodOptions)
r.HandleFunc(path, f).Methods(methods...)
}
Expand Down
2 changes: 1 addition & 1 deletion controller/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"
"github.com/spf13/viper"
"github.com/zembrodt/music-display-api/util/global"
"github.com/zembrodt/showtunes-api/util/global"
"log"
"net/http"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/zembrodt/music-display-api
module github.com/zembrodt/showtunes-api

go 1.16

Expand Down
8 changes: 4 additions & 4 deletions properties.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package musicapi
package showtunes

const (
Name = "Music Display API"
Version = "0.1.1"
APIRoot = "/api"
Name = "Showtunes API"
Version = "1.0.0"
APIRoot = "/v1"
)
4 changes: 2 additions & 2 deletions resources/scripts/build.bat
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
@echo off
setlocal enabledelayedexpansion
rem Generate and build the Music Display API server code and executable
rem Generate and build the Showtunes API server code and executable

rem Set executable name
set execname=musicapi.exe
set execname=showtunesapi.exe

rem Set absolute project path
for %%a in ("%~dp0\.") do for %%b in ("%%~dpa\.") do for %%c in ("%%~dpb\.") do (
Expand Down
4 changes: 2 additions & 2 deletions resources/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash
# Generate and build the Music Display API server code and executable
# Generate and build the Showtunes API server code and executable

# Set executable name
execname=musicapi
execname=showtunesapi

# Set absolute project path
projectpath="$(dirname "$( cd ../"$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )")"
Expand Down

0 comments on commit a58e287

Please sign in to comment.