Ver Fonte

centralize logging, fail for websockets

teizz há 2 anos atrás
pai
commit
cd0673777f
3 ficheiros alterados com 22 adições e 11 exclusões
  1. 1 2
      src/get.go
  2. 19 6
      src/main.go
  3. 2 3
      src/post.go

+ 1 - 2
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)
 }

+ 19 - 6
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...)
 	}
 }

+ 2 - 3
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)
 }