Skip to content

Commit 83184db

Browse files
committed
chore: review nitpicks
1 parent d408fe9 commit 83184db

4 files changed

Lines changed: 24 additions & 13 deletions

File tree

‎internal/service/oauth_extractors.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package service
22

33
import (
4+
"context"
45
"errors"
56
"net/http"
67
"strconv"
@@ -20,21 +21,21 @@ type GithubUserinfoResponse struct {
2021
ID int `json:"id"`
2122
}
2223

23-
func defaultExtractor(client *http.Client, url string) (*model.Claims, error) {
24-
return simpleReq[model.Claims](client, url, nil)
24+
func defaultExtractor(client *http.Client, ctx context.Context, url string) (*model.Claims, error) {
25+
return simpleReq[model.Claims](client, ctx, url, nil)
2526
}
2627

27-
func githubExtractor(client *http.Client, _ string) (*model.Claims, error) {
28+
func githubExtractor(client *http.Client, ctx context.Context, _ string) (*model.Claims, error) {
2829
var user model.Claims
2930

30-
userInfo, err := simpleReq[GithubUserinfoResponse](client, "https://api.github.com/user", map[string]string{
31+
userInfo, err := simpleReq[GithubUserinfoResponse](client, ctx, "https://api.github.com/user", map[string]string{
3132
"accept": "application/vnd.github+json",
3233
})
3334
if err != nil {
3435
return nil, err
3536
}
3637

37-
userEmails, err := simpleReq[GithubEmailResponse](client, "https://api.github.com/user/emails", map[string]string{
38+
userEmails, err := simpleReq[GithubEmailResponse](client, ctx, "https://api.github.com/user/emails", map[string]string{
3839
"accept": "application/vnd.github+json",
3940
})
4041
if err != nil {

‎internal/service/oauth_service.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"golang.org/x/oauth2"
1111
)
1212

13-
type OAuthUserinfoExtractor func(client *http.Client, url string) (*model.Claims, error)
13+
type OAuthUserinfoExtractor func(client *http.Client, ctx context.Context, url string) (*model.Claims, error)
1414

1515
type OAuthService struct {
1616
serviceCfg model.OAuthServiceConfig
@@ -80,7 +80,7 @@ func (s *OAuthService) GetToken(code string, verifier string) (*oauth2.Token, er
8080

8181
func (s *OAuthService) GetUserinfo(token *oauth2.Token) (*model.Claims, error) {
8282
client := oauth2.NewClient(s.ctx, oauth2.StaticTokenSource(token))
83-
return s.userinfoExtractor(client, s.serviceCfg.UserinfoURL)
83+
return s.userinfoExtractor(client, s.ctx, s.serviceCfg.UserinfoURL)
8484
}
8585

8686
func (s *OAuthService) GetConfig() model.OAuthServiceConfig {

‎internal/service/simple_req.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package service
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io"
78
"net/http"
89
)
910

10-
func simpleReq[T any](client *http.Client, url string, headers map[string]string) (*T, error) {
11+
func simpleReq[T any](client *http.Client, ctx context.Context, url string, headers map[string]string) (*T, error) {
1112
var decodedRes T
1213

13-
req, err := http.NewRequest("GET", url, nil)
14+
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
1415
if err != nil {
1516
return nil, err
1617
}
@@ -26,7 +27,11 @@ func simpleReq[T any](client *http.Client, url string, headers map[string]string
2627
defer res.Body.Close()
2728

2829
if res.StatusCode < 200 || res.StatusCode >= 300 {
29-
return nil, fmt.Errorf("request failed with status: %s", res.Status)
30+
body, err := io.ReadAll(res.Body)
31+
if err != nil {
32+
return nil, fmt.Errorf("request failed with status: %s", res.Status)
33+
}
34+
return nil, fmt.Errorf("request failed with status: %s and body: %s", res.Status, body)
3035
}
3136

3237
body, err := io.ReadAll(res.Body)

‎internal/service/tailscale_service.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type TailscaleService struct {
5353
config *model.Config
5454
log *logger.Logger
5555
client *http.Client
56+
ctx context.Context
5657

5758
caches struct {
5859
devices *CacheStore[tailscaleAPIDevices]
@@ -71,6 +72,7 @@ type TailscaleServiceInput struct {
7172
Ding *ding.Ding
7273
Config *model.Config
7374
Log *logger.Logger
75+
Ctx context.Context
7476
}
7577

7678
func NewTailscaleService(i TailscaleServiceInput) (*TailscaleService, error) {
@@ -89,6 +91,7 @@ func NewTailscaleService(i TailscaleServiceInput) (*TailscaleService, error) {
8991
s := &TailscaleService{
9092
config: i.Config,
9193
log: i.Log,
94+
ctx: i.Ctx,
9295
}
9396

9497
devicesCache := NewCacheStore[tailscaleAPIDevices](0)
@@ -114,7 +117,9 @@ func NewTailscaleService(i TailscaleServiceInput) (*TailscaleService, error) {
114117

115118
s.urls.devices = tailscaleAPIDeviceList(i.Config.Tailscale.Tailnet)
116119
s.urls.users = tailscaleAPIUserList(i.Config.Tailscale.Tailnet)
117-
s.client = &http.Client{}
120+
s.client = &http.Client{
121+
Timeout: 10 * time.Second,
122+
}
118123

119124
_, _, err := s.getDeviceList()
120125

@@ -144,7 +149,7 @@ func (s *TailscaleService) getDeviceList() (*tailscaleAPIDevices, bool, error) {
144149
return &cached, true, nil
145150
}
146151

147-
devices, err := simpleReq[tailscaleAPIDevices](s.client, s.urls.devices, map[string]string{
152+
devices, err := simpleReq[tailscaleAPIDevices](s.client, s.ctx, s.urls.devices, map[string]string{
148153
"Authorization": s.buildAuthorizationHeader(),
149154
})
150155

@@ -164,7 +169,7 @@ func (s *TailscaleService) getUsersList() (*tailscaleAPIUsers, bool, error) {
164169
return &cached, true, nil
165170
}
166171

167-
users, err := simpleReq[tailscaleAPIUsers](s.client, s.urls.users, map[string]string{
172+
users, err := simpleReq[tailscaleAPIUsers](s.client, s.ctx, s.urls.users, map[string]string{
168173
"Authorization": s.buildAuthorizationHeader(),
169174
})
170175

0 commit comments

Comments
 (0)