From d41e98ab8ba41a4e4423722af94f817549749569 Mon Sep 17 00:00:00 2001 From: perennial Date: Sun, 13 Oct 2024 16:27:35 +1100 Subject: [PATCH] run gofumpt https://github.com/mvdan/gofumpt --- core/appapi.go | 18 ++++++++++-------- core/index.go | 3 --- core/mangaseries.go | 3 +-- core/newest.go | 3 ++- core/novel.go | 15 ++++++++------- core/novelseries.go | 2 +- core/personal.go | 3 ++- core/pixivision.go | 11 ++++++----- core/ranking.go | 2 +- core/tag.go | 2 +- core/user.go | 5 +---- i18n/crawler/main.go | 6 ++++-- i18n/lookup.go | 1 + server/audit/audit_init.go | 8 +++++--- server/audit/spans.go | 13 ++++++++++++- server/middleware/error_handler.go | 1 - server/routes/about.go | 3 ++- server/routes/mangaseries.go | 3 +-- server/routes/newest.go | 3 ++- server/routes/novelseries.go | 3 +-- server/routes/pixivision.go | 4 ++-- server/routes/proxy.go | 2 -- server/routes/ranking.go | 2 +- server/routes/rankingCalendar.go | 3 +-- server/routes/types.go | 8 +++++--- server/routes/user.go | 2 +- server/session/cookie.go | 3 +-- server/template/localized_loader.go | 2 +- server/template/render_test.go | 1 - server/template/templateFunctions.go | 16 ++++++++-------- 30 files changed, 81 insertions(+), 70 deletions(-) diff --git a/core/appapi.go b/core/appapi.go index 09bd87b..a36fbdd 100644 --- a/core/appapi.go +++ b/core/appapi.go @@ -17,12 +17,14 @@ import ( "codeberg.org/vnpower/pixivfe/v2/server/utils" ) -const USER_AGENT = "PixivAndroidApp/5.0.234 (Android 11; Pixel 5)" -const REDIRECT_URI = "https://app-api.pixiv.net/web/v1/users/auth/pixiv/callback" -const LOGIN_URL = "https://app-api.pixiv.net/web/v1/login" -const AUTH_TOKEN_URL = "https://oauth.secure.pixiv.net/auth/token" -const CLIENT_ID = "MOBrBDS8blbauoSck0ZfDbtuzpyT" -const CLIENT_SECRET = "lsACyCD94FhDUtGTXi3QzcFE2uU1hqtDaKeqrdwj" +const ( + USER_AGENT = "PixivAndroidApp/5.0.234 (Android 11; Pixel 5)" + REDIRECT_URI = "https://app-api.pixiv.net/web/v1/users/auth/pixiv/callback" + LOGIN_URL = "https://app-api.pixiv.net/web/v1/login" + AUTH_TOKEN_URL = "https://oauth.secure.pixiv.net/auth/token" + CLIENT_ID = "MOBrBDS8blbauoSck0ZfDbtuzpyT" + CLIENT_SECRET = "lsACyCD94FhDUtGTXi3QzcFE2uU1hqtDaKeqrdwj" +) type AppAPICredentials struct { AccessToken string `json:"access_token"` @@ -75,7 +77,7 @@ func oauth_pkce() (string, string) { func AppAPIRefresh(r *http.Request, refresh_token string) AppAPICredentials { var credentials AppAPICredentials - var body = []byte(fmt.Sprintf(`client_id=%s&client_secret=%s&grant_type=refresh_token&include_policy=true&refresh_token=%s`, CLIENT_ID, CLIENT_SECRET, refresh_token)) + body := []byte(fmt.Sprintf(`client_id=%s&client_secret=%s&grant_type=refresh_token&include_policy=true&refresh_token=%s`, CLIENT_ID, CLIENT_SECRET, refresh_token)) req, err := http.NewRequestWithContext(r.Context(), "POST", AUTH_TOKEN_URL, bytes.NewBuffer(body)) if err != nil { panic(err) @@ -110,7 +112,7 @@ func AppAPILogin(r *http.Request) AppAPICredentials { // Change this. fmt.Scanln(&s) - var body = []byte(fmt.Sprintf(`client_id=%s&client_secret=%s&code=%s&code_verifier=%s&grant_type=authorization_code&include_policy=true&redirect_uri=%s`, CLIENT_ID, CLIENT_SECRET, s, code_verifier, REDIRECT_URI)) + body := []byte(fmt.Sprintf(`client_id=%s&client_secret=%s&code=%s&code_verifier=%s&grant_type=authorization_code&include_policy=true&redirect_uri=%s`, CLIENT_ID, CLIENT_SECRET, s, code_verifier, REDIRECT_URI)) req, err := http.NewRequestWithContext(r.Context(), "POST", AUTH_TOKEN_URL, bytes.NewBuffer(body)) if err != nil { panic(err) diff --git a/core/index.go b/core/index.go index 083138a..994f406 100644 --- a/core/index.go +++ b/core/index.go @@ -59,7 +59,6 @@ func GetLanding(r *http.Request, mode string, isLoggedIn bool) (*LandingArtworks var landing LandingArtworks resp, err := API_GET_UnwrapJson(r.Context(), URL, "") - if err != nil { return &landing, err } @@ -80,7 +79,6 @@ func GetLanding(r *http.Request, mode string, isLoggedIn bool) (*LandingArtworks stuff.ForEach(func(key, value gjson.Result) bool { var artwork ArtworkBrief err = json.Unmarshal([]byte(value.String()), &artwork) - if err != nil { return false } @@ -94,7 +92,6 @@ func GetLanding(r *http.Request, mode string, isLoggedIn bool) (*LandingArtworks pagesStr := gjson.Get(resp, "page").String() err = json.Unmarshal([]byte(pagesStr), &pages) - if err != nil { return &landing, err } diff --git a/core/mangaseries.go b/core/mangaseries.go index 046dd6a..c3a3352 100644 --- a/core/mangaseries.go +++ b/core/mangaseries.go @@ -1,11 +1,10 @@ package core import ( + "net/http" "strconv" "time" - "net/http" - "codeberg.org/vnpower/pixivfe/v2/server/session" "github.com/goccy/go-json" ) diff --git a/core/newest.go b/core/newest.go index 07cf618..88ac9f5 100644 --- a/core/newest.go +++ b/core/newest.go @@ -1,9 +1,10 @@ package core import ( + "net/http" + "codeberg.org/vnpower/pixivfe/v2/server/session" "github.com/goccy/go-json" - "net/http" ) func GetNewestArtworks(r *http.Request, worktype string, r18 string) ([]ArtworkBrief, error) { diff --git a/core/novel.go b/core/novel.go index c3c9412..23ace0b 100644 --- a/core/novel.go +++ b/core/novel.go @@ -2,12 +2,11 @@ package core import ( "fmt" + "net/http" "regexp" "strings" "time" - "net/http" - "codeberg.org/vnpower/pixivfe/v2/server/session" "github.com/goccy/go-json" ) @@ -126,11 +125,13 @@ type NovelBrief struct { } // Novel embedded illusts -var re_r = regexp.MustCompile(`\[pixivimage:\d+(-\d+)?\]`) -var re_d = regexp.MustCompile(`\d+(-\d+)?`) -var re_t = regexp.MustCompile(`\"original\":\"(.+?)\"`) -var re_u = regexp.MustCompile(`\[uploadedimage:(\d+)\]`) -var re_id = regexp.MustCompile(`\d+`) +var ( + re_r = regexp.MustCompile(`\[pixivimage:\d+(-\d+)?\]`) + re_d = regexp.MustCompile(`\d+(-\d+)?`) + re_t = regexp.MustCompile(`\"original\":\"(.+?)\"`) + re_u = regexp.MustCompile(`\[uploadedimage:(\d+)\]`) + re_id = regexp.MustCompile(`\d+`) +) func GetNovelByID(r *http.Request, id string) (Novel, error) { var novel Novel diff --git a/core/novelseries.go b/core/novelseries.go index 60113ae..4317cc8 100644 --- a/core/novelseries.go +++ b/core/novelseries.go @@ -1,11 +1,11 @@ package core import ( + "net/http" "time" "codeberg.org/vnpower/pixivfe/v2/server/session" "github.com/goccy/go-json" - "net/http" ) type NovelSeries struct { diff --git a/core/personal.go b/core/personal.go index 0f826ea..c5767a0 100644 --- a/core/personal.go +++ b/core/personal.go @@ -1,10 +1,11 @@ package core import ( + "net/http" + "codeberg.org/vnpower/pixivfe/v2/server/session" "github.com/goccy/go-json" - "net/http" ) func GetNewestFromFollowing(r *http.Request, mode, page string) ([]ArtworkBrief, error) { diff --git a/core/pixivision.go b/core/pixivision.go index a471af2..7eba313 100644 --- a/core/pixivision.go +++ b/core/pixivision.go @@ -2,7 +2,6 @@ package core import ( "fmt" - "github.com/PuerkitoBio/goquery" "html" "io" "net/http" @@ -11,6 +10,8 @@ import ( "strings" "time" + "github.com/PuerkitoBio/goquery" + "codeberg.org/vnpower/pixivfe/v2/i18n" ) @@ -216,8 +217,8 @@ func PixivisionGetTag(r *http.Request, id string, page string, lang ...string) ( doc.Find("._article-card").Each(func(i int, s *goquery.Selection) { var article PixivisionArticle - //article.ID = s.Find(".arc__title a").AttrOr("data-gtm-label", "") - //article.Title = s.Find(".arc__title a").Text() + // article.ID = s.Find(".arc__title a").AttrOr("data-gtm-label", "") + // article.Title = s.Find(".arc__title a").Text() article.ID = s.Find(`a[data-gtm-action="ClickTitle"]`).AttrOr("data-gtm-label", "") article.Title = s.Find(`a[data-gtm-action="ClickTitle"]`).Text() @@ -330,8 +331,8 @@ func PixivisionGetCategory(r *http.Request, id string, page string, lang ...stri doc.Find("._article-card").Each(func(i int, s *goquery.Selection) { var article PixivisionArticle - //article.ID = s.Find(".arc__title a").AttrOr("data-gtm-label", "") - //article.Title = s.Find(".arc__title a").Text() + // article.ID = s.Find(".arc__title a").AttrOr("data-gtm-label", "") + // article.Title = s.Find(".arc__title a").Text() article.ID = s.Find(`a[data-gtm-action="ClickTitle"]`).AttrOr("data-gtm-label", "") article.Title = s.Find(`a[data-gtm-action="ClickTitle"]`).Text() diff --git a/core/ranking.go b/core/ranking.go index f26e6a8..952eaa1 100644 --- a/core/ranking.go +++ b/core/ranking.go @@ -1,6 +1,7 @@ package core import ( + "net/http" "strings" "time" @@ -8,7 +9,6 @@ import ( "codeberg.org/vnpower/pixivfe/v2/server/session" "github.com/goccy/go-json" - "net/http" ) /* diff --git a/core/tag.go b/core/tag.go index e0c0dd7..f12e436 100644 --- a/core/tag.go +++ b/core/tag.go @@ -1,11 +1,11 @@ package core import ( + "net/http" "strings" "codeberg.org/vnpower/pixivfe/v2/server/session" "github.com/goccy/go-json" - "net/http" ) type TagDetail struct { diff --git a/core/user.go b/core/user.go index eb4f3f0..9c3a45c 100644 --- a/core/user.go +++ b/core/user.go @@ -3,9 +3,8 @@ package core import ( "fmt" "math" - "sort" - "net/http" + "sort" "codeberg.org/vnpower/pixivfe/v2/i18n" "codeberg.org/vnpower/pixivfe/v2/server/session" @@ -153,7 +152,6 @@ func GetUserArtworkList(r *http.Request, id, ids string) ([]ArtworkBrief, error) for _, v := range body.Illusts { var illust ArtworkBrief err = json.Unmarshal(v, &illust) - if err != nil { return nil, err } @@ -194,7 +192,6 @@ func GetUserNovels(r *http.Request, id, ids string) ([]NovelBrief, error) { for _, v := range body.Novels { var novel NovelBrief err = json.Unmarshal(v, &novel) - if err != nil { return nil, err } diff --git a/i18n/crawler/main.go b/i18n/crawler/main.go index 59492be..f9822b0 100644 --- a/i18n/crawler/main.go +++ b/i18n/crawler/main.go @@ -35,8 +35,10 @@ func main() { encoder.Encode(result) } -var re_command_fullmatch = regexp.MustCompile(`\A([\s\n# =]*\{\{[^\{\}]*\}\})*[\s\n]*\z`) -var re_comment = regexp.MustCompile(`\{\*[\s\S]*?\*\}`) +var ( + re_command_fullmatch = regexp.MustCompile(`\A([\s\n# =]*\{\{[^\{\}]*\}\})*[\s\n]*\z`) + re_comment = regexp.MustCompile(`\{\*[\s\S]*?\*\}`) +) func stripComments(s string) string { return re_comment.ReplaceAllString(s, "") diff --git a/i18n/lookup.go b/i18n/lookup.go index ade3622..6d2df13 100644 --- a/i18n/lookup.go +++ b/i18n/lookup.go @@ -46,6 +46,7 @@ func loadLocale(fs_i18n fs.FS, locale string) (map[string]string, error) { maps.Copy(m0, m1) return m0, nil } + func loadLocale_helper(fs_i18n fs.FS, locale string, filename string) (map[string]string, error) { file, err := fs_i18n.Open(path.Join(locale, filename)) if err != nil { diff --git a/server/audit/audit_init.go b/server/audit/audit_init.go index 042dba5..38049ef 100644 --- a/server/audit/audit_init.go +++ b/server/audit/audit_init.go @@ -9,9 +9,11 @@ import ( "go.uber.org/zap/zapcore" ) -var optionSaveResponse bool -var MaxRecordedCount = 0 -var logger *zap.Logger +var ( + optionSaveResponse bool + MaxRecordedCount = 0 + logger *zap.Logger +) // Init initializes the audit package and sets up response saving if enabled. // saveResponse is passed as a boolean from main.go. diff --git a/server/audit/spans.go b/server/audit/spans.go index ba63197..d910c0e 100644 --- a/server/audit/spans.go +++ b/server/audit/spans.go @@ -1,9 +1,10 @@ package audit import ( - "codeberg.org/vnpower/pixivfe/v2/i18n" "net/http" "time" + + "codeberg.org/vnpower/pixivfe/v2/i18n" ) type Span interface { @@ -34,21 +35,26 @@ type ServerRequestSpan struct { func (span ServerRequestSpan) GetStartTime() time.Time { return span.StartTime } + func (span ServerRequestSpan) GetEndTime() time.Time { return span.EndTime } + func (span ServerRequestSpan) GetRequestId() string { return span.RequestId } + func (span ServerRequestSpan) Component() string { return "server" } + func (span ServerRequestSpan) Action() map[string]interface{} { return map[string]interface{}{ "method": span.Method, "path": span.Path, } } + func (span ServerRequestSpan) Outcome() map[string]interface{} { outcome := map[string]interface{}{ "status": span.Status, @@ -77,15 +83,19 @@ type APIRequestSpan struct { func (span APIRequestSpan) GetStartTime() time.Time { return span.StartTime } + func (span APIRequestSpan) GetEndTime() time.Time { return span.EndTime } + func (span APIRequestSpan) GetRequestId() string { return span.RequestId } + func (span APIRequestSpan) Component() string { return "API" } + func (span APIRequestSpan) Action() map[string]interface{} { return map[string]interface{}{ "method": span.Method, @@ -93,6 +103,7 @@ func (span APIRequestSpan) Action() map[string]interface{} { "response_file": span.ResponseFilename, } } + func (span APIRequestSpan) Outcome() map[string]interface{} { outcome := map[string]interface{}{ "status": "success", diff --git a/server/middleware/error_handler.go b/server/middleware/error_handler.go index b87af0e..52db17c 100644 --- a/server/middleware/error_handler.go +++ b/server/middleware/error_handler.go @@ -51,7 +51,6 @@ func HandleError(h http.Handler) http.Handler { // Check if an error was caught during the request processing err := request_context.Get(r).CaughtError - if err != nil { // If an error was caught, render the error page routes.ErrorPage(w, r, err, http.StatusInternalServerError) diff --git a/server/routes/about.go b/server/routes/about.go index 4118f52..2b47265 100644 --- a/server/routes/about.go +++ b/server/routes/about.go @@ -1,8 +1,9 @@ package routes import ( - "codeberg.org/vnpower/pixivfe/v2/config" "net/http" + + "codeberg.org/vnpower/pixivfe/v2/config" ) func AboutPage(w http.ResponseWriter, r *http.Request) error { diff --git a/server/routes/mangaseries.go b/server/routes/mangaseries.go index 8de3056..1710c0e 100644 --- a/server/routes/mangaseries.go +++ b/server/routes/mangaseries.go @@ -3,9 +3,8 @@ package routes import ( "fmt" "math" - "strconv" - "net/http" + "strconv" "codeberg.org/vnpower/pixivfe/v2/core" "codeberg.org/vnpower/pixivfe/v2/i18n" diff --git a/server/routes/newest.go b/server/routes/newest.go index 825c956..a3f48c5 100644 --- a/server/routes/newest.go +++ b/server/routes/newest.go @@ -1,8 +1,9 @@ package routes import ( - "codeberg.org/vnpower/pixivfe/v2/core" "net/http" + + "codeberg.org/vnpower/pixivfe/v2/core" ) func NewestPage(w http.ResponseWriter, r *http.Request) error { diff --git a/server/routes/novelseries.go b/server/routes/novelseries.go index 12dc9f7..6ec9a55 100644 --- a/server/routes/novelseries.go +++ b/server/routes/novelseries.go @@ -3,9 +3,8 @@ package routes import ( "fmt" "math" - "strconv" - "net/http" + "strconv" "codeberg.org/vnpower/pixivfe/v2/core" "codeberg.org/vnpower/pixivfe/v2/i18n" diff --git a/server/routes/pixivision.go b/server/routes/pixivision.go index 2ee7554..550bb2a 100644 --- a/server/routes/pixivision.go +++ b/server/routes/pixivision.go @@ -1,10 +1,10 @@ package routes import ( + "net/http" + "codeberg.org/vnpower/pixivfe/v2/core" "codeberg.org/vnpower/pixivfe/v2/server/session" - - "net/http" ) func PixivisionHomePage(w http.ResponseWriter, r *http.Request) error { diff --git a/server/routes/proxy.go b/server/routes/proxy.go index dfef2ad..9275d30 100644 --- a/server/routes/proxy.go +++ b/server/routes/proxy.go @@ -15,7 +15,6 @@ func SPximgProxy(w http.ResponseWriter, r *http.Request) error { } core.ProxyRequest(w, req) return nil - } func IPximgProxy(w http.ResponseWriter, r *http.Request) error { @@ -27,7 +26,6 @@ func IPximgProxy(w http.ResponseWriter, r *http.Request) error { req.Header.Add("Referer", "https://www.pixiv.net/") core.ProxyRequest(w, req) return nil - } func UgoiraProxy(w http.ResponseWriter, r *http.Request) error { diff --git a/server/routes/ranking.go b/server/routes/ranking.go index 12a50cf..78e52b4 100644 --- a/server/routes/ranking.go +++ b/server/routes/ranking.go @@ -1,10 +1,10 @@ package routes import ( + "net/http" "strconv" "codeberg.org/vnpower/pixivfe/v2/core" - "net/http" ) func RankingPage(w http.ResponseWriter, r *http.Request) error { diff --git a/server/routes/rankingCalendar.go b/server/routes/rankingCalendar.go index ef8539d..4457818 100644 --- a/server/routes/rankingCalendar.go +++ b/server/routes/rankingCalendar.go @@ -2,11 +2,10 @@ package routes import ( "fmt" + "net/http" "strconv" "time" - "net/http" - "codeberg.org/vnpower/pixivfe/v2/core" "codeberg.org/vnpower/pixivfe/v2/server/utils" ) diff --git a/server/routes/types.go b/server/routes/types.go index 38a61ab..34ff779 100644 --- a/server/routes/types.go +++ b/server/routes/types.go @@ -2,11 +2,11 @@ package routes import ( "net/http" + "time" "codeberg.org/vnpower/pixivfe/v2/core" "codeberg.org/vnpower/pixivfe/v2/server/request_context" "codeberg.org/vnpower/pixivfe/v2/server/template" - "time" ) func RenderHTML[T any](w http.ResponseWriter, r *http.Request, data T) error { @@ -48,7 +48,6 @@ type Data_artworkMulti struct { Artworks []core.Illust Title string } -type Data_diagnostics struct{} type Data_discovery struct { Artworks []core.ArtworkBrief Title string @@ -153,7 +152,6 @@ type Data_tag struct { ActiveRatio string ActiveSearchMode string } -type Data_unauthorized struct{} type Data_user struct { Title string User core.User @@ -179,3 +177,7 @@ type Data_mangaSeries struct { Page int PageLimit int } +type ( + Data_diagnostics struct{} + Data_unauthorized struct{} +) diff --git a/server/routes/user.go b/server/routes/user.go index fbcb6e8..3703ddb 100644 --- a/server/routes/user.go +++ b/server/routes/user.go @@ -2,11 +2,11 @@ package routes import ( "math" + "net/http" "strconv" "time" "codeberg.org/vnpower/pixivfe/v2/core" - "net/http" ) type userPageData struct { diff --git a/server/session/cookie.go b/server/session/cookie.go index e8f8643..6b4f14d 100644 --- a/server/session/cookie.go +++ b/server/session/cookie.go @@ -3,9 +3,8 @@ package session import ( - "time" - "net/http" + "time" ) type CookieName string diff --git a/server/template/localized_loader.go b/server/template/localized_loader.go index 4815c08..70105ee 100644 --- a/server/template/localized_loader.go +++ b/server/template/localized_loader.go @@ -34,7 +34,7 @@ func (l *LocalizedFSLoader) Open(templatePath string) (io.ReadCloser, error) { i18n_path := path.Join(l.Dir, templatePath) templatePath = filepath.Join(l.Dir, filepath.FromSlash(templatePath)) - //println("load replacer:", i18n_path) + // println("load replacer:", i18n_path) replacer := i18n.Replacer(locale, i18n_path) if replacer == nil { diff --git a/server/template/render_test.go b/server/template/render_test.go index 2aa20ef..49d9614 100644 --- a/server/template/render_test.go +++ b/server/template/render_test.go @@ -83,7 +83,6 @@ func testWith[T any](t *testing.T, data T) { } err := template.Render(io.Discard, variables, data) - if err != nil { template_name, _ := strings.CutPrefix(reflect.TypeFor[T]().Name(), "Data_") t.Errorf("while rendering template %s: %v", template_name, err) diff --git a/server/template/templateFunctions.go b/server/template/templateFunctions.go index 3b0288a..0f86eef 100644 --- a/server/template/templateFunctions.go +++ b/server/template/templateFunctions.go @@ -317,11 +317,13 @@ func SwitchButtonAttributes(baseURL, selection, currentSelection string) string return fmt.Sprintf(`href=%s%s class=switch-button selected=%s`, baseURL, selection, cur) } -var furiganaPattern = regexp.MustCompile(`\[\[rb:\s*(.+?)\s*>\s*(.+?)\s*\]\]`) -var chapterPattern = regexp.MustCompile(`\[chapter:\s*(.+?)\s*\]`) -var jumpUriPattern = regexp.MustCompile(`\[\[jumpuri:\s*(.+?)\s*>\s*(.+?)\s*\]\]`) -var jumpPagePattern = regexp.MustCompile(`\[jump:\s*(\d+?)\s*\]`) -var newPagePattern = regexp.MustCompile(`\s*\[newpage\]\s*`) +var ( + furiganaPattern = regexp.MustCompile(`\[\[rb:\s*(.+?)\s*>\s*(.+?)\s*\]\]`) + chapterPattern = regexp.MustCompile(`\[chapter:\s*(.+?)\s*\]`) + jumpUriPattern = regexp.MustCompile(`\[\[jumpuri:\s*(.+?)\s*>\s*(.+?)\s*\]\]`) + jumpPagePattern = regexp.MustCompile(`\[jump:\s*(\d+?)\s*\]`) + newPagePattern = regexp.MustCompile(`\s*\[newpage\]\s*`) +) // GetTemplateFunctions returns a map of custom template functions for use in HTML templates func GetTemplateFunctions() map[string]any { @@ -347,9 +349,7 @@ func GetTemplateFunctions() map[string]any { "isEmphasize": func(s string) bool { switch s { - case - "R-18", - "R-18G": + case "R-18", "R-18G": return true } return false