refactor(grpc): restructure task handling and migrate to slog
All checks were successful
Build / build (push) Successful in 5m28s
All checks were successful
Build / build (push) Successful in 5m28s
Refactor the gRPC client and task handling logic to improve modularity, error handling, and observability. - Split `grpc/handler.go` into specialized handler files (login, classroom, exams, grades, schedule) to reduce complexity. - Replace standard `log` with `log/slog` throughout the project for structured logging. - Refactor `grpc/client.go` to use a thread-safe connection management pattern with `sync.RWMutex`. - Remove the legacy `service` package and HTTP server implementation, transitioning the application to a pure gRPC runner mode. - Clean up `config` and `pkg/errors` to remove unused utilities and simplify the API. - Improve error reporting in parsers and HTTP clients by checking response status codes.
This commit is contained in:
@@ -3,41 +3,16 @@ package client
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"schedule_converter/config"
|
||||
)
|
||||
|
||||
var (
|
||||
Client *http.Client
|
||||
RecognizerClient *http.Client
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
func InitClient() {
|
||||
once.Do(func() {
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
slog.Error("创建 cookie jar 失败", "error", err)
|
||||
panic(err)
|
||||
}
|
||||
Client = &http.Client{Jar: jar}
|
||||
|
||||
cfg := config.Get()
|
||||
RecognizerClient = &http.Client{
|
||||
Timeout: cfg.Recognizer.Timeout,
|
||||
}
|
||||
|
||||
slog.Info("HTTP 客户端初始化完成")
|
||||
})
|
||||
}
|
||||
const defaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||
|
||||
func GetHeaders() http.Header {
|
||||
return http.Header{
|
||||
"User-Agent": []string{"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"},
|
||||
"User-Agent": []string{defaultUserAgent},
|
||||
"Accept": []string{"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"},
|
||||
"Accept-Language": []string{"zh-CN,zh;q=0.9,en;q=0.8"},
|
||||
}
|
||||
@@ -49,15 +24,10 @@ func GetHeadersWithContentType(contentType string) http.Header {
|
||||
return headers
|
||||
}
|
||||
|
||||
func GetInitialCookies() error {
|
||||
return GetInitialCookiesWithClient(Client)
|
||||
}
|
||||
|
||||
func GetInitialCookiesWithClient(httpClient *http.Client) error {
|
||||
slog.Info("访问登录页面获取初始Cookie")
|
||||
|
||||
cfg := config.Get()
|
||||
req, err := http.NewRequest("GET", cfg.BaseURL+"/loginNOCAS", nil)
|
||||
req, err := http.NewRequest("GET", config.Get().BaseURL+"/loginNOCAS", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -70,31 +40,13 @@ func GetInitialCookiesWithClient(httpClient *http.Client) error {
|
||||
defer resp.Body.Close()
|
||||
|
||||
slog.Info("登录页面访问完成", "status_code", resp.StatusCode)
|
||||
logCookies(httpClient)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func logCookies(httpClient *http.Client) {
|
||||
cfg := config.Get()
|
||||
cookies := httpClient.Jar.Cookies(parseURL(cfg.BaseURL))
|
||||
|
||||
var cookieStrs []string
|
||||
for _, cookie := range cookies {
|
||||
cookieStrs = append(cookieStrs, cookie.Name+"="+cookie.Value)
|
||||
}
|
||||
slog.Debug("当前Cookies", "cookies", strings.Join(cookieStrs, "; "))
|
||||
}
|
||||
|
||||
func parseURL(rawURL string) *url.URL {
|
||||
u, _ := url.Parse(rawURL)
|
||||
return u
|
||||
}
|
||||
|
||||
func CheckLoginSuccess(body string) bool {
|
||||
return strings.Contains(body, "本科教学管理服务系统")
|
||||
}
|
||||
|
||||
func IsCaptchaError(body string) bool {
|
||||
return strings.Contains(body, "验证码")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user