I did many things but I do not understand a single character
This commit is contained in:
		
							
								
								
									
										167
									
								
								pkg/API/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										167
									
								
								pkg/API/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,167 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"database/sql"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	"github.com/spf13/viper"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Global Variables
 | 
			
		||||
var db *sql.DB
 | 
			
		||||
var dbUser, dbPass, dbHost, dbPort, dbName string
 | 
			
		||||
 | 
			
		||||
type media struct {
 | 
			
		||||
	ID        string `json: "id"`
 | 
			
		||||
	YTID      string `json: "yt-id"`
 | 
			
		||||
	Url       string `json: "url"`
 | 
			
		||||
	Localpath string `json: "localpath"`
 | 
			
		||||
	Size      int    `json: "size"`
 | 
			
		||||
	Sha256sum string `json: "sha256sum"`
 | 
			
		||||
	Length    int    `json: "length"`
 | 
			
		||||
	Title     string `json: "title"`
 | 
			
		||||
	Author    string `json: "author"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// read db cred
 | 
			
		||||
func readdbCred() {
 | 
			
		||||
	dbCred := viper.New()
 | 
			
		||||
	dbCred.SetConfigName("db-cred")
 | 
			
		||||
	dbCred.AddConfigPath(".")
 | 
			
		||||
	dbCred.SetConfigType("yaml")
 | 
			
		||||
	err := dbCred.ReadInConfig()
 | 
			
		||||
	check(err)
 | 
			
		||||
	dbUser = dbCred.GetString("dbUser")
 | 
			
		||||
	dbPass = dbCred.GetString("dbPass")
 | 
			
		||||
	dbHost = dbCred.GetString("dbHost")
 | 
			
		||||
	dbPort = dbCred.GetString("dbPort")
 | 
			
		||||
	dbName = dbCred.GetString("dbName")
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// check error
 | 
			
		||||
func check(err error) {
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err.Error())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func opendb() {
 | 
			
		||||
	// open db
 | 
			
		||||
	sqlopen := fmt.Sprintf(dbUser + ":" + dbPass + "@tcp(" + dbHost + ":" + dbPort + ")/" + dbName)
 | 
			
		||||
	var err error
 | 
			
		||||
	db, err = sql.Open("mysql", sqlopen)
 | 
			
		||||
	check(err)
 | 
			
		||||
	fmt.Println("Successfully connected to the database")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func dbinit(db *sql.DB) {
 | 
			
		||||
	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`));")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("Error while creating table")
 | 
			
		||||
		panic(err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	defer dbinit.Close()
 | 
			
		||||
	fmt.Println("DB-Init successfull")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func dbentrycount(db *sql.DB) {
 | 
			
		||||
	var count int
 | 
			
		||||
	dbcount, err := db.Query("SELECT COUNT(*) FROM testdb.media;")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("Error while counting rows from table")
 | 
			
		||||
		panic(err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	defer dbcount.Close()
 | 
			
		||||
	for dbcount.Next() {
 | 
			
		||||
		err = dbcount.Scan(&count)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			panic(err.Error())
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Printf("There are %v entries in the database\n", count)
 | 
			
		||||
}
 | 
			
		||||
func getDB(c *gin.Context) {
 | 
			
		||||
	dbquery := fmt.Sprintf("SELECT * FROM testdb.media")
 | 
			
		||||
	media, err := getJSON(dbquery)
 | 
			
		||||
	check(err)
 | 
			
		||||
	c.IndentedJSON(http.StatusOK, media)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// func getMedia() []media {
 | 
			
		||||
// 	dbquery := fmt.Sprintf("SELECT * FROM testdb.media")
 | 
			
		||||
// 	rows, err := db.Query(dbquery)
 | 
			
		||||
// 	check(err)
 | 
			
		||||
// 	var MediaDB []media
 | 
			
		||||
// 	for rows.Next() {
 | 
			
		||||
// 		err := rows.Scan(&id, &yt-id, &url, &localpath, &size, &sha256sum, &length, &title, &author)
 | 
			
		||||
// 		if err != nil {
 | 
			
		||||
// 			if err == sql.ErrNoRows {
 | 
			
		||||
// 				log.Println("no rows found in DB")
 | 
			
		||||
// 			} else {
 | 
			
		||||
// 				panic(err)
 | 
			
		||||
// 			}
 | 
			
		||||
// 		} else {
 | 
			
		||||
// 			MediaDB = append(MediaDB, media{ID: id, YTID: yt - id, Url: url, Localpath: localpath, Size: size, Sha256sum: sha256sum, Length: length, Title: title, Author: author})
 | 
			
		||||
// 		}
 | 
			
		||||
// 	}
 | 
			
		||||
// 	return MediaDB
 | 
			
		||||
// }
 | 
			
		||||
 | 
			
		||||
func getJSON(sqlString string) (string, error) {
 | 
			
		||||
	rows, err := db.Query(sqlString)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	defer rows.Close()
 | 
			
		||||
	columns, err := rows.Columns()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	count := len(columns)
 | 
			
		||||
	tableData := make([]map[string]interface{}, 0)
 | 
			
		||||
	values := make([]interface{}, count)
 | 
			
		||||
	valuePtrs := make([]interface{}, count)
 | 
			
		||||
	for rows.Next() {
 | 
			
		||||
		for i := 0; i < count; i++ {
 | 
			
		||||
			valuePtrs[i] = &values[i]
 | 
			
		||||
		}
 | 
			
		||||
		rows.Scan(valuePtrs...)
 | 
			
		||||
		entry := make(map[string]interface{})
 | 
			
		||||
		for i, col := range columns {
 | 
			
		||||
			var v interface{}
 | 
			
		||||
			val := values[i]
 | 
			
		||||
			b, ok := val.([]byte)
 | 
			
		||||
			if ok {
 | 
			
		||||
				v = string(b)
 | 
			
		||||
			} else {
 | 
			
		||||
				v = val
 | 
			
		||||
			}
 | 
			
		||||
			entry[col] = v
 | 
			
		||||
		}
 | 
			
		||||
		tableData = append(tableData, entry)
 | 
			
		||||
	}
 | 
			
		||||
	jsonData, err := json.Marshal(tableData)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return "", err
 | 
			
		||||
	}
 | 
			
		||||
	return string(jsonData), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	readdbCred()
 | 
			
		||||
	opendb()
 | 
			
		||||
	defer db.Close()
 | 
			
		||||
 | 
			
		||||
	dbinit(db)
 | 
			
		||||
	dbentrycount(db)
 | 
			
		||||
 | 
			
		||||
	router := gin.Default()
 | 
			
		||||
	router.GET("/api/db", getDB)
 | 
			
		||||
	//router.GET("/api/songs", getMedia)
 | 
			
		||||
	router.Run("127.0.0.1:8000")
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										0
									
								
								pkg/FETCHER/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								pkg/FETCHER/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -1,65 +1,65 @@
 | 
			
		||||
// package main
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
// import (
 | 
			
		||||
// 	"errors"
 | 
			
		||||
// 	"net/http"
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
// 	"github.com/gin-gonic/gin"
 | 
			
		||||
// )
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// type media struct {
 | 
			
		||||
// 	ID     string `json: "id"`
 | 
			
		||||
// 	Url    string `json: "url"`
 | 
			
		||||
// 	Title  string `json: "title"`
 | 
			
		||||
// 	Author string `json: "author"`
 | 
			
		||||
// }
 | 
			
		||||
type media struct {
 | 
			
		||||
	ID     string `json: "id"`
 | 
			
		||||
	Url    string `json: "url"`
 | 
			
		||||
	Title  string `json: "title"`
 | 
			
		||||
	Author string `json: "author"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// var songs = []media{
 | 
			
		||||
// 	{ID: "1", Url: "https://www.youtube.com/watch?v=DmeUuoxyt_E", Title: "Rockstars", Author: "Nickelback"},
 | 
			
		||||
// 	{ID: "2", Url: "https://www.youtube.com/watch?v=u9Dg-g7t2l4", Title: "The Sound Of Silence", Author: "Disturbed"},
 | 
			
		||||
// 	{ID: "3", Url: "https://www.youtube.com/watch?v=uxUATkpMQ8A", Title: "Gives You Hell", Author: "The All-American Rejects"},
 | 
			
		||||
// }
 | 
			
		||||
var songs = []media{
 | 
			
		||||
	{ID: "1", Url: "https://www.youtube.com/watch?v=DmeUuoxyt_E", Title: "Rockstars", Author: "Nickelback"},
 | 
			
		||||
	{ID: "2", Url: "https://www.youtube.com/watch?v=u9Dg-g7t2l4", Title: "The Sound Of Silence", Author: "Disturbed"},
 | 
			
		||||
	{ID: "3", Url: "https://www.youtube.com/watch?v=uxUATkpMQ8A", Title: "Gives You Hell", Author: "The All-American Rejects"},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// func getMedia(c *gin.Context) {
 | 
			
		||||
// 	c.IndentedJSON(http.StatusOK, songs)
 | 
			
		||||
// }
 | 
			
		||||
func getMedia(c *gin.Context) {
 | 
			
		||||
	c.IndentedJSON(http.StatusOK, songs)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// func songByid(c *gin.Context) {
 | 
			
		||||
// 	id := c.Param("id")
 | 
			
		||||
// 	song, err := getMediaByid(id)
 | 
			
		||||
func songByid(c *gin.Context) {
 | 
			
		||||
	id := c.Param("id")
 | 
			
		||||
	song, err := getMediaByid(id)
 | 
			
		||||
 | 
			
		||||
// 	if err != nil {
 | 
			
		||||
// 		c.IndentedJSON(http.StatusNotFound, gin.H{"message": "Book not found."})
 | 
			
		||||
// 		return
 | 
			
		||||
// 	}
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.IndentedJSON(http.StatusNotFound, gin.H{"message": "Book not found."})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
// 	c.IndentedJSON(http.StatusOK, song)
 | 
			
		||||
// }
 | 
			
		||||
	c.IndentedJSON(http.StatusOK, song)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// func getMediaByid(id string) (*media, error) {
 | 
			
		||||
// 	for i, song := range songs {
 | 
			
		||||
// 		if song.ID == id {
 | 
			
		||||
// 			return &songs[i], nil
 | 
			
		||||
// 		}
 | 
			
		||||
// 	}
 | 
			
		||||
// 	return nil, errors.New("id not found")
 | 
			
		||||
// }
 | 
			
		||||
func getMediaByid(id string) (*media, error) {
 | 
			
		||||
	for i, song := range songs {
 | 
			
		||||
		if song.ID == id {
 | 
			
		||||
			return &songs[i], nil
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil, errors.New("id not found")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// func addMedia(c *gin.Context) {
 | 
			
		||||
// 	var newSong media
 | 
			
		||||
func addMedia(c *gin.Context) {
 | 
			
		||||
	var newSong media
 | 
			
		||||
 | 
			
		||||
// 	if err := c.BindJSON(&newSong); err != nil {
 | 
			
		||||
// 		return
 | 
			
		||||
// 	}
 | 
			
		||||
	if err := c.BindJSON(&newSong); err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
// 	songs = append(songs, newSong)
 | 
			
		||||
// 	c.IndentedJSON(http.StatusCreated, newSong)
 | 
			
		||||
// }
 | 
			
		||||
	songs = append(songs, newSong)
 | 
			
		||||
	c.IndentedJSON(http.StatusCreated, newSong)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// func main() {
 | 
			
		||||
// 	router := gin.Default()
 | 
			
		||||
// 	router.GET("/songs", getMedia)
 | 
			
		||||
// 	router.GET("/songs/:id", songByid)
 | 
			
		||||
// 	router.POST("/songs", addMedia)
 | 
			
		||||
// 	router.Run("127.0.0.1:8000")
 | 
			
		||||
// }
 | 
			
		||||
func main() {
 | 
			
		||||
	router := gin.Default()
 | 
			
		||||
	router.GET("/songs", getMedia)
 | 
			
		||||
	router.GET("/songs/:id", songByid)
 | 
			
		||||
	router.POST("/songs", addMedia)
 | 
			
		||||
	router.Run("127.0.0.1:8000")
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										49
									
								
								pkg/main.go
									
									
									
									
									
								
							
							
						
						
									
										49
									
								
								pkg/main.go
									
									
									
									
									
								
							@@ -1,49 +0,0 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"database/sql"
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func dbinit(db *sql.DB) {
 | 
			
		||||
	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, PRIMARY KEY (`id`));")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("Error while creating table")
 | 
			
		||||
		panic(err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	defer dbinit.Close()
 | 
			
		||||
	fmt.Println("DB-Init successfull")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func dbentrycount(db *sql.DB) {
 | 
			
		||||
	var count int
 | 
			
		||||
	dbcount, err := db.Query("SELECT COUNT(*) FROM testdb.media;")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("Error while counting rows from table")
 | 
			
		||||
		panic(err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	defer dbcount.Close()
 | 
			
		||||
	for dbcount.Next() {
 | 
			
		||||
		err = dbcount.Scan(&count)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			panic(err.Error())
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Printf("There are %v entries in the database\n", count)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	db, err := sql.Open("mysql", "username:password@tcp(127.0.0.1:3306)/testdb")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("Error while connecting with the database")
 | 
			
		||||
		panic(err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Println("Successfully connected to the database")
 | 
			
		||||
	defer db.Close()
 | 
			
		||||
 | 
			
		||||
	dbinit(db)
 | 
			
		||||
 | 
			
		||||
	dbentrycount(db)
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user