added manager root

This commit is contained in:
drevil 2023-07-03 20:11:29 -04:00
parent 92cf109746
commit 3b85ba0592
8 changed files with 55 additions and 37 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
comics
db.sqlite
media
secret.key
secret.key
manager.root

View File

@ -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

View File

@ -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))
}

View File

@ -15,7 +15,7 @@
<h2><a href="/{{ .Current }}">{{ .Comic.Title }}</a></h2>
{{ template "nextprev" .}}
<img src="/media/{{ .Comic.Image }}"></img>
<p>Black Ram Comics Issue No. {{ .Current }}, {{ .Comic.Title }}</p>
<p>Black Ram Comics Issue No. {{ .Current }}, {{ .Comic.Title }}{{ if .UserName }} by {{ .UserName }} {{ end }}</p>
{{ template "nextprev" .}}
</div>
{{ end }}

View File

@ -17,11 +17,11 @@
</div>
<div class="login">
<div class="manager-form">
<form action="/login" method="post">
<form action="login" method="post">
<label for="username">&ltUsername&gt</label>
<input type="text" class="input" name="username">
<input type="username" class="input" name="username">
<label for="password">&ltPassword&gt</label>
<input type="text" class="input" name="password">
<input class="input" type="password" name="password">
<input type="submit" style="display:none;" id="login">
<label type="submit" class="login fake-button" for="login">&ltLogin&gt</label>
</form>

View File

@ -1,12 +1,12 @@
{{ define "top" }}
<div class="manager-top">
<div class="logging-buttom">
<a href="/logout">
<a href="logout">
<h1>{{ .User.Name }} &ltLogout&gt</h1>
</a>
</div>
<div class="site-button">
<a href="{{ .SiteURL }}">
<a href="/">
<h1>&ltSite&gt</h1>
</a>
</div>
@ -28,7 +28,7 @@
{{ template "top" . }}
<div class="manager-form">
<form enctype="multipart/form-data" action="/publish" method="post">
<form enctype="multipart/form-data" action="publish" method="post">
<label for="title">&ltTitle&gt</label>
<input type="text" class="input" name="title" onkeypress="return event.keyCode != 13;">
<input type="file" name="image" style="display:none;" id="browse" accept=".jpg, .jpeg, .png">
@ -57,8 +57,8 @@
</a>
</td>
<td class="actions">
<a href="/edit/{{ $comic.No }}">&ltEdit&gt</a>
<a href="/remove/{{ $comic.No }}">&ltRemove&gt</a>
<a href="edit/{{ $comic.No }}">&ltEdit&gt</a>
<a href="remove/{{ $comic.No }}">&ltRemove&gt</a>
</td>
</tr>
{{ end }}

View File

@ -1 +0,0 @@
7oAhklgrWmAyaqdfD0ny3Z0uM3jAWSOilTM06iDomjboM8KXznDck7wj5UGqwWHeeWVwmnOzdKDLkaLLFeobyG1FO5upVYceVtSf

View File

@ -1 +0,0 @@
CW2trQSolcPHr3I005RLwdoiJMMisl6ZMAd5fFTCD8RUwPk0bf1WOqsGqzS5sroTNVVw53EJCgkO2vf4RwP1Znh4lkB7NbP34WUI