From 2382b30af17db68bc4f7fa201e11d0f606ef916b Mon Sep 17 00:00:00 2001 From: Eggbertx Date: Thu, 25 Mar 2021 14:42:46 -0700 Subject: [PATCH] Move minifier functions to serverutil package gcutil should only be used for stuff that doesn't require configuration --- cmd/gochan/main.go | 4 ++-- cmd/gochan/startupbuild.go | 4 ++-- go.mod | 4 ++-- go.sum | 5 +++++ pkg/building/boards.go | 7 +++--- pkg/building/building.go | 8 +++---- pkg/building/threads.go | 4 ++-- pkg/config/config.go | 30 -------------------------- pkg/gcutil/util.go | 10 --------- pkg/posting/bans.go | 2 +- pkg/posting/captcha.go | 4 ++-- pkg/posting/post.go | 2 +- pkg/{gcutil => serverutil}/minifier.go | 2 +- pkg/serverutil/util.go | 4 ++-- 14 files changed, 28 insertions(+), 62 deletions(-) rename pkg/{gcutil => serverutil}/minifier.go (98%) diff --git a/cmd/gochan/main.go b/cmd/gochan/main.go index 108a9c18..1c0c1393 100644 --- a/cmd/gochan/main.go +++ b/cmd/gochan/main.go @@ -12,8 +12,8 @@ import ( "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" "github.com/gochan-org/gochan/pkg/posting" + "github.com/gochan-org/gochan/pkg/serverutil" _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" @@ -39,7 +39,7 @@ func main() { config.Config.DBusername, config.Config.DBpassword, config.Config.DBprefix) gcsql.CheckAndInitializeDatabase(config.Config.DBtype) parseCommandLine() - gcutil.InitMinifier() + serverutil.InitMinifier() posting.InitCaptcha() if err := gctemplates.InitTemplates(); err != nil { diff --git a/cmd/gochan/startupbuild.go b/cmd/gochan/startupbuild.go index e7ed55e6..e26e010a 100644 --- a/cmd/gochan/startupbuild.go +++ b/cmd/gochan/startupbuild.go @@ -7,7 +7,7 @@ import ( "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" + "github.com/gochan-org/gochan/pkg/serverutil" ) const ( @@ -21,7 +21,7 @@ const ( func startupRebuild(buildFlag int) { var err error - gcutil.InitMinifier() + serverutil.InitMinifier() if err = gctemplates.InitTemplates(); err != nil { gclog.Print(buildLogFlags, "Error initializing templates: ", err.Error()) } diff --git a/go.mod b/go.mod index 460bd1a0..cdafae37 100755 --- a/go.mod +++ b/go.mod @@ -14,6 +14,6 @@ require ( github.com/tdewolff/minify v2.3.6+incompatible github.com/tdewolff/parse v2.3.4+incompatible // indirect github.com/tdewolff/test v1.0.6 // indirect - golang.org/x/crypto v0.0.0-20210317152858-513c2a44f670 - golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4 + golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 + golang.org/x/net v0.0.0-20210324205630-d1beb07c2056 ) diff --git a/go.sum b/go.sum index 4b84d47f..6dd6ce35 100755 --- a/go.sum +++ b/go.sum @@ -43,6 +43,8 @@ golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqt golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20210317152858-513c2a44f670 h1:gzMM0EjIYiRmJI3+jBdFuoynZlpxa2JQZsolKu09BXo= golang.org/x/crypto v0.0.0-20210317152858-513c2a44f670/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w= +golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/image v0.0.0-20190501045829-6d32002ffd75/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U= golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= @@ -67,12 +69,15 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxW golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4 h1:b0LrWgu8+q7z4J+0Y3Umo5q1dL7NXBkKBWkaVkAq17E= golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= +golang.org/x/net v0.0.0-20210324205630-d1beb07c2056 h1:sANdAef76Ioam9aQUUdcAqricwY/WUaMc4+7LY4eGg8= +golang.org/x/net v0.0.0-20210324205630-d1beb07c2056/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= diff --git a/pkg/building/boards.go b/pkg/building/boards.go index 950482b8..5641583d 100644 --- a/pkg/building/boards.go +++ b/pkg/building/boards.go @@ -14,6 +14,7 @@ import ( "github.com/gochan-org/gochan/pkg/gcsql" "github.com/gochan-org/gochan/pkg/gctemplates" "github.com/gochan-org/gochan/pkg/gcutil" + "github.com/gochan-org/gochan/pkg/serverutil" ) const ( @@ -137,7 +138,7 @@ func BuildBoardPages(board *gcsql.Board) error { // Render board page template to the file, // packaging the board/section list, threads, and board info - if err = gcutil.MinifyTemplate(gctemplates.BoardPage, map[string]interface{}{ + if err = serverutil.MinifyTemplate(gctemplates.BoardPage, map[string]interface{}{ "config": config.Config, "boards": gcsql.AllBoards, "sections": gcsql.AllSections, @@ -179,7 +180,7 @@ func BuildBoardPages(board *gcsql.Board) error { defer currentPageFile.Close() // Render the boardpage template - if err = gcutil.MinifyTemplate(gctemplates.BoardPage, map[string]interface{}{ + if err = serverutil.MinifyTemplate(gctemplates.BoardPage, map[string]interface{}{ "config": config.Config, "boards": gcsql.AllBoards, "sections": gcsql.AllSections, @@ -282,7 +283,7 @@ func BuildCatalog(boardID int) string { threadInterfaces = append(threadInterfaces, thread) } - if err = gcutil.MinifyTemplate(gctemplates.Catalog, map[string]interface{}{ + if err = serverutil.MinifyTemplate(gctemplates.Catalog, map[string]interface{}{ "boards": gcsql.AllBoards, "config": config.Config, "board": board, diff --git a/pkg/building/building.go b/pkg/building/building.go index cf665cb0..fbf90145 100644 --- a/pkg/building/building.go +++ b/pkg/building/building.go @@ -10,7 +10,7 @@ import ( "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" + "github.com/gochan-org/gochan/pkg/serverutil" ) // BuildFrontPage builds the front page using templates/front.html @@ -42,7 +42,7 @@ func BuildFrontPage() error { } } - if err = gcutil.MinifyTemplate(gctemplates.FrontPage, map[string]interface{}{ + if err = serverutil.MinifyTemplate(gctemplates.FrontPage, map[string]interface{}{ "config": config.Config, "sections": gcsql.AllSections, "boards": gcsql.AllBoards, @@ -84,7 +84,7 @@ func BuildBoardListJSON() error { gclog.Print(gclog.LErrorLog, "Failed to create boards.json: ", err.Error())) } - if _, err = gcutil.MinifyWriter(boardListFile, boardJSON, "application/json"); err != nil { + if _, err = serverutil.MinifyWriter(boardListFile, boardJSON, "application/json"); err != nil { return errors.New( gclog.Print(gclog.LErrorLog, "Failed writing boards.json file: ", err.Error())) } @@ -107,7 +107,7 @@ func BuildJS() error { } defer constsJSFile.Close() - if err = gcutil.MinifyTemplate(gctemplates.JsConsts, config.Config, constsJSFile, "text/javascript"); err != nil { + if err = serverutil.MinifyTemplate(gctemplates.JsConsts, config.Config, constsJSFile, "text/javascript"); err != nil { return errors.New(gclog.Printf(gclog.LErrorLog, "Error building %q: %s", constsJSPath, err.Error())) } diff --git a/pkg/building/threads.go b/pkg/building/threads.go index b8ec20f8..0b9a94c3 100644 --- a/pkg/building/threads.go +++ b/pkg/building/threads.go @@ -11,7 +11,7 @@ import ( "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" + "github.com/gochan-org/gochan/pkg/serverutil" ) // BuildThreads builds thread(s) given a boardid, or if all = false, also given a threadid. @@ -73,7 +73,7 @@ func BuildThreadPages(op *gcsql.Post) error { } // render thread page - if err = gcutil.MinifyTemplate(gctemplates.ThreadPage, map[string]interface{}{ + if err = serverutil.MinifyTemplate(gctemplates.ThreadPage, map[string]interface{}{ "config": config.Config, "boards": gcsql.AllBoards, "board": board, diff --git a/pkg/config/config.go b/pkg/config/config.go index eb893baf..185647de 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -5,8 +5,6 @@ import ( "io/ioutil" "net" "reflect" - - "github.com/gochan-org/gochan/pkg/gclog" ) const ( @@ -170,34 +168,6 @@ func (cfg *GochanConfig) ToMap() map[string]interface{} { return out } -func (cfg *GochanConfig) checkString(val, defaultVal string, critical bool, msg string) string { - if val == "" { - val = defaultVal - flags := gclog.LStdLog | gclog.LErrorLog - if critical { - flags |= gclog.LFatal - } - if msg != "" { - gclog.Print(flags, msg) - } - } - return val -} - -func (cfg *GochanConfig) checkInt(val, defaultVal int, critical bool, msg string) int { - if val == 0 { - val = defaultVal - flags := gclog.LStdLog | gclog.LErrorLog - if critical { - flags |= gclog.LFatal - } - if msg != "" { - gclog.Print(flags, msg) - } - } - return val -} - // ValidateValues checks to make sure that the configuration options are usable // (e.g., ListenIP is a valid IP address, Port isn't a negative number, etc) func (cfg *GochanConfig) ValidateValues() error { diff --git a/pkg/gcutil/util.go b/pkg/gcutil/util.go index bba1f357..c54a8d7f 100644 --- a/pkg/gcutil/util.go +++ b/pkg/gcutil/util.go @@ -53,16 +53,6 @@ func Sha1Sum(str string) string { return fmt.Sprintf("%x", hash.Sum(nil)) } -/* func byteByByteReplace(input, from, to string) string { - if len(from) != len(to) { - return "" - } - for i := 0; i < len(from); i++ { - input = strings.Replace(input, from[i:i+1], to[i:i+1], -1) - } - return input -} */ - // CloseHandle closes the given closer object only if it is non-nil func CloseHandle(handle io.Closer) { if handle != nil { diff --git a/pkg/posting/bans.go b/pkg/posting/bans.go index 8b47911f..ee86933b 100644 --- a/pkg/posting/bans.go +++ b/pkg/posting/bans.go @@ -51,7 +51,7 @@ func BanHandler(writer http.ResponseWriter, request *http.Request) { return } - if err = gcutil.MinifyTemplate(gctemplates.Banpage, map[string]interface{}{ + if err = serverutil.MinifyTemplate(gctemplates.Banpage, map[string]interface{}{ "config": config.Config, "ban": banStatus, "banBoards": banStatus.Boards, "post": gcsql.Post{}, }, writer, "text/html"); err != nil { serverutil.ServeErrorPage(writer, gclog.Print(gclog.LErrorLog, diff --git a/pkg/posting/captcha.go b/pkg/posting/captcha.go index 2bd1391c..9436915d 100644 --- a/pkg/posting/captcha.go +++ b/pkg/posting/captcha.go @@ -66,7 +66,7 @@ func ServeCaptcha(writer http.ResponseWriter, request *http.Request) { writer.Header().Add("Content-Type", "application/json") str, _ := gcutil.MarshalJSON(captchaStruct, false) - gcutil.MinifyWriter(writer, []byte(str), "application/json") + serverutil.MinifyWriter(writer, []byte(str), "application/json") return } if request.FormValue("reload") == "Reload" { @@ -102,7 +102,7 @@ func ServeCaptcha(writer http.ResponseWriter, request *http.Request) { captchaStruct.Result = "Incorrect CAPTCHA" } } - if err = gcutil.MinifyTemplate(gctemplates.Captcha, captchaStruct, writer, "text/html"); err != nil { + if err = serverutil.MinifyTemplate(gctemplates.Captcha, captchaStruct, writer, "text/html"); err != nil { fmt.Fprintf(writer, gclog.Print(gclog.LErrorLog, "Error executing captcha template: ", err.Error())) } diff --git a/pkg/posting/post.go b/pkg/posting/post.go index a4db58d2..bb7cd158 100644 --- a/pkg/posting/post.go +++ b/pkg/posting/post.go @@ -151,7 +151,7 @@ func MakePost(writer http.ResponseWriter, request *http.Request) { if banStatus != nil && banStatus.IsBanned(postBoard.Dir) { var banpageBuffer bytes.Buffer - if err = gcutil.MinifyTemplate(gctemplates.Banpage, map[string]interface{}{ + if err = serverutil.MinifyTemplate(gctemplates.Banpage, map[string]interface{}{ "config": config.Config, "ban": banStatus, "banBoards": boards[post.BoardID-1].Dir, }, writer, "text/html"); err != nil { serverutil.ServeErrorPage(writer, diff --git a/pkg/gcutil/minifier.go b/pkg/serverutil/minifier.go similarity index 98% rename from pkg/gcutil/minifier.go rename to pkg/serverutil/minifier.go index 8ed10c17..c82d4fa4 100644 --- a/pkg/gcutil/minifier.go +++ b/pkg/serverutil/minifier.go @@ -1,4 +1,4 @@ -package gcutil +package serverutil import ( "html/template" diff --git a/pkg/serverutil/util.go b/pkg/serverutil/util.go index 03754af5..cb713e3b 100644 --- a/pkg/serverutil/util.go +++ b/pkg/serverutil/util.go @@ -12,7 +12,7 @@ import ( // ServeErrorPage shows a general error page if something goes wrong func ServeErrorPage(writer http.ResponseWriter, err string) { - gcutil.MinifyTemplate(gctemplates.ErrorPage, map[string]interface{}{ + MinifyTemplate(gctemplates.ErrorPage, map[string]interface{}{ "config": config.Config, "ErrorTitle": "Error :c", // "ErrorImage": "/error/lol 404.gif", @@ -29,7 +29,7 @@ func ServeNotFound(writer http.ResponseWriter, request *http.Request) { if err != nil { writer.Write([]byte("Requested page not found, and /error/404.html not found")) } else { - gcutil.MinifyWriter(writer, errorPage, "text/html") + MinifyWriter(writer, errorPage, "text/html") } gclog.Printf(gclog.LAccessLog, "Error: 404 Not Found from %s @ %s", gcutil.GetRealIP(request), request.URL.Path) }