mirror of
https://github.com/nkanaev/yarr.git
synced 2025-11-09 19:08:57 +00:00
rewrite basepath
This commit is contained in:
@@ -19,11 +19,11 @@ import (
|
||||
)
|
||||
|
||||
func (s *Server) handler() http.Handler {
|
||||
r := router.NewRouter(BasePath)
|
||||
r := router.NewRouter(s.BasePath)
|
||||
|
||||
if s.Username != "" && s.Password != "" {
|
||||
a := &auth.Middleware{
|
||||
BasePath: BasePath,
|
||||
BasePath: s.BasePath,
|
||||
Username: s.Username,
|
||||
Password: s.Password,
|
||||
Public: "/static",
|
||||
@@ -61,7 +61,7 @@ func (s *Server) handleIndex(c *router.Context) {
|
||||
|
||||
func (s *Server) handleStatic(c *router.Context) {
|
||||
// TODO: gzip?
|
||||
http.StripPrefix(BasePath+"/static/", http.FileServer(http.FS(assets.FS))).ServeHTTP(c.Out, c.Req)
|
||||
http.StripPrefix(s.BasePath+"/static/", http.FileServer(http.FS(assets.FS))).ServeHTTP(c.Out, c.Req)
|
||||
}
|
||||
|
||||
func (s *Server) handleStatus(c *router.Context) {
|
||||
@@ -433,6 +433,6 @@ func (s *Server) handlePageCrawl(c *router.Context) {
|
||||
}
|
||||
|
||||
func (s *Server) handleLogout(c *router.Context) {
|
||||
auth.Logout(c.Out, BasePath)
|
||||
auth.Logout(c.Out, s.BasePath)
|
||||
c.Out.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
33
src/server/routes_test.go
Normal file
33
src/server/routes_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStatic(t *testing.T) {
|
||||
handler := NewServer(nil, "127.0.0.1:8000").handler()
|
||||
url := "/static/javascripts/app.js"
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest("GET", url, nil)
|
||||
handler.ServeHTTP(recorder, request)
|
||||
if recorder.Result().StatusCode != 200 {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaticWithBase(t *testing.T) {
|
||||
server := NewServer(nil, "127.0.0.1:8000")
|
||||
server.BasePath = "/sub"
|
||||
|
||||
handler := server.handler()
|
||||
url := "/sub/static/javascripts/app.js"
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest("GET", url, nil)
|
||||
handler.ServeHTTP(recorder, request)
|
||||
if recorder.Result().StatusCode != 200 {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,13 @@ import (
|
||||
"github.com/nkanaev/yarr/src/worker"
|
||||
)
|
||||
|
||||
var BasePath string = ""
|
||||
|
||||
type Server struct {
|
||||
Addr string
|
||||
db *storage.Storage
|
||||
worker *worker.Worker
|
||||
|
||||
BasePath string
|
||||
|
||||
// auth
|
||||
Username string
|
||||
Password string
|
||||
@@ -35,7 +36,7 @@ func (h *Server) GetAddr() string {
|
||||
if h.CertFile != "" && h.KeyFile != "" {
|
||||
proto = "https"
|
||||
}
|
||||
return proto + "://" + h.Addr + BasePath
|
||||
return proto + "://" + h.Addr + h.BasePath
|
||||
}
|
||||
|
||||
func (s *Server) Start() {
|
||||
|
||||
Reference in New Issue
Block a user