Added User structure and more attributes to Task structure, changed file so that it returns JSON now

This commit is contained in:
emirbarucija
2018-05-11 16:07:47 +02:00
parent 722caccf3b
commit 6a54807efc
2 changed files with 54 additions and 7 deletions

Binary file not shown.

View File

@@ -2,14 +2,24 @@ package main
import (
"net/http"
"fmt"
"encoding/json"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
type User struct {
Username string
FirstName string
LastName string
}
type Task struct {
Title string
Description string
UserOfTask User
Date string
}
func main() {
@@ -19,22 +29,59 @@ func main() {
e.Use(middleware.Logger())
e.Use(middleware.Recover())
//CORS
// CORS
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
}))
task := Task{
"First task",
"This is my first task in GO language",
// Getting JSON as []byte
b, err := Handler()
if err != nil {
fmt.Println("error:", err)
}
// Route => handler
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
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
}