168 lines
3.9 KiB
Go
168 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gorilla/mux"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
// Get the value of an Environment Variable
|
|
host := os.Getenv("DB_HOST")
|
|
dbPort, dbPortErr := strconv.Atoi(os.Getenv("DB_PORT"))
|
|
if dbPortErr != nil {
|
|
dbPort = 0
|
|
}
|
|
username := os.Getenv("DB_USERNAME")
|
|
password := os.Getenv("DB_PASSWORD")
|
|
dbName := os.Getenv("DB_NAME")
|
|
|
|
fmt.Printf("host: %s, dbPort: %v, username: %s, password: %s, dbName: %s\n",
|
|
host, dbPort, username, password, dbName)
|
|
|
|
if host == "" {
|
|
host = "localhost"
|
|
}
|
|
if dbPort == 0 {
|
|
dbPort = 5432
|
|
}
|
|
if username == "" {
|
|
username = "sebastian"
|
|
}
|
|
if password == "" {
|
|
password = "950100"
|
|
}
|
|
if dbName == "" {
|
|
dbName = "music_dev_local"
|
|
}
|
|
|
|
initDB(host, dbPort, username, password, dbName)
|
|
defer closeDb()
|
|
|
|
r := mux.NewRouter()
|
|
r.HandleFunc("/sync", syncHandler)
|
|
r.HandleFunc("/sync/{func}", syncHandler)
|
|
r.HandleFunc("/music", musicHandler)
|
|
r.HandleFunc("/music/{func}", musicHandler)
|
|
r.HandleFunc("/music/{func}/{func2}", musicHandler)
|
|
r.HandleFunc("/{func}", indexHandler)
|
|
http.Handle("/", r)
|
|
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "8080"
|
|
log.Printf("Defaulting to port %s", port)
|
|
}
|
|
|
|
log.Printf("Listening on port %s", port)
|
|
log.Printf("Open http://localhost:%s in the browser", port)
|
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
|
|
}
|
|
|
|
type VersionData struct {
|
|
Version string `json:"version"`
|
|
Changelog string `json:"changelog"`
|
|
History []VersionData `json:"history"`
|
|
}
|
|
|
|
type SongInfo struct {
|
|
Game string `json:"Game"`
|
|
GamePlayed int `json:"GamePlayed"`
|
|
Song string `json:"Song"`
|
|
SongPlayed int `json:"SongPlayed"`
|
|
CurrentlyPlaying bool `json:"CurrentlyPlaying"`
|
|
SongNo int `json:"SongNo"`
|
|
}
|
|
|
|
type GameData struct {
|
|
id int
|
|
gameName string
|
|
added time.Time
|
|
deleted time.Time
|
|
lastChanged time.Time
|
|
path string
|
|
timesPlayed int
|
|
lastPlayed time.Time
|
|
numberOfSongs int32
|
|
}
|
|
|
|
type SongData struct {
|
|
gameId int
|
|
songName string
|
|
path string
|
|
timesPlayed int
|
|
}
|
|
|
|
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|
(w).Header().Set("Access-Control-Allow-Origin", "*")
|
|
if r.URL.Path == "/version" {
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
testf()
|
|
|
|
data := VersionData{Version: "2.0.1",
|
|
Changelog: "Fixed CORS",
|
|
History: []VersionData{
|
|
{
|
|
Version: "2.0.0",
|
|
Changelog: "Rebuilt the application in Go.",
|
|
},
|
|
{
|
|
Version: "1.2.0",
|
|
Changelog: "Made the /sync endpoint async. " +
|
|
"Fixed bug where the game list wasn't reloaded when using /reset." +
|
|
"Fixed bug where the songNo showed in the list didn't match what should be sent.",
|
|
},
|
|
{
|
|
Version: "1.1.0",
|
|
Changelog: "Added sync endpoint, don't really trust it to 100%, would say beta. " +
|
|
"Fixed bug with /next after /previous. Added /reset endpoint. " +
|
|
"Added some info more to /info and /list.",
|
|
},
|
|
{
|
|
Version: "1.0.0",
|
|
Changelog: "Added swagger documentation. Created version 1.0.",
|
|
},
|
|
{
|
|
Version: "0.5.5",
|
|
Changelog: "Added increase played endpoint.",
|
|
},
|
|
},
|
|
}
|
|
_ = json.NewEncoder(w).Encode(data)
|
|
|
|
} else if r.URL.Path == "/doc" {
|
|
http.ServeFile(w, r, "./doc/swagger.yaml")
|
|
|
|
} else if r.URL.Path == "/" {
|
|
rows, dbErr := dbPool.Query(context.Background(), "select game_name from game")
|
|
if dbErr != nil {
|
|
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", dbErr)
|
|
os.Exit(1)
|
|
}
|
|
for rows.Next() {
|
|
var gameName string
|
|
dbErr = rows.Scan(&gameName)
|
|
if dbErr != nil {
|
|
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", dbErr)
|
|
}
|
|
_, _ = fmt.Fprintf(w, "%v\n", gameName)
|
|
}
|
|
|
|
_, err := fmt.Fprint(w, "Hello, World!!")
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
} else {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
}
|