Skip to content

Commit

Permalink
Merge pull request #48 from cloudflare/caw/improve-localhost-debug
Browse files Browse the repository at this point in the history
Print out gateway configuration when running locally
  • Loading branch information
chris-wood authored May 22, 2023
2 parents 13b1e72 + 71b7ddf commit 450ca28
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
package main

import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -64,20 +66,23 @@ type gatewayServer struct {
metricsFactory MetricsFactory
}

func (s gatewayServer) indexHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("%s Handling %s\n", r.Method, r.URL.Path)
func (s gatewayServer) formatConfiguration(w io.Writer) {
fmt.Fprint(w, "OHTTP Gateway\n")
fmt.Fprint(w, "----------------\n")
fmt.Fprintf(w, "Config endpoint: https://%s%s\n", r.Host, s.endpoints["Config"])
fmt.Fprintf(w, "Legacy config endpoint: https://%s%s\n", r.Host, s.endpoints["LegacyConfig"])
fmt.Fprintf(w, "Target endpoint: https://%s%s\n", r.Host, s.endpoints["Target"])
fmt.Fprintf(w, "Config endpoint: %s\n", s.endpoints["Config"])
fmt.Fprintf(w, "Legacy config endpoint: %s\n", s.endpoints["LegacyConfig"])
fmt.Fprintf(w, "Target endpoint: %s\n", s.endpoints["Target"])
fmt.Fprintf(w, " Request content type: %s\n", s.requestLabel)
fmt.Fprintf(w, " Response content type: %s\n", s.responseLabel)
fmt.Fprintf(w, "Echo endpoint: https://%s%s\n", r.Host, s.endpoints["Echo"])
fmt.Fprintf(w, "Metadata endpoint: https://%s%s\n", r.Host, s.endpoints["Metadata"])
fmt.Fprintf(w, "Echo endpoint: %s\n", s.endpoints["Echo"])
fmt.Fprintf(w, "Metadata endpoint: %s\n", s.endpoints["Metadata"])
fmt.Fprint(w, "----------------\n")
}

func (s gatewayServer) indexHandler(w http.ResponseWriter, r *http.Request) {
s.formatConfiguration(w)
}

func (s gatewayServer) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("%s Handling %s\n", r.Method, r.URL.Path)
fmt.Fprint(w, "ok")
Expand Down Expand Up @@ -289,12 +294,15 @@ func main() {
http.HandleFunc(configEndpoint, target.configHandler)
http.HandleFunc("/", server.indexHandler)

var b bytes.Buffer
server.formatConfiguration(io.Writer(&b))
log.Println(b.String())

if enableTLSServe {
log.Printf("Listening on port %v with cert %v and key %v\n", port, certFile, keyFile)
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf(":%s", port), certFile, keyFile, nil))
} else {
log.Printf("Listening on port %v without enabling TLS\n", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

}

0 comments on commit 450ca28

Please sign in to comment.