From cd0673777f2ce897cf25d0640c09bf2225eda231 Mon Sep 17 00:00:00 2001 From: teizz Date: Sun, 26 Sep 2021 15:49:29 +0200 Subject: [PATCH] centralize logging, fail for websockets --- src/get.go | 3 +-- src/main.go | 25 +++++++++++++++++++------ src/post.go | 5 ++--- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/get.go b/src/get.go index a1a0d0e..48bfc53 100644 --- a/src/get.go +++ b/src/get.go @@ -2,7 +2,6 @@ package main import ( "io" - "log" "net/http" "sync/atomic" ) @@ -39,5 +38,5 @@ func handleGet(pathID string, w http.ResponseWriter, r *http.Request) { debug("%s [GET] Removes path", pathID) } } - log.Printf("%s [GET] Finishes", pathID) + info("%s [GET] Finishes", pathID) } diff --git a/src/main.go b/src/main.go index a3ecd0b..0d1b1e9 100644 --- a/src/main.go +++ b/src/main.go @@ -4,6 +4,7 @@ import ( "io" "log" "net/http" + "strings" "sync" ) @@ -54,17 +55,25 @@ func pathHandler(w http.ResponseWriter, r *http.Request) { return } + if h := r.Header.Get("Connection"); len(h) > 0 && strings.ToLower(h) == "upgrade" { + if h = r.Header.Get("Upgrade"); len(h) > 0 && strings.ToLower(h) == "websocket" { + info("Websocket connections not supported yet. Headers: %+v", r.Header) + } + } + if r.Method == "GET" { - log.Printf("%s [GET] Connected", pathID) + info("%s [GET] Connected", pathID) handleGet(pathID, w, r) - } else { - log.Printf("%s [POST] Connected", pathID) + } else if r.Method == "POST" { + info("%s [POST] Connected", pathID) if h := r.Header.Get("X-Pathway"); h == "pubsub" { debug("%s [PUBSUB] Upgrade POST to PUBSUB", pathID) handlePubSub(pathID, r) } else { handlePost(pathID, r) } + } else { + info("Unhandled request type: '%s'", r.Method) } } @@ -78,7 +87,7 @@ func emptyHandler(w http.ResponseWriter, r *http.Request) { } func main() { - log.Printf("pathway version:%s buildtime:%s", version, buildtime) + info("pathway version:%s buildtime:%s", version, buildtime) http.HandleFunc("/health", okHandler) http.HandleFunc("/favicon.ico", emptyHandler) @@ -87,12 +96,16 @@ func main() { err := http.ListenAndServe(":8080", nil) if err != nil { - log.Println(err) + info("%s", err.Error()) } } +func info(msg string, args ...interface{}) { + log.Printf("INFO | "+msg, args...) +} + func debug(msg string, args ...interface{}) { if len(debugging) > 0 { - log.Printf(msg, args...) + log.Printf("DEBUG | "+msg, args...) } } diff --git a/src/post.go b/src/post.go index 297d112..caed6b2 100644 --- a/src/post.go +++ b/src/post.go @@ -2,7 +2,6 @@ package main import ( "io" - "log" "net/http" "sync/atomic" ) @@ -56,7 +55,7 @@ func handlePubSub(pathID string, r *http.Request) { debug("%s [PUBSUB] Removes path", pathID) } } - log.Printf("%s [PUBSUB] Finishes", pathID) + info("%s [PUBSUB] Finishes", pathID) } func handlePost(pathID string, r *http.Request) { @@ -99,5 +98,5 @@ func handlePost(pathID string, r *http.Request) { debug("%s [POST] Removes path", pathID) } } - log.Printf("%s [POST] Finishes", pathID) + info("%s [POST] Finishes", pathID) }