mirror of
https://codeberg.org/VnPower/PixivFE
synced 2024-12-06 19:16:23 +01:00
organize files
add http client with better maxidleconnections
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
+3
-2
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+2
-1
@@ -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 {
|
||||
|
||||
+4
-3
@@ -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})
|
||||
}
|
||||
|
||||
+4
-1
@@ -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()
|
||||
|
||||
+2
-1
@@ -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 {
|
||||
|
||||
+2
-1
@@ -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})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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_")
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package utils
|
||||
|
||||
import "net/http"
|
||||
|
||||
var HttpClient = &http.Client{
|
||||
Transport: &http.Transport{
|
||||
MaxIdleConnsPerHost: 20,
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user