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: if status != 0:
print("Failed starting a docker container, exited with status code", status) 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": 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`") print("Installation is not currently supported for Windows and macOS, use the respective directory created by running `python build.py release`")
exit(1) 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") css_install_dir = path.join(document_root, "css")
fs_action("copy", "html/css", css_install_dir) fs_action("copy", "html/css", css_install_dir)
done = True 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: if done:
print("done.") print("done.")
return return
@ -346,6 +360,10 @@ if __name__ == "__main__":
action = "store_true", action = "store_true",
help = "only install CSS" help = "only install CSS"
) )
parser.add_argument("--templates",
action = "store_true",
help = "install the template files"
)
parser.add_argument("--prefix", parser.add_argument("--prefix",
default = "/usr", default = "/usr",
help = "install gochan to this directory and its subdirectories", 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" help = "install files in ./html/ to this directory to be requested by a browser"
) )
args = parser.parse_args() 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": elif action == "js":
parser.add_argument("--minify", action = "store_true", help = "create a minified gochan.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)") 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 ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"io"
"os" "os"
"path" "path"
@ -97,6 +98,28 @@ func BuildBoardListJSON() error {
return nil 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 // BuildJS minifies (if enabled) consts.js, which is built from a template
func BuildJS() error { func BuildJS() error {
// build consts.js from template // build consts.js from template

View file

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

View file

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

View file

@ -5,9 +5,8 @@ import (
"fmt" "fmt"
"net/http" "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/gclog"
"github.com/gochan-org/gochan/pkg/gctemplates"
"github.com/gochan-org/gochan/pkg/gcutil" "github.com/gochan-org/gochan/pkg/gcutil"
"github.com/gochan-org/gochan/pkg/serverutil" "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") serverutil.MinifyWriter(writer, []byte(outputJSON), "application/json")
return return
} }
managePageBuffer.WriteString("<!DOCTYPE html><html><head>") if err = building.BuildPageHeader(&managePageBuffer); err != nil {
criticalCfg := config.GetSystemCriticalConfig() serveError(writer, "error", actionID,
gclog.Print(gclog.LErrorLog, "Failed writing page header: ", err.Error()), false)
if err = serverutil.MinifyTemplate(gctemplates.ManageHeader, return
map[string]interface{}{ }
"webroot": criticalCfg.WebRoot, managePageBuffer.WriteString("<br />" + fmt.Sprint(output) + "<br /><br />")
}, if err = building.BuildPageFooter(&managePageBuffer); err != nil {
&managePageBuffer, "text/html"); err != nil { serveError(writer, "error", actionID,
serverutil.ServeErrorPage(writer, gclog.Print(gclog.LErrorLog|gclog.LStaffLog, gclog.Print(gclog.LErrorLog, "Failed writing page footer: ", err.Error()), false)
"Error executing manage page header template: ", err.Error()))
return return
} }
managePageBuffer.WriteString(fmt.Sprint(output, "</body></html>"))
writer.Write(managePageBuffer.Bytes()) 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"> <form method="POST" action="/manage?action=bans">
<input type="hidden" name="do" value="add" /> <input type="hidden" name="do" value="add" />
<b>User filter:</b><br /> <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}} {{if ne .status ""}}{{.status}}<hr />{{end}}
Some fields omitted because they can not be (safely) edited from the web interface while Gochan is running. 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 /> 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 /> <h1 class="manage-header">Staff</h1><br />
<table id="stafftable" border="1"> <table id="stafftable" border="1">
<tr> <tr>

View file

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