shiori/internal/webserver/server.go

99 lines
2.9 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"
2019-10-07 14:38:40 +08:00
"path"
2019-05-27 18:01:53 +08:00
"time"
"github.com/go-shiori/shiori/internal/database"
"github.com/julienschmidt/httprouter"
cch "github.com/patrickmn/go-cache"
"github.com/sirupsen/logrus"
)
2019-10-07 14:38:40 +08:00
// Config is parameter that used for starting web server
type Config struct {
DB database.DB
DataDir string
ServerAddress string
ServerPort int
RootPath string
}
2019-05-27 18:01:53 +08:00
// ServeApp serves wb interface in specified port
2019-10-07 14:38:40 +08:00
func ServeApp(cfg Config) error {
2019-05-27 18:01:53 +08:00
// Create handler
hdl := handler{
2019-10-07 14:38:40 +08:00
DB: cfg.DB,
DataDir: cfg.DataDir,
2019-05-27 18:01:53 +08:00
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-10-07 14:38:40 +08:00
RootPath: cfg.RootPath,
2019-05-27 18:01:53 +08:00
}
2019-10-07 14:38:40 +08:00
hdl.prepareSessionCache()
hdl.prepareArchiveCache()
err := hdl.prepareTemplates()
if err != nil {
return fmt.Errorf("failed to prepare templates: %v", err)
}
2019-08-06 21:51:06 +08:00
2019-05-27 18:01:53 +08:00
// Create router
router := httprouter.New()
2019-10-07 14:38:40 +08:00
// jp here means "join path", as in "join route with root path"
jp := func(route string) string {
return path.Join(cfg.RootPath, route)
}
router.GET(jp("/js/*filepath"), hdl.serveJsFile)
router.GET(jp("/res/*filepath"), hdl.serveFile)
router.GET(jp("/css/*filepath"), hdl.serveFile)
router.GET(jp("/fonts/*filepath"), hdl.serveFile)
2019-05-27 18:01:53 +08:00
2019-10-07 14:38:40 +08:00
router.GET(jp("/"), hdl.serveIndexPage)
router.GET(jp("/login"), hdl.serveLoginPage)
router.GET(jp("/bookmark/:id/thumb"), hdl.serveThumbnailImage)
router.GET(jp("/bookmark/:id/content"), hdl.serveBookmarkContent)
router.GET(jp("/bookmark/:id/archive/*filepath"), hdl.serveBookmarkArchive)
2019-05-27 18:01:53 +08:00
2019-10-07 14:38:40 +08:00
router.POST(jp("/api/login"), hdl.apiLogin)
router.POST(jp("/api/logout"), hdl.apiLogout)
router.GET(jp("/api/bookmarks"), hdl.apiGetBookmarks)
router.GET(jp("/api/tags"), hdl.apiGetTags)
router.PUT(jp("/api/tag"), hdl.apiRenameTag)
router.POST(jp("/api/bookmarks"), hdl.apiInsertBookmark)
router.DELETE(jp("/api/bookmarks"), hdl.apiDeleteBookmark)
router.PUT(jp("/api/bookmarks"), hdl.apiUpdateBookmark)
router.PUT(jp("/api/cache"), hdl.apiUpdateCache)
router.PUT(jp("/api/bookmarks/tags"), hdl.apiUpdateBookmarkTags)
router.POST(jp("/api/bookmarks/ext"), hdl.apiInsertViaExtension)
router.DELETE(jp("/api/bookmarks/ext"), hdl.apiDeleteViaExtension)
2019-05-27 18:01:53 +08:00
2019-10-07 14:38:40 +08:00
router.GET(jp("/api/accounts"), hdl.apiGetAccounts)
router.PUT(jp("/api/accounts"), hdl.apiUpdateAccount)
router.POST(jp("/api/accounts"), hdl.apiInsertAccount)
router.DELETE(jp("/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-10-07 14:38:40 +08:00
url := fmt.Sprintf("%s:%d", cfg.ServerAddress, cfg.ServerPort)
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()
}