From c4d8fdb0f01611b84a8ddaee3ed855ee960ed543 Mon Sep 17 00:00:00 2001 From: iacore Date: Tue, 27 Aug 2024 19:54:37 +0000 Subject: [PATCH] organize files add http client with better maxidleconnections --- core/rankingCalendar.go | 3 ++- core/requests.go | 5 +++-- main.go | 5 +++-- routes/actions.go | 3 ++- routes/discovery.go | 7 ++++--- routes/proxy.go | 5 ++++- routes/settings.go | 3 ++- routes/tag.go | 3 ++- routes/{render_types.go => types.go} | 15 ++++++++++----- {utils => template}/partialURL.go | 19 +++++++++++++------ {routes => template}/render.go | 4 ++-- {routes => template}/render_test.go | 18 ++++++++++-------- {utils => template}/templateFunctions.go | 10 +++------- utils/http_client.go | 9 +++++++++ 14 files changed, 69 insertions(+), 40 deletions(-) rename routes/{render_types.go => types.go} (90%) rename {utils => template}/partialURL.go (67%) rename {routes => template}/render.go (96%) rename {routes => template}/render_test.go (79%) rename {utils => template}/templateFunctions.go (97%) create mode 100644 utils/http_client.go diff --git a/core/rankingCalendar.go b/core/rankingCalendar.go index fa3ace3..b36d0fa 100644 --- a/core/rankingCalendar.go +++ b/core/rankingCalendar.go @@ -7,6 +7,7 @@ import ( "time" "codeberg.org/vnpower/pixivfe/v2/session" + "codeberg.org/vnpower/pixivfe/v2/utils" "golang.org/x/net/html" ) @@ -49,7 +50,7 @@ func GetRankingCalendar(r *http.Request, mode string, year, month int) (template // Value: token, // }) - resp, err := http.DefaultClient.Do(req) + resp, err := utils.HttpClient.Do(req) if err != nil { return "", err } diff --git a/core/requests.go b/core/requests.go index 80f024e..fb079d1 100644 --- a/core/requests.go +++ b/core/requests.go @@ -12,6 +12,7 @@ import ( "time" config "codeberg.org/vnpower/pixivfe/v2/config" + "codeberg.org/vnpower/pixivfe/v2/utils" "github.com/tidwall/gjson" ) @@ -90,8 +91,7 @@ func webAPIRequest(context context.Context, URL, token string) HttpResponse { } // Make the request - resp, err := http.DefaultClient.Do(req) - + resp, err := utils.HttpClient.Do(req) if err != nil { return HttpResponse{ Ok: false, @@ -100,6 +100,7 @@ func webAPIRequest(context context.Context, URL, token string) HttpResponse { Message: fmt.Sprintf("Failed to send a request to %s\n.", URL), } } + defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { diff --git a/main.go b/main.go index 4e780b6..53804ae 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "codeberg.org/vnpower/pixivfe/v2/core" "codeberg.org/vnpower/pixivfe/v2/routes" "codeberg.org/vnpower/pixivfe/v2/session" + "codeberg.org/vnpower/pixivfe/v2/template" "codeberg.org/vnpower/pixivfe/v2/utils" ) @@ -55,7 +56,7 @@ func main() { if config.GlobalServerConfig.InDevelopment { core.CreateResponseAuditFolder() } - routes.InitTemplatingEngine(config.GlobalServerConfig.InDevelopment) + template.InitTemplatingEngine(config.GlobalServerConfig.InDevelopment) router := defineRoutes() @@ -89,7 +90,7 @@ func main() { code := http.StatusInternalServerError w.WriteHeader(code) // Send custom error page - err = routes.Render(w, r, routes.Data_error{Title: "Error", Error: err}) + err = template.Render(w, r, routes.Data_error{Title: "Error", Error: err}) if err != nil { err = utils.SendString(w, (fmt.Sprintf("Internal Server Error: %s", err))) if err != nil { diff --git a/routes/actions.go b/routes/actions.go index 4a50b96..67c869e 100644 --- a/routes/actions.go +++ b/routes/actions.go @@ -34,10 +34,11 @@ func pixivPostRequest(r *http.Request, url, payload, token, csrf string, isJSON // Value: token, // }) - resp, err := http.DefaultClient.Do(req) + resp, err := utils.HttpClient.Do(req) if err != nil { return errors.New("Failed to do this action.") } + defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { diff --git a/routes/discovery.go b/routes/discovery.go index 7d3620d..0cc6f21 100644 --- a/routes/discovery.go +++ b/routes/discovery.go @@ -1,9 +1,10 @@ package routes import ( - "codeberg.org/vnpower/pixivfe/v2/core" - "codeberg.org/vnpower/pixivfe/v2/utils" "net/http" + + "codeberg.org/vnpower/pixivfe/v2/core" + "codeberg.org/vnpower/pixivfe/v2/template" ) func DiscoveryPage(w http.ResponseWriter, r *http.Request) error { @@ -14,7 +15,7 @@ func DiscoveryPage(w http.ResponseWriter, r *http.Request) error { return err } - urlc := utils.PartialURL{Path: "discovery", Query: map[string]string{"mode": mode}} + urlc := template.PartialURL{Path: "discovery", Query: map[string]string{"mode": mode}} return Render(w, r, Data_discovery{Artworks: works, Title: "Discovery", Queries: urlc}) } diff --git a/routes/proxy.go b/routes/proxy.go index 3070281..6cbe55b 100644 --- a/routes/proxy.go +++ b/routes/proxy.go @@ -4,14 +4,17 @@ import ( "fmt" "io" "net/http" + + "codeberg.org/vnpower/pixivfe/v2/utils" ) func copyRequest(w http.ResponseWriter, req *http.Request) error { // Make the request - resp, err := http.DefaultClient.Do(req) + resp, err := utils.HttpClient.Do(req) if err != nil { return err } + defer resp.Body.Close() // copy headers header := w.Header() diff --git a/routes/settings.go b/routes/settings.go index 9443248..b92889b 100644 --- a/routes/settings.go +++ b/routes/settings.go @@ -37,10 +37,11 @@ func setToken(w http.ResponseWriter, r *http.Request) error { Value: token, }) - resp, err := http.DefaultClient.Do(req) + resp, err := utils.HttpClient.Do(req) if err != nil { return errors.New("Cannot authorize with supplied token.") } + defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { diff --git a/routes/tag.go b/routes/tag.go index dcd9e8c..7e6fbd5 100644 --- a/routes/tag.go +++ b/routes/tag.go @@ -6,6 +6,7 @@ import ( "strconv" "codeberg.org/vnpower/pixivfe/v2/core" + "codeberg.org/vnpower/pixivfe/v2/template" "codeberg.org/vnpower/pixivfe/v2/utils" ) @@ -49,7 +50,7 @@ func TagPage(w http.ResponseWriter, r *http.Request) error { return err } - urlc := utils.PartialURL{Path: "tags", Query: queries.ReturnMap()} + urlc := template.PartialURL{Path: "tags", Query: queries.ReturnMap()} return Render(w, r, Data_tag{Title: "Results for " + name, Tag: tag, Data: *result, QueriesC: urlc, TrueTag: param, Page: pageInt}) } diff --git a/routes/render_types.go b/routes/types.go similarity index 90% rename from routes/render_types.go rename to routes/types.go index 1a09397..74ee9d6 100644 --- a/routes/render_types.go +++ b/routes/types.go @@ -1,13 +1,18 @@ package routes import ( - "html/template" + html "html/template" + "net/http" "codeberg.org/vnpower/pixivfe/v2/core" - "codeberg.org/vnpower/pixivfe/v2/utils" + "codeberg.org/vnpower/pixivfe/v2/template" "codeberg.org/vnpower/pixivision" ) +func Render[T any](w http.ResponseWriter, r *http.Request, data T) error { + return template.Render(w, r, data) +} + // Tutorial: adding new types in this file // Whenever you add new types, update `TestTemplates` in render_test.go to include the type in the test // Do not use pointer in Data_* struct. faker will insert nil. @@ -55,7 +60,7 @@ type Data_unauthorized struct{} type Data_discovery struct { Artworks []core.ArtworkBrief Title string - Queries utils.PartialURL + Queries template.PartialURL } type Data_novelDiscovery struct { Novels []core.NovelBrief @@ -97,7 +102,7 @@ type Data_rank struct { } type Data_rankingCalendar struct { Title string - Render template.HTML + Render html.HTML Mode string Year int MonthBefore DateWrap @@ -112,7 +117,7 @@ type Data_tag struct { Title string Tag core.TagDetail Data core.SearchResult - QueriesC utils.PartialURL + QueriesC template.PartialURL TrueTag string Page int } diff --git a/utils/partialURL.go b/template/partialURL.go similarity index 67% rename from utils/partialURL.go rename to template/partialURL.go index b152b16..3f27c83 100644 --- a/utils/partialURL.go +++ b/template/partialURL.go @@ -1,19 +1,26 @@ -package utils +package template -import "fmt" +import ( + "fmt" + "strings" +) type PartialURL struct { Path string Query map[string]string } +func LowercaseFirstChar(s string) string { + return strings.ToLower(s[0:1]) + s[1:] +} + // Turn `url` into /path?other_key=other_value&`key`= -func unfinishedQuery(url PartialURL, key string) string { +func UnfinishedQuery(url PartialURL, key string) string { result := fmt.Sprintf("/%s", url.Path) first_query_pair := true for k, v := range url.Query { - k = lowercaseFirstChar(k) + k = LowercaseFirstChar(k) if k == key { continue @@ -45,6 +52,6 @@ func unfinishedQuery(url PartialURL, key string) string { return result } -func replaceQuery(url PartialURL, key string, value string) string { - return unfinishedQuery(url, key) + value +func ReplaceQuery(url PartialURL, key string, value string) string { + return UnfinishedQuery(url, key) + value } diff --git a/routes/render.go b/template/render.go similarity index 96% rename from routes/render.go rename to template/render.go index 0900020..0f5dc85 100644 --- a/routes/render.go +++ b/template/render.go @@ -1,4 +1,4 @@ -package routes +package template import ( "io" @@ -27,7 +27,7 @@ func InitTemplatingEngine(DisableCache bool) { jet.NewOSFileSystemLoader("assets/views"), ) } - for fn_name, fn := range utils.GetTemplateFunctions() { + for fn_name, fn := range GetTemplateFunctions() { views.AddGlobal(fn_name, fn) } } diff --git a/routes/render_test.go b/template/render_test.go similarity index 79% rename from routes/render_test.go rename to template/render_test.go index 88b5fa6..c0ed6da 100644 --- a/routes/render_test.go +++ b/template/render_test.go @@ -1,4 +1,4 @@ -package routes +package template_test import ( "io" @@ -8,6 +8,8 @@ import ( "strings" "testing" + . "codeberg.org/vnpower/pixivfe/v2/routes" + template "codeberg.org/vnpower/pixivfe/v2/template" "github.com/CloudyKit/jet/v6" "github.com/go-faker/faker/v4" ) @@ -43,7 +45,7 @@ func TestMain(m *testing.M) { if err != nil { panic(err) } - InitTemplatingEngine(false) + template.InitTemplatingEngine(false) m.Run() } @@ -70,16 +72,16 @@ func manualTest[T any](t *testing.T, data T) { variables := jet.VarMap{} for k, v := range map[string]any{ - "BaseURL": fakeData[string](), - "PageURL": fakeData[string](), - "LoggedIn": fakeData[bool](), - "Queries": fakeData[map[string]string](), - "CookieList": fakeData[map[string]string](), + "BaseURL": fakeData[string](), + "PageURL": fakeData[string](), + "LoggedIn": fakeData[bool](), + "Queries": fakeData[map[string]string](), + "CookieList": fakeData[map[string]string](), } { variables.Set(k, v) } - err := RenderInner(io.Discard, variables, data) + err := template.RenderInner(io.Discard, variables, data) if err != nil { template_name, _ := strings.CutPrefix(reflect.TypeFor[T]().Name(), "Data_") diff --git a/utils/templateFunctions.go b/template/templateFunctions.go similarity index 97% rename from utils/templateFunctions.go rename to template/templateFunctions.go index 55de643..d770ca8 100644 --- a/utils/templateFunctions.go +++ b/template/templateFunctions.go @@ -1,4 +1,4 @@ -package utils +package template import ( "fmt" @@ -247,10 +247,6 @@ func SwitchButtonAttributes(baseURL, selection, currentSelection string) string return fmt.Sprintf(`href=%s%s class=switch-button selected=%s`, baseURL, selection, cur) } -func lowercaseFirstChar(s string) string { - return strings.ToLower(s[0:1]) + s[1:] -} - func GetTemplateFunctions() map[string]any { return map[string]any{ "parseEmojis": func(s string) template.HTML { @@ -316,8 +312,8 @@ func GetTemplateFunctions() map[string]any { "floor": func(i float64) int { return int(math.Floor(i)) }, - "unfinishedQuery": unfinishedQuery, - "replaceQuery": replaceQuery, + "unfinishedQuery": UnfinishedQuery, + "replaceQuery": ReplaceQuery, // "AttrGen": SwitchButtonAttributes, } } diff --git a/utils/http_client.go b/utils/http_client.go new file mode 100644 index 0000000..1f7f710 --- /dev/null +++ b/utils/http_client.go @@ -0,0 +1,9 @@ +package utils + +import "net/http" + +var HttpClient = &http.Client{ + Transport: &http.Transport{ + MaxIdleConnsPerHost: 20, + }, +}