Changed the structure of the whole application, should be no changes to functionality.
This commit is contained in:
70
pkg/conf/conf.go
Normal file
70
pkg/conf/conf.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gorilla/mux"
|
||||
"log"
|
||||
"music-server/pkg/api"
|
||||
"music-server/pkg/db"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func SetupDb() {
|
||||
// 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"
|
||||
}
|
||||
|
||||
db.InitDB(host, dbPort, username, password, dbName)
|
||||
}
|
||||
|
||||
func CloseDb() {
|
||||
defer db.CloseDb()
|
||||
}
|
||||
|
||||
func SetupRestServer() {
|
||||
r := mux.NewRouter()
|
||||
r.HandleFunc("/sync", api.SyncHandler)
|
||||
r.HandleFunc("/sync/{func}", api.SyncHandler)
|
||||
r.HandleFunc("/music", api.MusicHandler)
|
||||
r.HandleFunc("/music/{func}", api.MusicHandler)
|
||||
r.HandleFunc("/music/{func}/{func2}", api.MusicHandler)
|
||||
r.HandleFunc("/{func}", api.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))
|
||||
}
|
||||
Reference in New Issue
Block a user