Private
Public Access
0
0

centralize logging, fail for websockets

This commit is contained in:
teizz
2021-09-26 15:49:29 +02:00
parent f7713084a4
commit cd0673777f
3 changed files with 22 additions and 11 deletions

View File

@@ -2,7 +2,6 @@ package main
import ( import (
"io" "io"
"log"
"net/http" "net/http"
"sync/atomic" "sync/atomic"
) )
@@ -39,5 +38,5 @@ func handleGet(pathID string, w http.ResponseWriter, r *http.Request) {
debug("%s [GET] Removes path", pathID) debug("%s [GET] Removes path", pathID)
} }
} }
log.Printf("%s [GET] Finishes", pathID) info("%s [GET] Finishes", pathID)
} }

View File

@@ -4,6 +4,7 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"strings"
"sync" "sync"
) )
@@ -54,17 +55,25 @@ func pathHandler(w http.ResponseWriter, r *http.Request) {
return 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" { if r.Method == "GET" {
log.Printf("%s [GET] Connected", pathID) info("%s [GET] Connected", pathID)
handleGet(pathID, w, r) handleGet(pathID, w, r)
} else { } else if r.Method == "POST" {
log.Printf("%s [POST] Connected", pathID) info("%s [POST] Connected", pathID)
if h := r.Header.Get("X-Pathway"); h == "pubsub" { if h := r.Header.Get("X-Pathway"); h == "pubsub" {
debug("%s [PUBSUB] Upgrade POST to PUBSUB", pathID) debug("%s [PUBSUB] Upgrade POST to PUBSUB", pathID)
handlePubSub(pathID, r) handlePubSub(pathID, r)
} else { } else {
handlePost(pathID, r) 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() { 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("/health", okHandler)
http.HandleFunc("/favicon.ico", emptyHandler) http.HandleFunc("/favicon.ico", emptyHandler)
@@ -87,12 +96,16 @@ func main() {
err := http.ListenAndServe(":8080", nil) err := http.ListenAndServe(":8080", nil)
if err != 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{}) { func debug(msg string, args ...interface{}) {
if len(debugging) > 0 { if len(debugging) > 0 {
log.Printf(msg, args...) log.Printf("DEBUG | "+msg, args...)
} }
} }

View File

@@ -2,7 +2,6 @@ package main
import ( import (
"io" "io"
"log"
"net/http" "net/http"
"sync/atomic" "sync/atomic"
) )
@@ -56,7 +55,7 @@ func handlePubSub(pathID string, r *http.Request) {
debug("%s [PUBSUB] Removes path", pathID) debug("%s [PUBSUB] Removes path", pathID)
} }
} }
log.Printf("%s [PUBSUB] Finishes", pathID) info("%s [PUBSUB] Finishes", pathID)
} }
func handlePost(pathID string, r *http.Request) { func handlePost(pathID string, r *http.Request) {
@@ -99,5 +98,5 @@ func handlePost(pathID string, r *http.Request) {
debug("%s [POST] Removes path", pathID) debug("%s [POST] Removes path", pathID)
} }
} }
log.Printf("%s [POST] Finishes", pathID) info("%s [POST] Finishes", pathID)
} }