single user mode in server. #1
This commit is contained in:
parent
910e74682f
commit
52a68c1cae
|
@ -9,6 +9,18 @@ import (
|
|||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
||||
func getUserFromRepo(repo owl.Repository, ps httprouter.Params) (owl.User, error) {
|
||||
if repo.SingleUserName() != "" {
|
||||
return repo.GetUser(repo.SingleUserName())
|
||||
}
|
||||
userName := ps.ByName("user")
|
||||
user, err := repo.GetUser(userName)
|
||||
if err != nil {
|
||||
return owl.User{}, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -26,8 +38,7 @@ func repoIndexHandler(repo owl.Repository) func(http.ResponseWriter, *http.Reque
|
|||
|
||||
func userIndexHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
userName := ps.ByName("user")
|
||||
user, err := repo.GetUser(userName)
|
||||
user, err := getUserFromRepo(repo, ps)
|
||||
if err != nil {
|
||||
println("Error getting user: ", err.Error())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
|
@ -41,16 +52,16 @@ func userIndexHandler(repo owl.Repository) func(http.ResponseWriter, *http.Reque
|
|||
w.Write([]byte("Internal server error"))
|
||||
return
|
||||
}
|
||||
println("Rendering index page for user", userName)
|
||||
println("Rendering index page for user", user.Name())
|
||||
w.Write([]byte(html))
|
||||
}
|
||||
}
|
||||
|
||||
func postHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
userName := ps.ByName("user")
|
||||
postId := ps.ByName("post")
|
||||
user, err := repo.GetUser(userName)
|
||||
|
||||
user, err := getUserFromRepo(repo, ps)
|
||||
if err != nil {
|
||||
println("Error getting user: ", err.Error())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
|
@ -79,11 +90,10 @@ func postHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request, h
|
|||
|
||||
func postMediaHandler(repo owl.Repository) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
userName := ps.ByName("user")
|
||||
postId := ps.ByName("post")
|
||||
filepath := ps.ByName("filepath")
|
||||
user, err := repo.GetUser(userName)
|
||||
|
||||
user, err := getUserFromRepo(repo, ps)
|
||||
if err != nil {
|
||||
println("Error getting user: ", err.Error())
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
|
|
|
@ -19,13 +19,24 @@ func Router(repo owl.Repository) http.Handler {
|
|||
return router
|
||||
}
|
||||
|
||||
func SingleUserRouter(repo owl.Repository) http.Handler {
|
||||
router := httprouter.New()
|
||||
router.ServeFiles("/static/*filepath", http.Dir(repo.StaticDir()))
|
||||
router.GET("/", userIndexHandler(repo))
|
||||
router.GET("/posts/:post/", postHandler(repo))
|
||||
router.GET("/posts/:post/media/*filepath", postMediaHandler(repo))
|
||||
return router
|
||||
}
|
||||
|
||||
func main() {
|
||||
println("owl web server")
|
||||
println("Parameters")
|
||||
println("-repo <repo> - Specify the repository to use. Defaults to '.'")
|
||||
println("-port <port> - Specify the port to use, Default is '8080'")
|
||||
println("-user <name> - Start server in single user mode.")
|
||||
var repoName string
|
||||
var port int
|
||||
var singleUserName string
|
||||
for i, arg := range os.Args[0 : len(os.Args)-1] {
|
||||
if arg == "-port" {
|
||||
port, _ = strconv.Atoi(os.Args[i+1])
|
||||
|
@ -33,6 +44,9 @@ func main() {
|
|||
if arg == "-repo" {
|
||||
repoName = os.Args[i+1]
|
||||
}
|
||||
if arg == "-user" {
|
||||
singleUserName = os.Args[i+1]
|
||||
}
|
||||
}
|
||||
if repoName == "" {
|
||||
repoName = "."
|
||||
|
@ -41,13 +55,32 @@ func main() {
|
|||
port = 8080
|
||||
}
|
||||
|
||||
repo, err := owl.OpenRepository(repoName)
|
||||
var repo owl.Repository
|
||||
var err error
|
||||
if singleUserName != "" {
|
||||
println("Single user mode")
|
||||
println("Repository:", repoName)
|
||||
println("User:", singleUserName)
|
||||
repo, err = owl.OpenSingleUserRepo(repoName, singleUserName)
|
||||
} else {
|
||||
println("Multi user mode")
|
||||
println("Repository:", repoName)
|
||||
repo, err = owl.OpenRepository(repoName)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
println("Error opening repository: ", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
router := Router(repo)
|
||||
var router http.Handler
|
||||
if singleUserName == "" {
|
||||
println("Multi user mode Router used")
|
||||
router = Router(repo)
|
||||
} else {
|
||||
println("Single user mode Router used")
|
||||
router = SingleUserRouter(repo)
|
||||
}
|
||||
println("Listening on port", port)
|
||||
http.ListenAndServe(":"+strconv.Itoa(port), router)
|
||||
|
||||
|
|
|
@ -72,6 +72,10 @@ func OpenSingleUserRepo(name string, user_name string) (Repository, error) {
|
|||
return repo, nil
|
||||
}
|
||||
|
||||
func (repo Repository) SingleUserName() string {
|
||||
return repo.active_user
|
||||
}
|
||||
|
||||
func (repo Repository) Dir() string {
|
||||
return repo.name
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue