From 3b85ba0592985878ed35482c6251a846ef8c6df9 Mon Sep 17 00:00:00 2001 From: drevil Date: Mon, 3 Jul 2023 20:11:29 -0400 Subject: [PATCH] added manager root --- .gitignore | 3 ++- comics.go | 18 ++++++++++++--- manager.go | 51 ++++++++++++++++++++++++------------------ templates/comic.html | 2 +- templates/login.html | 6 ++--- templates/manager.html | 10 ++++----- testaapas | 1 - testpas | 1 - 8 files changed, 55 insertions(+), 37 deletions(-) delete mode 100644 testaapas delete mode 100644 testpas diff --git a/.gitignore b/.gitignore index 774156c..7cb1105 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ comics db.sqlite media -secret.key \ No newline at end of file +secret.key +manager.root \ No newline at end of file diff --git a/comics.go b/comics.go index fd884af..c856611 100644 --- a/comics.go +++ b/comics.go @@ -50,6 +50,7 @@ type Options struct { Username string Password string CreateUser bool + ManagerRoot string } type Comic struct { @@ -102,6 +103,7 @@ func (o * Options) Parse() error { flag.StringVar(&o.Username, "w", "", "Username for the new user") flag.StringVar(&o.Password, "q", "", "Password for the new user") flag.BoolVar(&o.CreateUser, "c", false, "Creates a new user. Needs -w and -q") + flag.StringVar(&o.ManagerRoot, "o", "manager", "Manager root page") flag.Parse() @@ -199,8 +201,16 @@ func executeTemplate(w http.ResponseWriter, name string, data interface{}) error func comicView(w http.ResponseWriter, r * http.Request) { var err error - + + if len(r.URL.Path) > 1 && r.URL.Path[len(r.URL.Path)-1] == '/' { + r.URL.Path = r.URL.Path[len(r.URL.Path)-2:] + } path := strings.TrimPrefix(r.URL.Path, "/") + if path == options.ManagerRoot { + http.Redirect(w, r, managerPath("/index"), http.StatusSeeOther) + return + } + if len(path) >= 5 { path = path[:5] } @@ -246,6 +256,10 @@ func comicView(w http.ResponseWriter, r * http.Request) { return } + u, err := c.getAuthor() + if err == nil { + context.UserName = u.Name + } context.Comic = c context.Title = fmt.Sprintf("Black Ram Comics: %s", c.Title) } else { @@ -471,14 +485,12 @@ func main() { http.HandleFunc("/404", return404) http.HandleFunc("/500", return500) - // manager if options.RunManager { err = startManager(options.Address, options.Port, options.DBPath, options.MediaPath) if err != nil { log.Fatal(err) } - return } // views diff --git a/manager.go b/manager.go index 38797fd..797d4bd 100644 --- a/manager.go +++ b/manager.go @@ -24,6 +24,7 @@ var seededRand *rand.Rand = nil var secretKey string = "" var secretKeyHash string = "" var tokenSecondsDefault = 60 * 60 * 24 * 14 +var managerRoot = "" const userSquema string = ` CREATE TABLE IF NOT EXISTS user ( @@ -55,6 +56,7 @@ type User struct { type ManagerContext struct { Context User User + ManagerRoot string } type ConfirmContext struct { @@ -458,6 +460,10 @@ func athenticateUserByPassword(username string, password string) (*User, error) return user, err } +func managerPath(path string) string { + return "/" + options.ManagerRoot + path +} + func managerIndexView(w http.ResponseWriter, r * http.Request) { var err error @@ -472,12 +478,7 @@ func managerIndexView(w http.ResponseWriter, r * http.Request) { user, _, err := getSessionUser(r) if err != nil { - http.Redirect(w, r, "/login", http.StatusSeeOther) - return - } - - if len(strings.TrimPrefix(r.URL.Path, "/")) > 0 { - return404(w,r) + http.Redirect(w, r, managerPath("/login"), http.StatusSeeOther) return } @@ -485,7 +486,8 @@ func managerIndexView(w http.ResponseWriter, r * http.Request) { Context: Context{ Title: "Black Ram Comics Manager", }, - User: *user, + User: *user, + ManagerRoot: options.ManagerRoot, } comics, err := user.getComics() @@ -576,14 +578,14 @@ func managerRemoveView(w http.ResponseWriter, r * http.Request) { return } - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, managerPath("/index"), http.StatusSeeOther) } func managerPublishView(w http.ResponseWriter, r * http.Request) { var err error if r.Method == "GET" { - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, managerPath("/index"), http.StatusSeeOther) return } if r.Method != "POST" { @@ -638,12 +640,16 @@ func managerPublishView(w http.ResponseWriter, r * http.Request) { return } - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, managerPath("/index"), http.StatusSeeOther) } func managerLoginView(w http.ResponseWriter, r * http.Request) { - context := Context{ - Title: "Blackram Manager", + context := ManagerContext{ + Context: Context { + Title: "Blackram Manager", + }, + + ManagerRoot: options.ManagerRoot, } if r.Method == "GET" { @@ -684,7 +690,7 @@ func managerLoginView(w http.ResponseWriter, r * http.Request) { Expires: claims.Expiry, }) - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, managerPath("/index"), http.StatusSeeOther) } func managerLogoutView(w http.ResponseWriter, r * http.Request) { @@ -711,7 +717,7 @@ func managerLogoutView(w http.ResponseWriter, r * http.Request) { Name: "token", Value: "", }) - http.Redirect(w, r, "/", http.StatusSeeOther) + http.Redirect(w, r, managerPath("/index"), http.StatusSeeOther) } func managerEditView(w http.ResponseWriter, r * http.Request) { @@ -762,14 +768,15 @@ func startManager(address string, port int, dbPath string, mediaPath string) err return nil } - http.HandleFunc("/", managerIndexView) - http.HandleFunc("/edit/", managerEditView) - http.HandleFunc("/remove/", managerRemoveView) - http.HandleFunc("/publish", managerPublishView) - http.HandleFunc("/login", managerLoginView) - http.HandleFunc("/logout", managerLogoutView) + http.HandleFunc(managerPath("/index") , managerIndexView) + http.HandleFunc(managerPath("/edit/"), managerEditView) + http.HandleFunc(managerPath("/remove/"), managerRemoveView) + http.HandleFunc(managerPath("/publish"), managerPublishView) + http.HandleFunc(managerPath("/login"), managerLoginView) + http.HandleFunc(managerPath("/logout"), managerLogoutView) uri := fmt.Sprintf("%s:%d", address, port) - log.Println("listening to http://" + uri) - return http.ListenAndServe(uri, logRequest(http.DefaultServeMux)) + log.Println("manager listening on http://" + uri + "/" + options.ManagerRoot) + return nil + //return http.ListenAndServe(uri, logRequest(http.DefaultServeMux)) } \ No newline at end of file diff --git a/templates/comic.html b/templates/comic.html index 34f3818..f52b0c5 100644 --- a/templates/comic.html +++ b/templates/comic.html @@ -15,7 +15,7 @@

{{ .Comic.Title }}

{{ template "nextprev" .}} -

Black Ram Comics Issue No. {{ .Current }}, {{ .Comic.Title }}

+

Black Ram Comics Issue No. {{ .Current }}, {{ .Comic.Title }}{{ if .UserName }} by {{ .UserName }} {{ end }}

{{ template "nextprev" .}} {{ end }} diff --git a/templates/login.html b/templates/login.html index d45aa42..3216e77 100644 --- a/templates/login.html +++ b/templates/login.html @@ -17,11 +17,11 @@
-
+ - + - +
diff --git a/templates/manager.html b/templates/manager.html index bbcb5bf..56f72b1 100644 --- a/templates/manager.html +++ b/templates/manager.html @@ -1,12 +1,12 @@ {{ define "top" }}
@@ -28,7 +28,7 @@ {{ template "top" . }}
-
+ @@ -57,8 +57,8 @@ - <Edit> - <Remove> + <Edit> + <Remove> {{ end }} diff --git a/testaapas b/testaapas deleted file mode 100644 index 1173e36..0000000 --- a/testaapas +++ /dev/null @@ -1 +0,0 @@ -7oAhklgrWmAyaqdfD0ny3Z0uM3jAWSOilTM06iDomjboM8KXznDck7wj5UGqwWHeeWVwmnOzdKDLkaLLFeobyG1FO5upVYceVtSf \ No newline at end of file diff --git a/testpas b/testpas deleted file mode 100644 index 04bb527..0000000 --- a/testpas +++ /dev/null @@ -1 +0,0 @@ -CW2trQSolcPHr3I005RLwdoiJMMisl6ZMAd5fFTCD8RUwPk0bf1WOqsGqzS5sroTNVVw53EJCgkO2vf4RwP1Znh4lkB7NbP34WUI \ No newline at end of file