1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-03 15:46:23 -07:00

Use generic page header for manage pages, add login template

This commit is contained in:
Eggbertx 2022-01-04 17:48:46 -08:00
parent c8d8af077b
commit b2318af7a3
12 changed files with 132 additions and 44 deletions

View file

@ -189,7 +189,7 @@ def docker(option = "guestdb", attached = False):
if status != 0:
print("Failed starting a docker container, exited with status code", status)
def install(prefix = "/usr", document_root = "/srv/gochan", js_only = False, css_only = False):
def install(prefix = "/usr", document_root = "/srv/gochan", js_only = False, css_only = False, templates_only = False):
if gcos == "windows" or gcos == "darwin":
print("Installation is not currently supported for Windows and macOS, use the respective directory created by running `python build.py release`")
exit(1)
@ -208,6 +208,20 @@ def install(prefix = "/usr", document_root = "/srv/gochan", js_only = False, css
css_install_dir = path.join(document_root, "css")
fs_action("copy", "html/css", css_install_dir)
done = True
if templates_only:
print("Installing template files")
print(document_root)
templates_install_dir = path.join(document_root, "templates")
if not path.exists(templates_install_dir):
fs_action("mkdir", templates_install_dir)
template_files = os.listdir("templates")
for template in template_files:
if template == "override":
continue
fs_action("copy",
path.join("templates", template),
path.join(document_root, "templates", template))
done = True
if done:
print("done.")
return
@ -346,6 +360,10 @@ if __name__ == "__main__":
action = "store_true",
help = "only install CSS"
)
parser.add_argument("--templates",
action = "store_true",
help = "install the template files"
)
parser.add_argument("--prefix",
default = "/usr",
help = "install gochan to this directory and its subdirectories",
@ -355,7 +373,7 @@ if __name__ == "__main__":
help = "install files in ./html/ to this directory to be requested by a browser"
)
args = parser.parse_args()
install(args.prefix, args.documentroot, args.js, args.css)
install(args.prefix, args.documentroot, args.js, args.css, args.templates)
elif action == "js":
parser.add_argument("--minify", action = "store_true", help = "create a minified gochan.js")
parser.add_argument("--watch", action = "store_true", help = "automatically rebuild when you change a file (keeps running)")

View file

