Reuse client and ctx

This commit is contained in:
blank X 2021-08-10 14:46:01 +07:00
parent 3718689e02
commit a94dd6fb44
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
1 changed files with 7 additions and 7 deletions

14
main.go
View File

@ -59,11 +59,7 @@ func trustCertificate(hostname string, cert *x509.Certificate) error {
return nil
}
func do(req *gemini.Request, via []*gemini.Request) (*gemini.Response, *gemini.Request, error) {
client := gemini.Client{
TrustCertificate: trustCertificate,
}
ctx := context.Background()
func do(client gemini.Client, ctx context.Context, req *gemini.Request, via []*gemini.Request) (*gemini.Response, *gemini.Request, error) {
resp, err := client.Do(ctx, req)
if err != nil {
return resp, req, err
@ -85,7 +81,7 @@ func do(req *gemini.Request, via []*gemini.Request) (*gemini.Response, *gemini.R
target = req.URL.ResolveReference(target)
redirect := *req
redirect.URL = target
return do(&redirect, via)
return do(client, ctx, &redirect, via)
}
if resp.Status.Class() != gemini.StatusSuccess {
return resp, req, errors.New(fmt.Sprintf("%d %s", resp.Status, resp.Meta))
@ -109,7 +105,11 @@ func main() {
log.Fatal(err)
}
resp, req, err := do(req, nil)
client := gemini.Client{
TrustCertificate: trustCertificate,
}
ctx := context.Background()
resp, req, err := do(client, ctx, req, nil)
if err != nil {
log.Fatal(err)
}