Created
January 17, 2018 22:29
-
-
Save mattn/8d860f830cf675de2cc523c42cb61485 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package livereload | |
| import ( | |
| "os" | |
| "github.com/labstack/echo" | |
| "github.com/labstack/echo/middleware" | |
| "github.com/omeid/livereload" | |
| ) | |
| type ( | |
| LiveReloadConfig struct { | |
| // Skipper defines a function to skip middleware. | |
| Skipper middleware.Skipper | |
| Name string | |
| } | |
| ) | |
| var ( | |
| DefaultLiveReloadConfig = LiveReloadConfig{ | |
| Skipper: middleware.DefaultSkipper, | |
| Name: os.Args[0], | |
| } | |
| ) | |
| func LiveReload() echo.MiddlewareFunc { | |
| return LiveReloadWithConfig(DefaultLiveReloadConfig) | |
| } | |
| func LiveReloadWithConfig(config LiveReloadConfig) echo.MiddlewareFunc { | |
| lrs := livereload.New(os.Args[0]) | |
| return func(next echo.HandlerFunc) echo.HandlerFunc { | |
| return func(c echo.Context) (err error) { | |
| if config.Skipper(c) { | |
| return next(c) | |
| } | |
| p := c.Path() | |
| if p == "/livereload.js" { | |
| livereload.LivereloadScript(c.Response(), c.Request()) | |
| return | |
| } | |
| if p == "/livereload" { | |
| lrs.ServeHTTP(c.Response(), c.Request()) | |
| return | |
| } | |
| return next(c) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "net/http" | |
| "github.com/labstack/echo" | |
| "github.com/labstack/echo/middleware" | |
| "github.com/mattn/echo-livereload" | |
| ) | |
| func main() { | |
| e := echo.New() | |
| e.GET("/", func(c echo.Context) error { | |
| return c.String(http.StatusOK, "") | |
| }) | |
| e.Use(middleware.Static("assets")) | |
| e.Use(livereload.LiveReload()) | |
| e.Logger.Fatal(e.Start(":8989")) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment