forked from PH9/golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
44 lines (35 loc) · 825 Bytes
/
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
package main
import (
"net/http"
"strconv"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
// Handler
func hello(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}
func add(c echo.Context) error {
var a, b int
var err error
if a, err = strconv.Atoi(c.Param("a")); err != nil {
return c.String(http.StatusBadRequest, "invalid value a")
}
if b, err = strconv.Atoi(c.Param("b")); err != nil {
return c.String(http.StatusBadRequest, "invalid value b")
}
return c.String(http.StatusOK, strconv.Itoa(a+b))
}
func main() {
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORS())
// Routes
e.GET("/", hello)
e.GET("/add/:a/:b", add)
// Start server
e.Logger.Fatal(e.Start(":1323"))
}