Return err from ProcessRequest instead of panic

chubin/logging
Igor Chubin 2 years ago
parent 5b240c590e
commit 2c367d0157

@ -11,11 +11,14 @@ import (
"time"
)
func processRequest(r *http.Request) responseWithHeader {
var response responseWithHeader
func processRequest(r *http.Request) (*responseWithHeader, error) {
var (
response *responseWithHeader
err error
)
if response, ok := redirectInsecure(r); ok {
return *response
if resp, ok := redirectInsecure(r); ok {
return resp, nil
}
if dontCache(r) {
@ -40,31 +43,36 @@ func processRequest(r *http.Request) responseWithHeader {
}
time.Sleep(30 * time.Millisecond)
cacheBody, ok = lruCache.Get(cacheDigest)
cacheEntry = cacheBody.(responseWithHeader)
if ok && cacheBody != nil {
cacheEntry = cacheBody.(responseWithHeader)
}
}
if cacheEntry.InProgress {
log.Printf("TIMEOUT: %s\n", cacheDigest)
}
if ok && !cacheEntry.InProgress && cacheEntry.Expires.After(time.Now()) {
response = cacheEntry
response = &cacheEntry
foundInCache = true
}
}
if !foundInCache {
lruCache.Add(cacheDigest, responseWithHeader{InProgress: true})
response = get(r)
response, err = get(r)
if err != nil {
return nil, err
}
if response.StatusCode == 200 || response.StatusCode == 304 || response.StatusCode == 404 {
lruCache.Add(cacheDigest, response)
lruCache.Add(cacheDigest, *response)
} else {
log.Printf("REMOVE: %d response for %s from cache\n", response.StatusCode, cacheDigest)
lruCache.Remove(cacheDigest)
}
}
return response
return response, nil
}
func get(req *http.Request) responseWithHeader {
func get(req *http.Request) (*responseWithHeader, error) {
client := &http.Client{}
@ -72,7 +80,7 @@ func get(req *http.Request) responseWithHeader {
proxyReq, err := http.NewRequest(req.Method, queryURL, req.Body)
if err != nil {
log.Printf("Request: %s\n", err)
return nil, err
}
// proxyReq.Header.Set("Host", req.Host)
@ -89,23 +97,22 @@ func get(req *http.Request) responseWithHeader {
}
res, err := client.Do(proxyReq)
if err != nil {
panic(err)
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println(err)
return nil, err
}
return responseWithHeader{
return &responseWithHeader{
InProgress: false,
Expires: time.Now().Add(time.Duration(randInt(1000, 1500)) * time.Second),
Body: body,
Header: res.Header,
StatusCode: res.StatusCode,
}
}, nil
}
// implementation of the cache.get_signature of original wttr.in

@ -143,7 +143,15 @@ func main() {
log.Println(err)
}
// printStat()
response := processRequest(r)
response, err := processRequest(r)
if err != nil {
log.Println(err)
return
}
if response.StatusCode == 0 {
log.Println("status code 0", response)
return
}
copyHeader(w.Header(), response.Header)
w.Header().Set("Access-Control-Allow-Origin", "*")

Loading…
Cancel
Save