Added User structure and more attributes to Task structure, changed file so that it returns JSON now
This commit is contained in:
Binary file not shown.
@@ -2,14 +2,24 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"fmt"
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
"github.com/labstack/echo/middleware"
|
"github.com/labstack/echo/middleware"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Username string
|
||||||
|
FirstName string
|
||||||
|
LastName string
|
||||||
|
}
|
||||||
|
|
||||||
type Task struct {
|
type Task struct {
|
||||||
Title string
|
Title string
|
||||||
Description string
|
Description string
|
||||||
|
UserOfTask User
|
||||||
|
Date string
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -19,22 +29,59 @@ func main() {
|
|||||||
e.Use(middleware.Logger())
|
e.Use(middleware.Logger())
|
||||||
e.Use(middleware.Recover())
|
e.Use(middleware.Recover())
|
||||||
|
|
||||||
//CORS
|
// CORS
|
||||||
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||||||
AllowOrigins: []string{"*"},
|
AllowOrigins: []string{"*"},
|
||||||
AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
|
AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
|
||||||
}))
|
}))
|
||||||
|
|
||||||
task := Task{
|
// Getting JSON as []byte
|
||||||
"First task",
|
b, err := Handler()
|
||||||
"This is my first task in GO language",
|
if err != nil {
|
||||||
|
fmt.Println("error:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Route => handler
|
// Route => handler
|
||||||
e.GET("/", func(c echo.Context) error {
|
e.GET("/", func(c echo.Context) error {
|
||||||
return c.String(http.StatusOK, "Title: " + task.Title + "\nDescription: " + task.Description)
|
return c.String(http.StatusOK, string(b[:]))
|
||||||
})
|
})
|
||||||
|
|
||||||
// Server
|
// Server
|
||||||
e.Start(":1323")
|
e.Start(":1500")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u User) String() string {
|
||||||
|
return fmt.Sprintf("Username: %v\nFirst name: %v\nLast name: %v", u.Username, u.FirstName, u.LastName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Task) String() string {
|
||||||
|
return fmt.Sprintf("Title: %v\nDescription: %v\nUser: %v\nDate: %v", t.Title, t.Description, t.UserOfTask, t.Date)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Handler() ([]byte, error) {
|
||||||
|
user := User {
|
||||||
|
Username: "emirbarucija",
|
||||||
|
FirstName: "Emir",
|
||||||
|
LastName: "Baručija",
|
||||||
|
}
|
||||||
|
|
||||||
|
task1 := Task {
|
||||||
|
Title: "First task",
|
||||||
|
Description: "This is my first task in GO language",
|
||||||
|
UserOfTask: user,
|
||||||
|
Date: "10.04.2018.",
|
||||||
|
}
|
||||||
|
|
||||||
|
task2 := Task {
|
||||||
|
Title: "Second task",
|
||||||
|
Description: "This is my second task in GO language",
|
||||||
|
UserOfTask: user,
|
||||||
|
Date: "14.04.2018.",
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks := []Task{task1, task2}
|
||||||
|
|
||||||
|
b, err := json.Marshal(tasks)
|
||||||
|
|
||||||
|
return b, err
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user