2015-03-01 19:45:13 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-11 06:48:26 +02:00
|
|
|
"io"
|
2015-03-01 19:45:13 +02:00
|
|
|
"net/http"
|
|
|
|
|
2015-04-11 06:48:26 +02:00
|
|
|
"html/template"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2015-04-11 06:48:26 +02:00
|
|
|
type (
|
|
|
|
// Template provides HTML template rendering
|
|
|
|
Template struct {
|
|
|
|
templates *template.Template
|
|
|
|
}
|
2015-03-01 19:45:13 +02:00
|
|
|
|
2015-04-11 06:48:26 +02:00
|
|
|
user struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
2015-04-11 06:48:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
users map[string]user
|
|
|
|
)
|
|
|
|
|
|
|
|
// Render HTML
|
|
|
|
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
|
|
|
|
return t.templates.ExecuteTemplate(w, name, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func welcome(c *echo.Context) {
|
|
|
|
c.Render("welcome", "Joe")
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-03-27 23:21:30 +02:00
|
|
|
func createUser(c *echo.Context) {
|
2015-03-06 21:12:33 +02:00
|
|
|
u := new(user)
|
2015-04-03 05:18:34 +02:00
|
|
|
if err := c.Bind(u); err == nil {
|
2015-03-26 09:42:34 +02:00
|
|
|
users[u.ID] = *u
|
2015-04-03 05:18:34 +02:00
|
|
|
if err := c.JSON(http.StatusCreated, u); err == nil {
|
|
|
|
// Do something!
|
|
|
|
}
|
|
|
|
return
|
2015-03-06 21:12:33 +02:00
|
|
|
}
|
2015-04-03 05:18:34 +02:00
|
|
|
http.Error(c.Response, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
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-11 06:48:26 +02:00
|
|
|
//***************//
|
|
|
|
// Templates //
|
|
|
|
//***************//
|
|
|
|
t := &Template{
|
|
|
|
// Cached templates
|
|
|
|
templates: template.Must(template.ParseFiles("public/tpl/welcome.tpl")),
|
|
|
|
}
|
|
|
|
e.Renderer(t)
|
|
|
|
e.Get("/welcome", welcome)
|
|
|
|
|
2015-04-07 22:43:52 +02:00
|
|
|
//***********//
|
|
|
|
// Group //
|
|
|
|
//***********//
|
|
|
|
// Group with parent middleware
|
|
|
|
a := e.Group("/admin")
|
|
|
|
a.Use(func(c *echo.Context) {
|
|
|
|
// Security middleware
|
2015-04-02 14:02:52 +02:00
|
|
|
})
|
2015-04-07 22:43:52 +02:00
|
|
|
a.Get("", func(c *echo.Context) {
|
|
|
|
c.String(http.StatusOK, "Welcome admin!")
|
2015-04-02 23:41:36 +02:00
|
|
|
})
|
|
|
|
|
2015-04-07 22:43:52 +02:00
|
|
|
// Group with no parent middleware
|
|
|
|
g := e.Group("/files", func(c *echo.Context) {
|
|
|
|
// Security middleware
|
2015-04-02 23:41:36 +02:00
|
|
|
})
|
2015-04-07 22:43:52 +02:00
|
|
|
g.Get("", func(c *echo.Context) {
|
|
|
|
c.String(http.StatusOK, "Your files!")
|
2015-04-02 14:02:52 +02:00
|
|
|
})
|
|
|
|
|
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
|
|
|
}
|
2015-04-11 06:48:26 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
users = map[string]user{
|
|
|
|
"1": user{
|
|
|
|
ID: "1",
|
|
|
|
Name: "Wreck-It Ralph",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|