1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-17 10:56:24 -07:00

Move minifier functions to serverutil package

gcutil should only be used for stuff that doesn't require configuration
This commit is contained in:
Eggbertx 2021-03-25 14:42:46 -07:00
parent 8ccd217ff0
commit 2382b30af1
14 changed files with 28 additions and 62 deletions

View file

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

View file

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

4
go.mod
View file

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

5
go.sum
View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,4 +1,4 @@
package gcutil
package serverutil
import (
"html/template"

View file

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