pass repo by reference in owl-web

This commit is contained in:
Niko Abeler 2022-08-06 20:22:09 +02:00
parent a0f912d81a
commit fb05e0de49
5 changed files with 21 additions and 21 deletions

View File

@ -28,7 +28,7 @@ func TestRedirectOnAliases(t *testing.T) {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router := main.Router(repo)
router := main.Router(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.
@ -66,7 +66,7 @@ func TestNoRedirectOnNonExistingAliases(t *testing.T) {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router := main.Router(repo)
router := main.Router(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.

View File

@ -9,7 +9,7 @@ import (
"github.com/julienschmidt/httprouter"
)
func getUserFromRepo(repo owl.Repository, ps httprouter.Params) (owl.User, error) {
func getUserFromRepo(repo *owl.Repository, ps httprouter.Params) (owl.User, error) {
if repo.SingleUserName() != "" {
return repo.GetUser(repo.SingleUserName())
}
@ -21,9 +21,9 @@ func getUserFromRepo(repo owl.Repository, ps httprouter.Params) (owl.User, error
return user, nil
}
func repoIndexHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
func repoIndexHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
html, err := owl.RenderUserList(repo)
html, err := owl.RenderUserList(*repo)
if err != nil {
println("Error rendering index: ", err.Error())
@ -36,7 +36,7 @@ func repoIndexHandler(repo owl.Repository) func(http.ResponseWriter, *http.Reque
}
}
func userIndexHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
func userIndexHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
user, err := getUserFromRepo(repo, ps)
if err != nil {
@ -57,7 +57,7 @@ func userIndexHandler(repo owl.Repository) func(http.ResponseWriter, *http.Reque
}
}
func postHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
func postHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
postId := ps.ByName("post")
@ -88,7 +88,7 @@ func postHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request, h
}
}
func postMediaHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
func postMediaHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
postId := ps.ByName("post")
filepath := ps.ByName("filepath")
@ -118,7 +118,7 @@ func postMediaHandler(repo owl.Repository) func(http.ResponseWriter, *http.Reque
}
}
func notFoundHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request) {
func notFoundHandler(repo *owl.Repository) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
aliases, _ := repo.PostAliases()

View File

@ -9,7 +9,7 @@ import (
"github.com/julienschmidt/httprouter"
)
func Router(repo owl.Repository) http.Handler {
func Router(repo *owl.Repository) http.Handler {
router := httprouter.New()
router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir()))
router.GET("/", repoIndexHandler(repo))
@ -20,7 +20,7 @@ func Router(repo owl.Repository) http.Handler {
return router
}
func SingleUserRouter(repo owl.Repository) http.Handler {
func SingleUserRouter(repo *owl.Repository) http.Handler {
router := httprouter.New()
router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir()))
router.GET("/", userIndexHandler(repo))
@ -77,10 +77,10 @@ func main() {
var router http.Handler
if singleUserName == "" {
println("Multi user mode Router used")
router = Router(repo)
router = Router(&repo)
} else {
println("Single user mode Router used")
router = SingleUserRouter(repo)
router = SingleUserRouter(&repo)
}
println("Listening on port", port)
http.ListenAndServe(":"+strconv.Itoa(port), router)

View File

@ -2,7 +2,7 @@ package main_test
import (
"h4kor/owl-blogs"
"h4kor/owl-blogs/cmd/owl-web"
main "h4kor/owl-blogs/cmd/owl-web"
"math/rand"
"net/http"
"net/http/httptest"
@ -43,7 +43,7 @@ func TestMultiUserRepoIndexHandler(t *testing.T) {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router := main.Router(repo)
router := main.Router(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.
@ -74,7 +74,7 @@ func TestMultiUserUserIndexHandler(t *testing.T) {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router := main.Router(repo)
router := main.Router(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.
@ -101,7 +101,7 @@ func TestMultiUserPostHandler(t *testing.T) {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router := main.Router(repo)
router := main.Router(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.
@ -129,7 +129,7 @@ func TestMultiUserPostMediaHandler(t *testing.T) {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router := main.Router(repo)
router := main.Router(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.

View File

@ -28,7 +28,7 @@ func TestSingleUserUserIndexHandler(t *testing.T) {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router := main.SingleUserRouter(repo)
router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.
@ -54,7 +54,7 @@ func TestSingleUserPostHandler(t *testing.T) {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router := main.SingleUserRouter(repo)
router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.
@ -81,7 +81,7 @@ func TestSingleUserPostMediaHandler(t *testing.T) {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router := main.SingleUserRouter(repo)
router := main.SingleUserRouter(&repo)
router.ServeHTTP(rr, req)
// Check the status code is what we expect.