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"
|
2015-04-21 08:17:21 +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) {
|
2015-04-19 02:02:02 +02:00
|
|
|
c.Render(http.StatusOK, "welcome", "Joe")
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
func createUser(c *echo.Context) error {
|
2015-03-06 21:12:33 +02:00
|
|
|
u := new(user)
|
2015-04-20 01:00:23 +02:00
|
|
|
if err := c.Bind(u); err != nil {
|
|
|
|
return err
|
2015-03-06 21:12:33 +02:00
|
|
|
}
|
2015-04-20 01:00:23 +02:00
|
|
|
users[u.ID] = *u
|
|
|
|
return c.JSON(http.StatusCreated, u)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
func getUsers(c *echo.Context) error {
|
|
|
|
return c.JSON(http.StatusOK, users)
|
2015-03-01 19:45:13 +02:00
|
|
|
}
|
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
func getUser(c *echo.Context) error {
|
|
|
|
return 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
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
// Middleware
|
|
|
|
e.Use(echo.Logger)
|
2015-03-25 20:16:17 +02:00
|
|
|
|
2015-04-21 08:17:21 +02:00
|
|
|
//------------------------
|
|
|
|
// Third-party middleware
|
|
|
|
//------------------------
|
|
|
|
|
|
|
|
// https://github.com/rs/cors
|
|
|
|
e.Use(cors.Default().Handler)
|
|
|
|
|
|
|
|
// https://github.com/thoas/stats
|
|
|
|
s := stats.New()
|
|
|
|
e.Use(s.Handler)
|
|
|
|
// Route
|
|
|
|
e.Get("/stats", func(c *echo.Context) {
|
|
|
|
c.JSON(http.StatusOK, s.Data())
|
|
|
|
})
|
|
|
|
|
2015-03-25 20:16:17 +02:00
|
|
|
// 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-04-11 19:09:41 +02:00
|
|
|
e.Static("/scripts", "public/scripts")
|
2015-03-25 20:16:17 +02:00
|
|
|
|
2015-04-19 01:47:48 +02:00
|
|
|
//--------
|
|
|
|
// Routes
|
|
|
|
//--------
|
2015-04-21 08:17:21 +02:00
|
|
|
|
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-20 01:00:23 +02:00
|
|
|
//-----------
|
|
|
|
// Templates
|
|
|
|
//-----------
|
2015-04-21 08:17:21 +02:00
|
|
|
|
2015-04-11 06:48:26 +02:00
|
|
|
t := &Template{
|
|
|
|
// Cached templates
|
2015-04-11 19:09:41 +02:00
|
|
|
templates: template.Must(template.ParseFiles("public/views/welcome.html")),
|
2015-04-11 06:48:26 +02:00
|
|
|
}
|
|
|
|
e.Renderer(t)
|
|
|
|
e.Get("/welcome", welcome)
|
|
|
|
|
2015-04-20 01:00:23 +02:00
|
|
|
//-------
|
|
|
|
// Group
|
|
|
|
//-------
|
|
|
|
|
2015-04-07 22:43:52 +02:00
|
|
|
// 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-04-20 01:00:23 +02:00
|
|
|
e.Run(":4444")
|
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",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|