Skip to content
Snippets Groups Projects
Commit a30c4db6 authored by ivan.rigo's avatar ivan.rigo
Browse files

Added Com,Need TLV working

parent 903e4e64
No related branches found
No related tags found
No related merge requests found
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"github.com/gin-gonic/gin"
)
var arrFile []string
var idServ string
var port string
// album represents data about a record album.
type album struct {
ID string `json:"id"`
Title string `json:"title"`
Artist string `json:"artist"`
Price float64 `json:"price"`
Neighb []int `json:"price"`
}
// albums slice to seed record album data.
var albums = []album{
{ID: "1", Title: "Blue Train", Artist: "John Coltrane",Price},
{ID: "2", Title: "Jeru", Artist: "Gerry Mulligan", Price: 17.99},
{ID: "3", Title: "Sarah Vaughan and Clifford Brown", Artist: "Sarah Vaughan", Price: 39.99},
{ID: "1", Neighb: []int{2, 3, 4}},
{ID: "2", Neighb: []int{1, 5, 6}},
{ID: "3", Neighb: []int{1, 6, 7}},
{ID: "4", Neighb: []int{1, 7}},
{ID: "5", Neighb: []int{2, 6}},
{ID: "6", Neighb: []int{2, 3, 5}},
{ID: "7", Neighb: []int{3, 4}},
}
func reqNeighb(c *gin.Context) {
for i := 0; i < len(albums); i++ {
if albums[i].ID == idServ {
for j := 0; j < len(albums[i].Neighb); j++ {
resp, err := http.Get("http://localhost:808" + strconv.Itoa(albums[i].Neighb[j]) + "/req/" + idServ + "/" + strconv.Itoa(j))
printErr(err)
fmt.Println(resp.Status)
}
}
}
//c.IndentedJSON(http.StatusOK, albums)
}
// getAlbums responds with the list of all albums as JSON.
func getAlbums(c *gin.Context) {
c.IndentedJSON(http.StatusOK, albums)
fmt.Println("Test")
func receiveReq(c *gin.Context) {
fmt.Println(c.Param("parent"))
fmt.Println(c.Param("tlv"))
c.Status(http.StatusOK)
}
func printErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
argsWithoutProg := os.Args[1:]
fmt.Println(argsWithoutProg[0])
idServ = os.Args[1]
nbIdServ, err := strconv.Atoi(idServ)
files, err := ioutil.ReadDir("Storage1/")
printErr(err)
for _, file := range files {
arrFile = append(arrFile, file.Name())
}
port = strconv.Itoa(8080 + nbIdServ)
router := gin.Default()
router.GET("/albums", getAlbums)
router.GET("/req", reqNeighb)
router.GET("/req/:parent/:tlv", receiveReq)
fmt.Println(port)
router.Run("localhost:8080")
/*
resp, err := http.Get("http://localhost:8080/req")
printErr(err)
body, err := ioutil.ReadAll(resp.Body)
printErr(err)
fmt.Println(body)
*/
router.Run("localhost:" + port)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment