refactor: introduce service layer and migrate gRPC handlers to use it
All checks were successful
Build / build (push) Successful in 2m17s
All checks were successful
Build / build (push) Successful in 2m17s
Extract business logic from gRPC handlers into a dedicated service package. Add context support throughout for cancellation and timeouts. Move models to their own package, remove hardcoded credentials from config, and simplify parsers to only handle HTML parsing.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
@@ -14,13 +15,13 @@ import (
|
||||
|
||||
var nameRegexp = regexp.MustCompile(`(\w+)同学`)
|
||||
|
||||
// LoginWithClient 使用指定的 HTTP 客户端和凭据登录
|
||||
func LoginWithClient(httpClient *http.Client, captchaCode, username, password string) (bool, error) {
|
||||
// LoginWithClient 使用指定的 HTTP 客户端和凭据登录。
|
||||
func LoginWithClient(ctx context.Context, httpClient *http.Client, captchaCode, username, password string) (bool, error) {
|
||||
log := slog.With("username", username)
|
||||
log.Debug("执行登录")
|
||||
|
||||
data := fmt.Sprintf("usercode=%s&password=%s&code=%s", username, password, captchaCode)
|
||||
req, err := http.NewRequest("POST", config.Get().BaseURL+"/login", strings.NewReader(data))
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", config.Get().BaseURL+"/login", strings.NewReader(data))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -54,4 +55,4 @@ func LoginWithClient(httpClient *http.Client, captchaCode, username, password st
|
||||
log.Warn("登录失败")
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user