1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-09-04 10:06:24 -07:00

Add command line option to rebuild templates on startup

This commit is contained in:
Eggbertx 2020-06-03 12:54:12 -07:00
parent c69072f00f
commit 75dbaa4676
8 changed files with 109 additions and 30 deletions

View file

@ -56,13 +56,30 @@ func main() {
func parseCommandLine() {
var newstaff string
var delstaff string
var rebuild string
var rank int
var err error
flag.StringVar(&newstaff, "newstaff", "", "<newusername>:<newpassword>")
flag.StringVar(&delstaff, "delstaff", "", "<username>")
flag.StringVar(&rebuild, "rebuild", "", "accepted values are boards,front,js, or all")
flag.IntVar(&rank, "rank", 0, "New staff member rank, to be used with -newstaff or -delstaff")
flag.Parse()
rebuildFlag := buildNone
switch rebuild {
case "boards":
rebuildFlag = buildBoards
case "front":
rebuildFlag = buildFront
case "js":
rebuildFlag = buildJS
case "all":
rebuildFlag = buildAll
}
if rebuildFlag > 0 {
startupRebuild(rebuildFlag)
}
if newstaff != "" {
arr := strings.Split(newstaff, ":")
if len(arr) < 2 || delstaff != "" {

View file

@ -258,7 +258,7 @@ func utilHandler(writer http.ResponseWriter, request *http.Request) {
return
}
building.BuildBoards(boardid)
building.BuildBoards(false, boardid)
if request.FormValue("parentid") == "0" {
http.Redirect(writer, request, "/"+board.Dir+"/res/"+strconv.Itoa(postid)+".html", http.StatusFound)
} else {
@ -330,7 +330,7 @@ func utilHandler(writer http.ResponseWriter, request *http.Request) {
_board, _ := gcsql.GetBoardFromID(post.BoardID)
building.BuildBoardPages(&_board)
}
building.BuildBoards(post.BoardID)
building.BuildBoards(false, post.BoardID)
writer.Header().Add("refresh", "4;url="+request.Referer())
fmt.Fprintf(writer, "%d deleted successfully\n", post.ID)

View file

@ -0,0 +1,56 @@
package main
import (
"os"
"github.com/gochan-org/gochan/pkg/building"
"github.com/gochan-org/gochan/pkg/gclog"
"github.com/gochan-org/gochan/pkg/gcsql"
"github.com/gochan-org/gochan/pkg/gctemplates"
"github.com/gochan-org/gochan/pkg/gcutil"
)
const (
buildNone = iota
buildBoards = 1 << (iota - 1)
buildFront
buildJS
buildAll = buildBoards | buildFront | buildJS
buildLogFlags = gclog.LErrorLog | gclog.LStdLog | gclog.LFatal
)
func startupRebuild(buildFlag int) {
var err *gcutil.GcError
gcutil.InitMinifier()
if err = gctemplates.InitTemplates(); err != nil {
gclog.Print(buildLogFlags, "Error initializing templates: ", err.Error())
}
if buildFlag&buildBoards > 0 {
gcsql.ResetBoardSectionArrays()
if err = building.BuildBoardListJSON(); err != nil {
gclog.Print(buildLogFlags, "Error building section array: ", err.Error())
}
if err = building.BuildBoards(true); err != nil {
gclog.Print(buildLogFlags, "Error building boards: ", err.Error())
}
gclog.Print(gclog.LStdLog, "Boards built successfully")
}
if buildFlag&buildJS > 0 {
if err = building.BuildJS(); err != nil {
gclog.Print(buildLogFlags, "Error building JS: ", err.Error())
}
gclog.Print(gclog.LStdLog, "JavaScript built successfully")
}
if buildFlag&buildFront > 0 {
if err = building.BuildFrontPage(); err != nil {
gclog.Print(buildLogFlags, "Error building front page: ", err.Error())
}
gclog.Print(gclog.LStdLog, "Front page built successfully")
}
gclog.Print(gclog.LStdLog, "Finished building without errors, exiting.")
os.Exit(0)
}

View file

@ -126,11 +126,11 @@ func BuildBoardPages(board *gcsql.Board) *gcutil.GcError {
board.CurrentPage = 1
// Open 1.html for writing to the first page.
boardPageFile, err := os.OpenFile(path.Join(config.Config.DocumentRoot, board.Dir, "1.html"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0777)
if err != nil {
boardPageFile, gErr := os.OpenFile(path.Join(config.Config.DocumentRoot, board.Dir, "1.html"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0777)
if gErr != nil {
return gcutil.NewError(gclog.Printf(gclog.LErrorLog,
"Failed opening /%s/board.html: %s",
board.Dir, err.Error()), false)
board.Dir, gErr.Error()), false)
}
// Render board page template to the file,
@ -142,9 +142,9 @@ func BuildBoardPages(board *gcsql.Board) *gcutil.GcError {
"threads": threads,
"board": board,
}, boardPageFile, "text/html"); err != nil {
return gcutil.NewError(gclog.Printf(gclog.LErrorLog,
"Failed building /%s/: %s",
board.Dir, err.Error()), false)
err.Message = gclog.Printf(gclog.LErrorLog,
"Failed building /%s/: %s", board.Dir, err.Message)
return err
}
return nil
}
@ -188,8 +188,9 @@ func BuildBoardPages(board *gcsql.Board) *gcutil.GcError {
gcsql.Post{BoardID: board.ID},
},
}, currentPageFile, "text/html"); err != nil {
return gcutil.NewError(gclog.Printf(gclog.LErrorLog,
"Failed building /%s/ boardpage: %s", board.Dir, err.Error()), false)
err.Message = gclog.Printf(gclog.LErrorLog,
"Failed building /%s/ boardpage: %s", board.Dir, err.Message)
return err
}
// Collect up threads for this page.
@ -213,8 +214,8 @@ func BuildBoardPages(board *gcsql.Board) *gcutil.GcError {
}
// BuildBoards builds the specified board IDs, or all boards if no arguments are passed
// The return value is a string of HTML with debug information produced by the build process.
func BuildBoards(which ...int) *gcutil.GcError {
// it returns any errors that were encountered
func BuildBoards(verbose bool, which ...int) *gcutil.GcError {
var boards []gcsql.Board
var err *gcutil.GcError
@ -239,6 +240,9 @@ func BuildBoards(which ...int) *gcutil.GcError {
"Error building /%s/: %s", board.Dir, err.Error())
return err
}
if verbose {
gclog.Printf(gclog.LStdLog, "Built /%s/ successfully", board.Dir)
}
}
return nil
}

View file

@ -95,20 +95,21 @@ func BuildBoardListJSON() *gcutil.GcError {
func BuildJS() *gcutil.GcError {
// minify gochan.js (if enabled)
gochanMinJSPath := path.Join(config.Config.DocumentRoot, "javascript", "gochan.min.js")
gochanMinJSFile, err := os.OpenFile(gochanMinJSPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
gochanMinJSFile, gErr := os.OpenFile(gochanMinJSPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if gErr != nil {
return gcutil.NewError(gclog.Printf(gclog.LErrorLog,
"Error opening %q for writing: %s", gochanMinJSPath, err.Error()), false)
"Error opening %q for writing: %s", gochanMinJSPath, gErr.Error()), false)
}
defer gochanMinJSFile.Close()
gochanJSPath := path.Join(config.Config.DocumentRoot, "javascript", "gochan.js")
gochanJSBytes, err := ioutil.ReadFile(gochanJSPath)
if err != nil {
gochanJSBytes, gErr := ioutil.ReadFile(gochanJSPath)
if gErr != nil {
return gcutil.NewError(gclog.Printf(gclog.LErrorLog,
"Error opening %q for writing: %s", gochanJSPath, err.Error()), false)
"Error opening %q for writing: %s", gochanJSPath, gErr.Error()), false)
}
if _, err := gcutil.MinifyWriter(gochanMinJSFile, gochanJSBytes, "text/javascript"); err != nil {
_, err := gcutil.MinifyWriter(gochanMinJSFile, gochanJSBytes, "text/javascript")
if err != nil {
config.Config.UseMinifiedGochanJS = false
return gcutil.NewError(gclog.Printf(gclog.LErrorLog,
"Error minifying %q: %s:", gochanMinJSPath, err.Error()), false)
@ -117,14 +118,15 @@ func BuildJS() *gcutil.GcError {
// build consts.js from template
if err = gctemplates.InitTemplates("js"); err != nil {
return gcutil.NewError(gclog.Printf(gclog.LErrorLog,
"Error loading consts.js template: ", err.Error()), false)
err.Message = gclog.Println(gclog.LErrorLog,
"Error loading consts.js template:", err.Error())
return err
}
constsJSPath := path.Join(config.Config.DocumentRoot, "javascript", "consts.js")
constsJSFile, err := os.OpenFile(constsJSPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
constsJSFile, gErr := os.OpenFile(constsJSPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if gErr != nil {
return gcutil.NewError(gclog.Printf(gclog.LErrorLog,
"Error opening %q for writing: %s", constsJSPath, err.Error()), false)
"Error opening %q for writing: %s", constsJSPath, gErr.Error()), false)
}
defer constsJSFile.Close()

View file

@ -557,7 +557,7 @@ var manageFunctions = map[string]ManageFunction{
break
} else {
boardCreationStatus = "Board created successfully"
building.BuildBoards()
building.BuildBoards(false)
gcsql.ResetBoardSectionArrays()
gclog.Print(gclog.LStaffLog, "Boards rebuilt successfully")
done = true
@ -668,7 +668,7 @@ var manageFunctions = map[string]ManageFunction{
return "", err
}
if err = building.BuildBoards(); err != nil {
if err = building.BuildBoards(false); err != nil {
return "", err
}
@ -685,7 +685,7 @@ var manageFunctions = map[string]ManageFunction{
if err = gctemplates.InitTemplates(); err != nil {
return "", err
}
return "Boards built successfully", building.BuildBoards()
return "Boards built successfully", building.BuildBoards(false)
}},
"reparsehtml": {
Title: "Reparse HTML",
@ -714,7 +714,7 @@ var manageFunctions = map[string]ManageFunction{
}
htmlOut += "Done building board list JSON<hr />"
if err = building.BuildBoards(); err != nil {
if err = building.BuildBoards(false); err != nil {
return "", err
}
htmlOut += "Done building boards<hr />"

View file

@ -86,7 +86,7 @@ func ServeCaptcha(writer http.ResponseWriter, request *http.Request) {
// and redirect to the thread
gcsql.InsertPost(&gcsql.TempPosts[tempPostIndex], emailCommand == "noko")
building.BuildBoards(gcsql.TempPosts[tempPostIndex].BoardID)
building.BuildBoards(false, gcsql.TempPosts[tempPostIndex].BoardID)
building.BuildFrontPage()
url := gcsql.TempPosts[tempPostIndex].GetURL(false)

View file

@ -381,7 +381,7 @@ func MakePost(writer http.ResponseWriter, request *http.Request) {
}
// rebuild the board page
building.BuildBoards(post.BoardID)
building.BuildBoards(false, post.BoardID)
building.BuildFrontPage()
if emailCommand == "noko" {