shiori/internal/webserver/server.go

74 lines
2.2 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"
"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
func ServeApp(DB database.DB, dataDir string, port int) error {
// 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
}
// 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)
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-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
url := fmt.Sprintf(":%d", port)
svr := &http.Server{
Addr: url,
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 20 * time.Second,
}
// Serve app
logrus.Infoln("Serve shiori in", url)
return svr.ListenAndServe()
}