Initial Commit

This commit is contained in:
Daniel Mason 2021-03-23 21:43:44 +13:00
parent f5d766bffe
commit 7c9c23ac92
14 changed files with 831 additions and 1 deletions

View file

@ -0,0 +1,5 @@
package goscrobble
var (
DBVersion = 1
)

113
internal/goscrobble/db.go Normal file
View file

@ -0,0 +1,113 @@
package goscrobble
import (
"database/sql"
"fmt"
"log"
"os"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/golang-migrate/migrate"
"github.com/golang-migrate/migrate/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
type dbItem struct {
conn *sql.DB
version *int
}
var db dbItem
// InitDb - Boots up a DB connection
func InitDb() {
dbHost := os.Getenv("MYSQL_HOST")
dbUser := os.Getenv("MYSQL_USER")
dbPass := os.Getenv("MYSQL_PASS")
dbName := os.Getenv("MYSQL_DB")
dbConn, err := sql.Open("mysql", dbUser+":"+dbPass+"@tcp("+dbHost+")/"+dbName)
if err != nil {
panic(err)
}
dbConn.SetConnMaxLifetime(time.Minute * 3)
dbConn.SetMaxOpenConns(25)
dbConn.SetMaxIdleConns(10)
err = dbConn.Ping()
if err != nil {
panic(err)
}
var vers int = 0
db = dbItem{
conn: dbConn,
version: &vers,
}
if !checkIfDbUpToDate() {
fmt.Printf("Database not up to date.. triggering migrations!\n")
} else {
fmt.Printf("Database up to date!\n")
}
}
// CloseDbConn - Closes DB connection
func CloseDbConn() {
db.conn.Close()
}
// checkIfDbUpToDate - Checks if we need to run migrations
func checkIfDbUpToDate() bool {
fmt.Printf("Code version: %v. ", DBVersion)
dbVers := db.getDbVersion()
fmt.Printf("DB version: %v.\n", dbVers)
if dbVers == DBVersion {
return true
} else if dbVers > DBVersion {
panic("!!Warning!! Your database is newer than the code. Please update!")
}
return false
}
// getDbVersion - Gets version of schema or generate basic schema
func (db dbItem) getDbVersion() int {
stmtOut, err := db.conn.Prepare("SELECT version FROM goscrobble WHERE id = ? ")
defer stmtOut.Close()
if err != nil {
// We can assume this is a fresh database - Lets config it!
return runMigrations(DBVersion)
}
err = stmtOut.QueryRow(1).Scan(db.version)
if err != nil {
panic(err.Error())
}
return *db.version
}
func runMigrations(latestVersion int) int {
driver, err := mysql.WithInstance(db.conn, &mysql.Config{})
if err != nil {
log.Fatalf("Unable to run migrations! %v", err)
}
m, _ := migrate.NewWithDatabaseInstance(
"file:///migrations",
"mysql",
driver,
)
m.Steps(2)
return latestVersion
}

View file

@ -0,0 +1,6 @@
package goscrobble
// ParseJellyfinInput - Transform API data into a common struct
func ParseJellyfinInput(test string) string {
return test
}

View file

@ -0,0 +1,32 @@
package goscrobble
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
// HandleRequests - Boot HTTP server
func HandleRequests() {
// creates a new instance of a mux router
httpRouter := mux.NewRouter().StrictSlash(true)
// replace http.HandleFunc with myRouter.HandleFunc
httpRouter.HandleFunc("/", serveFrontend)
httpRouter.HandleFunc("/api/v1", serveEndpoint)
httpRouter.HandleFunc("/api/v1/scrobble/jellyfin", serveEndpoint)
httpRouter.HandleFunc("/api/v1/jellyfin", serveEndpoint)
// Serve HTTP Server
log.Fatal(http.ListenAndServe(":42069", httpRouter))
}
// serveFrontend - Handle / queries
func serveFrontend(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome!")
}
func serveEndpoint(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "{}")
}

View file

@ -0,0 +1 @@
package goscrobble

View file

@ -0,0 +1 @@
package goscrobble