shiori/internal/webserver/server.go

83 lines
2.5 KiB
Go
Raw Normal View History

2019-05-21 11:31:40 +08:00
package webserver
2019-05-27 18:01:53 +08:00
import (
"fmt"
"net/http"
"time"
"github.com/go-shiori/shiori/internal/database"
2019-08-06 21:51:06 +08:00
"github.com/go-shiori/shiori/pkg/warc"
2019-05-27 18:01:53 +08:00
"github.com/julienschmidt/httprouter"
cch "github.com/patrickmn/go-cache"
"github.com/sirupsen/logrus"
)
var httpClient = &http.Client{Timeout: time.Minute}
2019-05-27 18:01:53 +08:00
// ServeApp serves wb interface in specified port
2019-08-27 13:36:24 +08:00
func ServeApp(DB database.DB, dataDir string, address string, port int) error {
2019-05-27 18:01:53 +08:00
// Create handler
hdl := handler{
DB: DB,
DataDir: dataDir,
UserCache: cch.New(time.Hour, 10*time.Minute),
SessionCache: cch.New(time.Hour, 10*time.Minute),
2019-08-05 19:26:37 +08:00
ArchiveCache: cch.New(time.Minute, 5*time.Minute),
2019-05-27 18:01:53 +08:00
}
2019-08-06 21:51:06 +08:00
hdl.ArchiveCache.OnEvicted(func(key string, data interface{}) {
archive := data.(*warc.Archive)
archive.Close()
})
2019-05-27 18:01:53 +08:00
// Create router
router := httprouter.New()
router.GET("/js/*filepath", hdl.serveJsFile)
router.GET("/res/*filepath", hdl.serveFile)
router.GET("/css/*filepath", hdl.serveFile)
router.GET("/fonts/*filepath", hdl.serveFile)
router.GET("/", hdl.serveIndexPage)
router.GET("/login", hdl.serveLoginPage)
2019-08-04 22:34:23 +08:00
router.GET("/bookmark/:id/thumb", hdl.serveThumbnailImage)
router.GET("/bookmark/:id/content", hdl.serveBookmarkContent)
2019-08-05 19:26:37 +08:00
router.GET("/bookmark/:id/archive/*filepath", hdl.serveBookmarkArchive)
2019-05-27 18:01:53 +08:00
router.POST("/api/login", hdl.apiLogin)
router.POST("/api/logout", hdl.apiLogout)
router.GET("/api/bookmarks", hdl.apiGetBookmarks)
router.GET("/api/tags", hdl.apiGetTags)
2019-08-08 00:30:17 +08:00
router.PUT("/api/tag", hdl.apiRenameTag)
router.POST("/api/bookmarks", hdl.apiInsertBookmark)
2019-05-29 21:08:58 +08:00
router.DELETE("/api/bookmarks", hdl.apiDeleteBookmark)
router.PUT("/api/bookmarks", hdl.apiUpdateBookmark)
2019-06-11 11:59:08 +08:00
router.PUT("/api/cache", hdl.apiUpdateCache)
2019-05-30 17:35:18 +08:00
router.PUT("/api/bookmarks/tags", hdl.apiUpdateBookmarkTags)
2019-08-19 19:22:21 +08:00
router.POST("/api/bookmarks/ext", hdl.apiInsertViaExtension)
router.DELETE("/api/bookmarks/ext", hdl.apiDeleteViaExtension)
2019-05-27 18:01:53 +08:00
router.GET("/api/accounts", hdl.apiGetAccounts)
router.PUT("/api/accounts", hdl.apiUpdateAccount)
router.POST("/api/accounts", hdl.apiInsertAccount)
router.DELETE("/api/accounts", hdl.apiDeleteAccount)
2019-05-27 18:01:53 +08:00
// Route for panic
router.PanicHandler = func(w http.ResponseWriter, r *http.Request, arg interface{}) {
http.Error(w, fmt.Sprint(arg), 500)
}
// Create server
2019-08-27 13:36:24 +08:00
url := fmt.Sprintf("%s:%d", address, port)
2019-05-27 18:01:53 +08:00
svr := &http.Server{
Addr: url,
Handler: router,
ReadTimeout: 10 * time.Second,
2019-08-20 16:34:53 +08:00
WriteTimeout: time.Minute,
2019-05-27 18:01:53 +08:00
}
// Serve app
logrus.Infoln("Serve shiori in", url)
return svr.ListenAndServe()
}