2015-03-01 19:45:13 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2015-03-27 23:21:30 +02:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
mw "github.com/labstack/echo/middleware"
|
2015-03-25 20:16:17 +02:00
|
|
|
"github.com/rs/cors"
|
|
|
|
"github.com/thoas/stats"
|
2015-03-01 19:45:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type user struct {
|
2015-03-06 21:12:33 +02:00
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-03-26 09:42:34 +02:00
|
|
|
var users map[string]user
|
2015-03-01 19:45:13 +02:00
|
|
|
|
|
|
|
func init() {
|
2015-03-26 09:42:34 +02:00
|
|
|
users = map[string]user{
|
|
|
|
"1": user{
|
2015-03-06 21:12:33 +02:00
|
|
|
ID: "1",
|
2015-03-01 19:45:13 +02:00
|
|
|
Name: "Wreck-It Ralph",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 23:21:30 +02:00
|
|
|
func createUser(c *echo.Context) {
|
2015-03-06 21:12:33 +02:00
|
|
|
u := new(user)
|
|
|
|
if c.Bind(u) {
|
2015-03-26 09:42:34 +02:00
|
|
|
users[u.ID] = *u
|
2015-03-31 05:54:38 +02:00
|
|
|
c.JSON(http.StatusCreated, u)
|
2015-03-06 21:12:33 +02:00
|
|
|
}
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-03-27 23:21:30 +02:00
|
|
|
func getUsers(c *echo.Context) {
|
2015-03-06 21:12:33 +02:00
|
|
|
c.JSON(http.StatusOK, users)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-03-27 23:21:30 +02:00
|
|
|
func getUser(c *echo.Context) {
|
2015-03-06 21:12:33 +02:00
|
|
|
c.JSON(http.StatusOK, users[c.P(0)])
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2015-03-27 23:49:45 +02:00
|
|
|
e := echo.New()
|
2015-03-25 20:16:17 +02:00
|
|
|
|
|
|
|
//*************************//
|
|
|
|
// Built-in middleware //
|
|
|
|
//*************************//
|
2015-03-27 23:49:45 +02:00
|
|
|
e.Use(mw.Logger)
|
2015-03-25 20:16:17 +02:00
|
|
|
|
|
|
|
//****************************//
|
|
|
|
// Third-party middleware //
|
|
|
|
//****************************//
|
|
|
|
// https://github.com/rs/cors
|
2015-03-27 23:49:45 +02:00
|
|
|
e.Use(cors.Default().Handler)
|
2015-03-25 20:16:17 +02:00
|
|
|
|
|
|
|
// https://github.com/thoas/stats
|
|
|
|
s := stats.New()
|
2015-03-27 23:49:45 +02:00
|
|
|
e.Use(s.Handler)
|
2015-03-25 20:16:17 +02:00
|
|
|
// Route
|
2015-03-27 23:49:45 +02:00
|
|
|
e.Get("/stats", func(c *echo.Context) {
|
2015-03-25 20:16:17 +02:00
|
|
|
c.JSON(200, s.Data())
|
|
|
|
})
|
|
|
|
|
|
|
|
// Serve index file
|
2015-03-27 23:49:45 +02:00
|
|
|
e.Index("public/index.html")
|
2015-03-25 20:16:17 +02:00
|
|
|
|
|
|
|
// Serve static files
|
2015-03-27 23:49:45 +02:00
|
|
|
e.Static("/js", "public/js")
|
2015-03-25 20:16:17 +02:00
|
|
|
|
|
|
|
//************//
|
|
|
|
// Routes //
|
|
|
|
//************//
|
2015-03-27 23:49:45 +02:00
|
|
|
e.Post("/users", createUser)
|
|
|
|
e.Get("/users", getUsers)
|
|
|
|
e.Get("/users/:id", getUser)
|
2015-03-25 20:16:17 +02:00
|
|
|
|
2015-04-02 14:02:52 +02:00
|
|
|
// Sub router
|
|
|
|
a := e.Sub("/admin")
|
|
|
|
a.Use(func(c *echo.Context) {
|
|
|
|
// Security check
|
|
|
|
})
|
|
|
|
a.Get("", func(c *echo.Context) {
|
|
|
|
c.String(200, "Welcome to the secured area!")
|
|
|
|
})
|
|
|
|
|
2015-03-25 20:16:17 +02:00
|
|
|
// Start server
|
2015-03-27 23:49:45 +02:00
|
|
|
e.Run(":8080")
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|