/
server.go
50 lines (44 loc) · 1.21 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//go:generate sh -c "go get -u github.com/kevinburke/go-bindata/... && `go env GOPATH`/bin/go-bindata -pkg server -ignore '(^|/)\\..*' static/..."
package server
import (
"bytes"
"net/http"
"net/url"
"path"
"path/filepath"
"time"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func serveAsset(c echo.Context, path string) error {
b, err := Asset(path)
if err != nil {
http.NotFound(c.Response(), c.Request())
return nil
}
http.ServeContent(c.Response(), c.Request(), path, time.Time{}, bytes.NewReader(b))
return nil
}
func New(db Database, ga string) *echo.Echo {
e := echo.New()
e.Use(middleware.Recover())
e.GET("/", IndexHandler(IndexConfiguration{
GoogleAnalytics: ga,
}))
e.GET("/activity.json", ActivityHandler(db))
e.GET("/rss", RSSHandler(db))
e.GET("/rss.php", func(c echo.Context) error {
return c.Redirect(http.StatusMovedPermanently, AbsoluteURL(c, "/rss"))
})
e.GET("/favicon.ico", func(c echo.Context) error {
return serveAsset(c, "static/favicon.ico")
})
e.GET("/static/*", func(c echo.Context) error {
p, err := url.PathUnescape(c.Param("*"))
if err != nil {
return err
}
return serveAsset(c, filepath.Join("static", path.Clean("/"+p)))
})
return e
}