Example #1
0
func createRouter() *echo.Echo {
	r := echo.New()

	r.SetHTTPErrorHandler(errorHandler)

	r.Use(mw.Logger())
	r.Use(mw.Recover())
	r.Use(cors())
	r.Use(mw.StripTrailingSlash())
	r.Use(auth())

	account := r.Group("/account")
	account.Post("/login", login)
	account.Get("/ping", ping)
	account.Get("/settings", getSettings)
	account.Post("/settings", setSettings)
	account.Delete("/settings/:id", removeSetting)
	//account.Get("/takeout")

	casts := r.Group("/library/casts")
	casts.Get("", getCasts)
	casts.Post("", addCast)
	casts.Put("/:id", renameCast)
	casts.Delete("/:id", removeCast)

	episodes := r.Group("/library")
	episodes.Get("/newepisodes", getNewEpisodes)
	episodes.Get("/episodes/:castid", getEpisodes)
	episodes.Get("/episode/:id", getEpisode)
	episodes.Get("/episodes/label/:label", getEpisodesByLabel)

	events := r.Group("/library/events")
	events.Get("", getEvents)
	events.Post("", addEvents)

	labels := r.Group("/library/labels")
	labels.Get("", getLabels)
	labels.Post("", addLabel)
	labels.Put("/:id", updateLabel)
	labels.Delete("/:id", removeLabel)

	/*opml := r.Group("/library")
	opml.Get("/casts.opml")
	opml.Post("/casts.opml")*/

	// Perhaps:
	// /newepisodes           -> /episodes?since=0
	// /episodes/label/:label -> /episodes?label=label

	return r
}
Example #2
0
func main() {
	// Echo instance
	e := echo.New()

	// Middleware
	e.Use(mw.Logger())
	e.Use(mw.Recover())

	// Routes
	e.Get("/", hello)

	// Start server
	e.Run(":1323")
}
Example #3
0
func main() {
	e := echo.New()

	// Middleware
	e.Use(mw.Logger())
	e.Use(mw.Recover())

	// Routes
	e.Post("/users", createUser)
	e.Get("/users/:id", getUser)
	e.Patch("/users/:id", updateUser)
	e.Delete("/users/:id", deleteUser)

	// Start server
	e.Run(":1323")
}
Example #4
0
func main() {
	// Echo instance
	e := echo.New()

	// Debug mode
	e.SetDebug(true)

	//------------
	// Middleware
	//------------

	// Logger
	e.Use(mw.Logger())

	// Recover
	e.Use(mw.Recover())

	// Basic auth
	e.Use(mw.BasicAuth(func(usr, pwd string) bool {
		if usr == "joe" && pwd == "secret" {
			return true
		}
		return false
	}))

	//-------
	// Slash
	//-------

	e.Use(mw.StripTrailingSlash())

	// or

	//	e.Use(mw.RedirectToSlash())

	// Gzip
	e.Use(mw.Gzip())

	// Routes
	e.Get("/", hello)

	// Start server
	e.Run(":1323")
}
Example #5
0
func main() {
	e := echo.New()

	// Middleware
	e.Use(mw.Logger())
	e.Use(mw.Recover())
	e.Use(mw.Gzip())

	//------------------------
	// 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) error {
		return c.JSON(http.StatusOK, s.Data())
	})

	// Serve index file
	e.Index("public/index.html")

	// Serve favicon
	e.Favicon("public/favicon.ico")

	// Serve static files
	e.Static("/scripts", "public/scripts")

	//--------
	// Routes
	//--------

	e.Post("/users", createUser)
	e.Get("/users", getUsers)
	e.Get("/users/:id", getUser)

	//-----------
	// Templates
	//-----------

	t := &Template{
		// Cached templates
		templates: template.Must(template.ParseFiles("public/views/welcome.html")),
	}
	e.SetRenderer(t)
	e.Get("/welcome", welcome)

	//-------
	// Group
	//-------

	// Group with parent middleware
	a := e.Group("/admin")
	a.Use(func(c *echo.Context) error {
		// Security middleware
		return nil
	})
	a.Get("", func(c *echo.Context) error {
		return c.String(http.StatusOK, "Welcome admin!")
	})

	// Group with no parent middleware
	g := e.Group("/files", func(c *echo.Context) error {
		// Security middleware
		return nil
	})
	g.Get("", func(c *echo.Context) error {
		return c.String(http.StatusOK, "Your files!")
	})

	// Start server
	e.Run(":1323")
}