packing all in one
This commit is contained in:
parent
359ce4a67b
commit
bee162628f
2
makefile
2
makefile
@ -1,3 +1,3 @@
|
|||||||
dev:
|
dev:
|
||||||
docker-compose -f Docker/compose.yml up -d
|
docker-compose -f Docker/compose.yml up -d
|
||||||
go run pkg/API/main.go
|
go run pkg/main.go
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@ -50,17 +51,18 @@ func check(err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func opendb() {
|
func opendb() (db *sql.DB) {
|
||||||
// open db
|
// open db
|
||||||
sqlopen := fmt.Sprintf(dbUser + ":" + dbPass + "@tcp(" + dbHost + ":" + dbPort + ")/" + dbName)
|
sqlopen := fmt.Sprintf(dbUser + ":" + dbPass + "@tcp(" + dbHost + ":" + dbPort + ")/" + dbName)
|
||||||
var err error
|
var err error
|
||||||
db, err = sql.Open("mysql", sqlopen)
|
db, err = sql.Open("mysql", sqlopen)
|
||||||
check(err)
|
check(err)
|
||||||
fmt.Println("Successfully connected to the database")
|
fmt.Println("Successfully connected to the database")
|
||||||
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbinit(db *sql.DB) {
|
func dbinit() {
|
||||||
dbinit, err := db.Query("CREATE TABLE IF NOT EXISTS `media` (`id` INT NOT NULL AUTO_INCREMENT, `yt-id` VARCHAR(100), `url` VARCHAR(255), `localpath` TEXT(65535), `size` INT, `sha256sum` VARCHAR(255), `length` INT, `title` VARCHAR(255), `author` VARCHAR(255), PRIMARY KEY (`id`));")
|
dbinit, err := db.Query("CREATE TABLE IF NOT EXISTS `media` (`id` INT NOT NULL AUTO_INCREMENT, `ytid` VARCHAR(100), `url` VARCHAR(255), `localpath` TEXT(65535), `size` INT, `sha256sum` VARCHAR(255), `length` INT, `title` VARCHAR(255), `author` VARCHAR(255), PRIMARY KEY (`id`));")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error while creating table")
|
fmt.Println("Error while creating table")
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
@ -69,7 +71,7 @@ func dbinit(db *sql.DB) {
|
|||||||
fmt.Println("DB-Init successfull")
|
fmt.Println("DB-Init successfull")
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbentrycount(db *sql.DB) {
|
func dbentrycount() {
|
||||||
var count int
|
var count int
|
||||||
dbcount, err := db.Query("SELECT COUNT(*) FROM testdb.media;")
|
dbcount, err := db.Query("SELECT COUNT(*) FROM testdb.media;")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -85,11 +87,11 @@ func dbentrycount(db *sql.DB) {
|
|||||||
}
|
}
|
||||||
fmt.Printf("There are %v entries in the database\n", count)
|
fmt.Printf("There are %v entries in the database\n", count)
|
||||||
}
|
}
|
||||||
func getDB(c *gin.Context) {
|
func get_api_db(c *gin.Context) {
|
||||||
dbquery := fmt.Sprintf("SELECT * FROM testdb.media")
|
dbquery := fmt.Sprintf("SELECT * FROM testdb.media")
|
||||||
media, err := getJSON(dbquery)
|
media, err := getJSON(dbquery)
|
||||||
check(err)
|
check(err)
|
||||||
c.IndentedJSON(http.StatusOK, media)
|
c.Data(http.StatusOK, "application/json", media)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func getMedia() []media {
|
// func getMedia() []media {
|
||||||
@ -112,15 +114,13 @@ func getDB(c *gin.Context) {
|
|||||||
// return MediaDB
|
// return MediaDB
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func getJSON(sqlString string) (string, error) {
|
func getJSON(sqlString string) ([]byte, error) {
|
||||||
rows, err := db.Query(sqlString)
|
rows, err := db.Query(sqlString)
|
||||||
if err != nil {
|
check(err)
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
columns, err := rows.Columns()
|
columns, err := rows.Columns()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return []byte("Error while generating json"), err
|
||||||
}
|
}
|
||||||
count := len(columns)
|
count := len(columns)
|
||||||
tableData := make([]map[string]interface{}, 0)
|
tableData := make([]map[string]interface{}, 0)
|
||||||
@ -147,21 +147,60 @@ func getJSON(sqlString string) (string, error) {
|
|||||||
}
|
}
|
||||||
jsonData, err := json.Marshal(tableData)
|
jsonData, err := json.Marshal(tableData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return []byte("Error while generating json"), err
|
||||||
}
|
}
|
||||||
return string(jsonData), nil
|
return jsonData, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func get_test_ping(c *gin.Context) {
|
||||||
|
c.String(http.StatusOK, "pong")
|
||||||
|
}
|
||||||
|
func get_api_add_ytid(c *gin.Context) {
|
||||||
|
newSong_ytid := c.Param("ytid")
|
||||||
|
newSong_url := "https://www.youtube.com/watch?v=" + newSong_ytid
|
||||||
|
_, err := url.ParseRequestURI(newSong_url)
|
||||||
|
check(err)
|
||||||
|
is_available := ytid_in_db(newSong_ytid)
|
||||||
|
c.String(http.StatusOK, is_available)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ytid_in_db(ytid string) string {
|
||||||
|
sqlString := fmt.Sprintf("SELECT COUNT(ytid) FROM testdb.media WHERE ytid LIKE \"%v\";", ytid)
|
||||||
|
var count int
|
||||||
|
err := db.QueryRow(sqlString).Scan(&count)
|
||||||
|
check(err)
|
||||||
|
if count == 0 {
|
||||||
|
return "Song is not available"
|
||||||
|
}
|
||||||
|
if count == 1 {
|
||||||
|
return "Song is available"
|
||||||
|
}
|
||||||
|
error_return := fmt.Sprintf("Rows returned: %s --> there is an Issue within the database", string(count))
|
||||||
|
return error_return
|
||||||
|
}
|
||||||
|
|
||||||
|
func get_api_get_ytid(c *gin.Context) {
|
||||||
|
ytid := c.Param("ytid")
|
||||||
|
sqlString := fmt.Sprintf("SELECT * FROM testdb.media WHERE ytid LIKE \"%v\";", ytid)
|
||||||
|
json, err := getJSON(sqlString)
|
||||||
|
check(err)
|
||||||
|
c.Data(http.StatusOK, "application/json", json)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
readdbCred()
|
readdbCred()
|
||||||
opendb()
|
db = opendb()
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
dbinit(db)
|
dbinit()
|
||||||
dbentrycount(db)
|
dbentrycount()
|
||||||
|
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
router.GET("/api/db", getDB)
|
router.GET("/api/db", get_api_db)
|
||||||
|
router.GET("/api/add/:ytid", get_api_add_ytid)
|
||||||
|
router.GET("/api/get/:ytid", get_api_get_ytid)
|
||||||
|
//router.GET("/api/download/:ytid", get_api_download_ytid)
|
||||||
|
router.GET("/test/ping", get_test_ping)
|
||||||
//router.GET("/api/songs", getMedia)
|
//router.GET("/api/songs", getMedia)
|
||||||
router.Run("127.0.0.1:8000")
|
router.Run("127.0.0.1:8000")
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user