@ -3,6 +3,7 @@ package building
import (
"encoding/json"
"errors"
"io"
"os"
"path"
@ -97,6 +98,28 @@ func BuildBoardListJSON() error {
return nil
}
// BuildPageHeader is a convenience function for automatically generating the top part
// of every normal HTML page
func BuildPageHeader(writer io.Writer) error {
return serverutil.MinifyTemplate(gctemplates.PageHeader,
map[string]interface{}{
"webroot": config.GetSystemCriticalConfig().WebRoot,
"site_config": config.GetSiteConfig(),
"sections": gcsql.AllSections,
"boards": gcsql.AllBoards,
"board_config": config.GetBoardConfig(""),
}, writer, "text/html")
}
// BuildPageFooter is a convenience function for automatically generating the bottom
// of every normal HTML page
func BuildPageFooter(writer io.Writer) (err error) {
return serverutil.MinifyTemplate(gctemplates.PageFooter,
map[string]interface{}{
"webroot": config.GetSystemCriticalConfig().WebRoot,
}, writer, "text/html")
}
// BuildJS minifies (if enabled) consts.js, which is built from a template
func BuildJS() error {
// build consts.js from template

View file

@ -22,8 +22,10 @@ var (
ManageBoards *template.Template
ManageConfig *template.Template
ManageDashboard *template.Template
ManageHeader *template.Template
ManageLogin *template.Template
ManageStaff *template.Template
PageHeader *template.Template
PageFooter *template.Template
PostEdit *template.Template
ThreadPage *template.Template
)
@ -145,10 +147,10 @@ func templateLoading(t string, buildAll bool) error {
return templateError("manage_dashboard.html", err)
}
}
if buildAll || t == "manageheader" {
ManageHeader, err = loadTemplate("manage_header.html")
if buildAll || t == "managelogin" {
ManageLogin, err = loadTemplate("manage_login.html")
if err != nil {
return templateError("manage_header.html", err)
return templateError("manage_login.html", err)
}
}
if buildAll || t == "managestaff" {
@ -157,6 +159,18 @@ func templateLoading(t string, buildAll bool) error {
return templateError("manage_staff.html", err)
}
}
if buildAll || t == "pageheader" {
PageHeader, err = loadTemplate("page_header.html")
if err != nil {
return templateError("page_header.html", err)
}
}
if buildAll || t == "pagefooter" {
PageFooter, err = loadTemplate("page_footer.html")
if err != nil {
return templateError("page_footer.html", err)
}
}
if buildAll || t == "js" {
JsConsts, err = loadTemplate("consts.js")
if err != nil {

View file

@ -344,17 +344,26 @@ var actions = []Action{
username := request.FormValue("username")
password := request.FormValue("password")
redirectAction := request.FormValue("action")
if redirectAction == "" {
if redirectAction == "" || redirectAction == "logout" {
redirectAction = "announcements"
}
if username == "" || password == "" {
//assume that they haven't logged in
output = `<form method="POST" action="` + systemCritical.WebRoot + `manage?action=login" id="login-box" class="staff-form">` +
`<input type="hidden" name="redirect" value="` + redirectAction + `" />` +
`<input type="text" name="username" class="logindata" /><br />` +
`<input type="password" name="password" class="logindata" /><br />` +
`<input type="submit" value="Login" />` +
`</form>`
manageLoginBuffer := bytes.NewBufferString("")
if err = serverutil.MinifyTemplate(gctemplates.ManageLogin,
map[string]interface{}{
"webroot": config.GetSystemCriticalConfig().WebRoot,
"site_config": config.GetSiteConfig(),
"sections": gcsql.AllSections,
"boards": gcsql.AllBoards,
"board_config": config.GetBoardConfig(""),
"redirect": redirectAction,
}, manageLoginBuffer, "text/html"); err != nil {
return "", errors.New(gclog.Print(gclog.LErrorLog,
"Error executing staff login page template: "+err.Error()))
}
output = manageLoginBuffer.String()
} else {
key := gcutil.Md5Sum(request.RemoteAddr + username + password + systemCritical.RandomSeed + gcutil.RandomString(3))[0:10]
createSession(key, username, password, request, writer)
@ -371,7 +380,7 @@ var actions = []Action{
cookie.MaxAge = 0
cookie.Expires = time.Now().Add(-7 * 24 * time.Hour)
http.SetCookie(writer, cookie)
return "Logged out successfully", nil
return "<br />Logged out successfully", nil
}},
{
ID: "announcements",

View file

@ -5,9 +5,8 @@ import (
"fmt"
"net/http"
"github.com/gochan-org/gochan/pkg/config"
"github.com/gochan-org/gochan/pkg/building"
"github.com/gochan-org/gochan/pkg/gclog"
"github.com/gochan-org/gochan/pkg/gctemplates"
"github.com/gochan-org/gochan/pkg/gcutil"
"github.com/gochan-org/gochan/pkg/serverutil"
)
@ -116,18 +115,16 @@ func CallManageFunction(writer http.ResponseWriter, request *http.Request) {
serverutil.MinifyWriter(writer, []byte(outputJSON), "application/json")
return
}
managePageBuffer.WriteString("<!DOCTYPE html><html><head>")
criticalCfg := config.GetSystemCriticalConfig()
if err = serverutil.MinifyTemplate(gctemplates.ManageHeader,
map[string]interface{}{
"webroot": criticalCfg.WebRoot,
},
&managePageBuffer, "text/html"); err != nil {
serverutil.ServeErrorPage(writer, gclog.Print(gclog.LErrorLog|gclog.LStaffLog,
"Error executing manage page header template: ", err.Error()))
if err = building.BuildPageHeader(&managePageBuffer); err != nil {
serveError(writer, "error", actionID,
gclog.Print(gclog.LErrorLog, "Failed writing page header: ", err.Error()), false)
return
}
managePageBuffer.WriteString("<br />" + fmt.Sprint(output) + "<br /><br />")
if err = building.BuildPageFooter(&managePageBuffer); err != nil {
serveError(writer, "error", actionID,
gclog.Print(gclog.LErrorLog, "Failed writing page footer: ", err.Error()), false)
return
}
managePageBuffer.WriteString(fmt.Sprint(output, "</body></html>"))
writer.Write(managePageBuffer.Bytes())
}

View file

@ -1,4 +1,4 @@
<h1 class="manage-header">Ban user</h1>
<h1>Ban user</h1>
<form method="POST" action="/manage?action=bans">
<input type="hidden" name="do" value="add" />
<b>User filter:</b><br />

View file

@ -1,4 +1,4 @@
<h2 class="manage-header">Config editor</h2>
<h1>Config editor</h1>
{{if ne .status ""}}{{.status}}<hr />{{end}}
Some fields omitted because they can not be (safely) edited from the web interface while Gochan is running.
Edit these directly in gochan.json, then restart Gochan.<br />

View file

@ -1,3 +1,30 @@
<h1 class="manage-header">Dashboard</h1>
{{/*
TODO: Make this actually useful
*/}}
Coming soon
<h2>Dashboard</h2>
(still a work in progress)<br /><br />
<fieldset><legend>Announcements</legend>
<div class="announcement">
<b>Announcement title</b><br />
Announcement body goes here
</div><hr />
<div class="announcement">
<b>Announcement title 2</b><br />
Announcement body goes here
</div>
</fieldset><br />
<fieldset><legend>Boards</legend>
<ul>
<li><a href="/test/">/test/</a> - Testing board</li>
<li><a href="/test2/">/test2/</a> - Testing board 2</li>
<li><a href="/test3/">/test2/</a> - Testing board 3</li>
</ul>
</fieldset><br />
<fieldset><legend>Staff actions (role: administrator)</legend>
<ul>
<li><a href="/manage?action=logout">Log out</a></li>
<li><a href="/manage?action=rebuildall">Rebuild everything</a></li>
<li><a href="/manage?action=bans">Bans</a></li>
</ul>
</fieldset>

View file

@ -1,8 +0,0 @@
<title>Gochan Manage page</title>
<link rel="stylesheet" href="{{.WebRoot}}css/global.css" />
<link id="theme" rel="stylesheet" href="{{.WebRoot}}css/{{.DefaultStyle}}" />
<link rel="shortcut icon" href="{{.WebRoot}}favicon.png" />
<script type="text/javascript" src="{{.WebRoot}}js/consts.js"></script>
<script type="text/javascript" src="{{.WebRoot}}js/gochan.js"></script>
</head>
<body>

View file

@ -0,0 +1,9 @@
<h1>Login</h1><br />
<form method="POST" action="{{.webroot}}manage?action=login" id="login-box" class="staff-form">
<input type="hidden" name="redirect" value="{{.redirect}}" />
<table>
<tr><td>Login</td><td><input type="text" name="username" class="logindata" /><br /></td></tr>
<tr><td>Password</td><td><input type="password" name="password" class="logindata" /><br /></td></tr>
<tr><td><input type="submit" value="Login" /></td></tr>
</table>
</form><br />

View file

@ -1,4 +1,3 @@
<h1 class="manage-header">Staff</h1><br />
<table id="stafftable" border="1">
<tr>

View file

@ -10,11 +10,11 @@
{{- else}}<title>/{{$.board.Dir}}/ - #{{$.op.ID}}</title>{{end}}
{{- else}}<title>/{{$.board.Dir}}/ - {{$.board.Title}}</title>{{end}}
{{- else}}<title>{{.site_config.SiteName}}</title>{{end}}
<link rel="stylesheet" href="{{$.webroot}}css/global.css" />
<link id="theme" rel="stylesheet" href="{{.webroot}}css/{{.board.DefaultStyle}}" />
<link rel="stylesheet" href="{{.webroot}}css/global.css" />
<link id="theme" rel="stylesheet" href="{{.webroot}}css/{{.board_config.DefaultStyle}}" />
<link rel="shortcut icon" href="{{.webroot}}favicon.png">
<script type="text/javascript" src="{{$.webroot}}js/consts.js"></script>
<script type="text/javascript" src="{{$.webroot}}js/gochan.js"></script>
<script type="text/javascript" src="{{.webroot}}js/consts.js"></script>
<script type="text/javascript" src="{{.webroot}}js/gochan.js"></script>
</head>
<body>
<div id="topbar">