Add non-root url path support

This commit is contained in:
hcl
2021-01-25 17:52:15 +08:00
committed by nkanaev
parent 63b265fa04
commit 4f79c919f0
6 changed files with 97 additions and 54 deletions

View File

@@ -11,7 +11,7 @@ import (
)
func userIsAuthenticated(req *http.Request, username, password string) bool {
cookie, _ := req.Cookie("auth")
cookie, _ := req.Cookie("auth")
if cookie == nil {
return false
}
@@ -23,11 +23,18 @@ func userIsAuthenticated(req *http.Request, username, password string) bool {
}
func userAuthenticate(rw http.ResponseWriter, username, password string) {
expires := time.Now().Add(time.Hour * 24 * 7) // 1 week
expires := time.Now().Add(time.Hour * 24 * 7) // 1 week
var cookiePath string
if BasePath != "" {
cookiePath = BasePath
} else {
cookiePath = "/"
}
cookie := http.Cookie{
Name: "auth",
Value: username + ":" + secret(username, password),
Name: "auth",
Value: username + ":" + secret(username, password),
Expires: expires,
Path: cookiePath,
}
http.SetCookie(rw, &cookie)
}

View File

@@ -6,7 +6,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/nkanaev/yarr/storage"
"html"
"html/template"
"io"
@@ -19,27 +18,41 @@ import (
"reflect"
"strconv"
"strings"
"github.com/nkanaev/yarr/storage"
)
var routes []Route = []Route{
p("/", IndexHandler).ManualAuth(),
p("/static/*path", StaticHandler).ManualAuth(),
var routes []Route
p("/api/status", StatusHandler),
p("/api/folders", FolderListHandler),
p("/api/folders/:id", FolderHandler),
p("/api/feeds", FeedListHandler),
p("/api/feeds/find", FeedHandler),
p("/api/feeds/refresh", FeedRefreshHandler),
p("/api/feeds/errors", FeedErrorsHandler),
p("/api/feeds/:id/icon", FeedIconHandler),
p("/api/feeds/:id", FeedHandler),
p("/api/items", ItemListHandler),
p("/api/items/:id", ItemHandler),
p("/api/settings", SettingsHandler),
p("/opml/import", OPMLImportHandler),
p("/opml/export", OPMLExportHandler),
p("/page", PageCrawlHandler),
var BasePathReady = make(chan bool)
func init() {
go func() {
<-BasePathReady
routes = []Route{
p(BasePath+"/", IndexHandler).ManualAuth(),
p(BasePath+"/static/*path", StaticHandler).ManualAuth(),
p(BasePath+"/api/status", StatusHandler),
p(BasePath+"/api/folders", FolderListHandler),
p(BasePath+"/api/folders/:id", FolderHandler),
p(BasePath+"/api/feeds", FeedListHandler),
p(BasePath+"/api/feeds/find", FeedHandler),
p(BasePath+"/api/feeds/refresh", FeedRefreshHandler),
p(BasePath+"/api/feeds/errors", FeedErrorsHandler),
p(BasePath+"/api/feeds/:id/icon", FeedIconHandler),
p(BasePath+"/api/feeds/:id", FeedHandler),
p(BasePath+"/api/items", ItemListHandler),
p(BasePath+"/api/items/:id", ItemHandler),
p(BasePath+"/api/settings", SettingsHandler),
p(BasePath+"/opml/import", OPMLImportHandler),
p(BasePath+"/opml/export", OPMLExportHandler),
p(BasePath+"/page", PageCrawlHandler),
}
if BasePath != "" {
routes = append(routes, p(BasePath, RedirectBasePathHandler).ManualAuth())
}
}()
}
type asset struct {
@@ -140,6 +153,11 @@ func IndexHandler(rw http.ResponseWriter, req *http.Request) {
func StaticHandler(rw http.ResponseWriter, req *http.Request) {
path := Vars(req)["path"]
if BasePath != "" {
path = strings.TrimPrefix(path, BasePath)
}
ctype := mime.TypeByExtension(filepath.Ext(path))
if assets != nil {
@@ -528,3 +546,7 @@ func PageCrawlHandler(rw http.ResponseWriter, req *http.Request) {
}
}
}
func RedirectBasePathHandler(rw http.ResponseWriter, req *http.Request) {
http.Redirect(rw, req, req.URL.Path+"/", http.StatusFound)
}

View File

@@ -5,10 +5,12 @@ import (
"regexp"
)
var BasePath string = ""
type Route struct {
url string
urlRegex *regexp.Regexp
handler func(http.ResponseWriter, *http.Request)
url string
urlRegex *regexp.Regexp
handler func(http.ResponseWriter, *http.Request)
manualAuth bool
}