-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathserver.go
More file actions
54 lines (47 loc) · 1.14 KB
/
server.go
File metadata and controls
54 lines (47 loc) · 1.14 KB
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
51
52
53
54
package server
import (
"embed"
"io"
"net/http"
"net/url"
"path"
"path/filepath"
"time"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
//go:embed static/*
var static embed.FS
func serveAsset(c echo.Context, path string) error {
f, err := static.Open(path)
if err != nil {
http.NotFound(c.Response(), c.Request())
return nil
}
defer f.Close()
http.ServeContent(c.Response(), c.Request(), path, time.Time{}, f.(io.ReadSeeker))
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
}