-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
47 lines (39 loc) · 1.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"net/http"
)
type HelloWorld struct {
Message string `json:"message"`
}
func main() {
//initialise Echo framework
e := echo.New()
//Middleware
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "method=${method}, uri=${uri}, status=${status}, host=${host}\n",
}))
//Handlers
e.GET("/", Greeting)
e.GET("/hello/:name", GreetingWithParams)
e.GET("/hello-queries", GreetingWithQuery)
e.Logger.Fatal(e.Start(":3000"))
}
func Greeting(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, HelloWorld{
Message: "Hello World",
})
}
func GreetingWithParams(ctx echo.Context) error {
params := ctx.Param("name")
return ctx.JSON(http.StatusOK, HelloWorld{
Message: "Hello World, my name is " + params,
})
}
func GreetingWithQuery(ctx echo.Context) error {
query := ctx.QueryParam("name")
return ctx.JSON(http.StatusOK, HelloWorld{
Message: "Hello World, my name is " + query + " and I'm using queries",
})
}