linting + timeout tweak
This commit is contained in:
parent
03c1b9d105
commit
148ac70f19
2 changed files with 21 additions and 4 deletions
|
@ -5,6 +5,7 @@ a proxy server for github copilot, handling token management and request forward
|
||||||
## requirements
|
## requirements
|
||||||
- go 1.20 or later
|
- go 1.20 or later
|
||||||
- a valid github pat set in the `GITHUB_AUTH_TOKEN` environment variable
|
- a valid github pat set in the `GITHUB_AUTH_TOKEN` environment variable
|
||||||
|
- (optional) `golangci-lint`
|
||||||
|
|
||||||
## usage
|
## usage
|
||||||
```bash
|
```bash
|
||||||
|
|
24
main.go
24
main.go
|
@ -35,7 +35,7 @@ type ProxyServer struct {
|
||||||
func NewProxyServer(authToken string) *ProxyServer {
|
func NewProxyServer(authToken string) *ProxyServer {
|
||||||
return &ProxyServer{
|
return &ProxyServer{
|
||||||
authToken: authToken,
|
authToken: authToken,
|
||||||
client: &http.Client{Timeout: 30 * time.Second},
|
client: &http.Client{Timeout: 5 * time.Minute},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +68,12 @@ func (p *ProxyServer) refreshTokenIfNeeded() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("token exchange request failed: %w", err)
|
return fmt.Errorf("token exchange request failed: %w", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer func() {
|
||||||
|
err := resp.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
body, _ := io.ReadAll(resp.Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
@ -105,7 +110,13 @@ func (p *ProxyServer) proxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, "error reading request body", http.StatusBadRequest)
|
http.Error(w, "error reading request body", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Body.Close()
|
defer func() {
|
||||||
|
err := r.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
log.Printf("request: %s", body)
|
||||||
|
|
||||||
targetURL := p.proxyEndpoint + r.URL.Path
|
targetURL := p.proxyEndpoint + r.URL.Path
|
||||||
if p.proxyEndpoint == "" {
|
if p.proxyEndpoint == "" {
|
||||||
|
@ -134,7 +145,12 @@ func (p *ProxyServer) proxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, "error forwarding request to copilot", http.StatusBadGateway)
|
http.Error(w, "error forwarding request to copilot", http.StatusBadGateway)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer func() {
|
||||||
|
err := resp.Body.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
for name, values := range resp.Header {
|
for name, values := range resp.Header {
|
||||||
for _, value := range values {
|
for _, value := range values {
|
||||||
|
|
Loading…
Add table
Reference in a new issue