added a bunch of logging

This commit is contained in:
Milo Schwartz 2024-11-24 13:28:20 -05:00
parent f5992bfdff
commit 7a018918f8
No known key found for this signature in database

16
main.go
View file

@ -75,11 +75,15 @@ func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
req.URL.RawQuery = query.Encode()
}
fmt.Println("checked for session param")
cookies := p.extractCookies(req)
if sess != "" {
cookies.Session = &sess
}
fmt.Println("extracted cookies")
verifyURL := fmt.Sprintf("%s/badger/verify-session", p.apiBaseUrl)
originalRequestURL := url.QueryEscape(fmt.Sprintf("%s://%s%s", p.getScheme(req), req.Host, req.URL.RequestURI()))
@ -97,12 +101,16 @@ func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
TLS: req.TLS != nil,
}
fmt.Println("built verify body")
jsonData, err := json.Marshal(cookieData)
if err != nil {
http.Error(rw, "Internal Server Error", http.StatusInternalServerError) // TODO: redirect to error page
return
}
fmt.Println("JSON data:", string(jsonData))
resp, err := http.Post(verifyURL, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
http.Error(rw, "Internal Server Error", http.StatusInternalServerError)
@ -110,11 +118,15 @@ func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}
defer resp.Body.Close()
fmt.Println("response status code:", resp.StatusCode)
if resp.StatusCode != http.StatusOK {
http.Error(rw, "Internal Server Error", http.StatusInternalServerError)
return
}
fmt.Println("de marshalling response")
var result VerifyResponse
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
@ -122,6 +134,8 @@ func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return
}
fmt.Println("handling response")
if result.RedirectURL != nil && *result.RedirectURL != "" {
http.Redirect(rw, req, *result.RedirectURL, http.StatusFound)
return
@ -132,6 +146,8 @@ func (p *Badger) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
return
}
fmt.Println("serving authorized")
p.next.ServeHTTP(rw, req)
}