Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bruwozniak committed Jun 24, 2021
1 parent 6b2dcd7 commit 213aedf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/

.air.toml
tmp
29 changes: 16 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ type JHOAuthHandler struct {
func validateCookie(r *http.Request) bool {
cookie, err := r.Cookie(cookieName)
if err != nil {
log.Println(err)
return false
}
var value string

var value string
if err = cookieSource.Decode(cookieName, cookie.Value, &value); err != nil {
fmt.Println(err)
log.Println(err)
return false
}

Expand All @@ -49,20 +50,20 @@ func validateCookie(r *http.Request) bool {
if res, err := http.DefaultClient.Do(req); err == nil {
payload, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
log.Println(err)
return false
}
var resp map[string]interface{}
err = json.Unmarshal(payload, &resp)
if err != nil {
fmt.Println(err)
log.Println(err)
return false
}
if resp["name"] == jhUser {
return true
}
} else {
fmt.Println(err)
log.Println(err)
}
return false
}
Expand All @@ -79,21 +80,21 @@ func handleCallback(w http.ResponseWriter, r *http.Request) {
"redirect_uri": {callbackUrl},
})
if err != nil {
fmt.Println(err)
log.Println(err)
w.WriteHeader(500)
return
}
if res.StatusCode == 200 {
var resp map[string]interface{}
payload, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
log.Println(err)
w.WriteHeader(500)
return
}
err = json.Unmarshal(payload, &resp)
if err != nil {
fmt.Println(err)
log.Println(err)
w.WriteHeader(500)
return
}
Expand All @@ -107,17 +108,16 @@ func handleCallback(w http.ResponseWriter, r *http.Request) {
}
http.SetCookie(w, cookie)
http.Redirect(w, r, servicePrefix, http.StatusFound)
return
} else {
log.Println(err)
w.WriteHeader(500)
return
}
}
}
}

func (ah JHOAuthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path)
log.Println(r.URL.Path)
if validateCookie(r) {
ah.wrappedHandler.ServeHTTP(w, r)
} else if r.URL.Path == callbackUrl {
Expand Down Expand Up @@ -146,11 +146,14 @@ func newPathTrimmingReverseProxy(target *url.URL) *httputil.ReverseProxy {

func main() {
flag.Parse()
backend, _ := url.Parse(*target)
backend, err := url.Parse(*target)
if err != nil {
log.Fatalln(err)
}
handler := JHOAuthHandler{
wrappedHandler: newPathTrimmingReverseProxy(backend),
}
err := http.ListenAndServe(":"+*port, handler)
err = http.ListenAndServe(":"+*port, handler)
if err != nil {
log.Fatalln(err)
}
Expand Down

0 comments on commit 213aedf

Please sign in to comment.