diff --git a/.github/workflows/build_tag.yaml b/.github/workflows/build_tag.yaml index 36accd6..a0e1f92 100644 --- a/.github/workflows/build_tag.yaml +++ b/.github/workflows/build_tag.yaml @@ -40,4 +40,4 @@ jobs: platforms: linux/amd64 ,linux/arm64/v8 tags: | registry.hub.docker.com/elopsod/echo-server:${{github.ref_name}} - registry.hub.docker.com/elopsod/echo-server:latest + registry.hub.docker.com/${{github.repository}}:latest diff --git a/Dockerfile b/Dockerfile index e17885d..0b0c08f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,8 +5,6 @@ WORKDIR /app COPY . ./ RUN go mod download - -# Build the Go app RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} go build -o ./echo-server ./main.go ######## Start a new stage from scratch ####### diff --git a/go.mod b/go.mod index 83b12e1..26ba7a1 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( ) require ( + github.com/caitlinelfring/go-env-default v1.1.0 // indirect github.com/golang/protobuf v1.5.3 // indirect golang.org/x/net v0.19.0 // indirect golang.org/x/sys v0.15.0 // indirect diff --git a/go.sum b/go.sum index e6bd4c0..aa8760e 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/caitlinelfring/go-env-default v1.1.0 h1:bhDfXmUolvcIGfQCX8qevQX8wxC54NGz0aimoUnhvDM= +github.com/caitlinelfring/go-env-default v1.1.0/go.mod h1:tESXPr8zFPP/cRy3cwxrHBmjJIf2A1x/o4C9CET2rEk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= diff --git a/main.go b/main.go index 0f63cb5..4d76518 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "fmt" + "github.com/caitlinelfring/go-env-default" pb "github.com/elopsod/echo-server/echoServer" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -16,75 +17,62 @@ import ( "os" "strings" "sync" - "syscall" ) -type server struct { - pb.EchoServerServer -} +func main() { -func (s *server) GrpcPing(ctx context.Context, req *pb.Request) (*pb.Response, error) { - name := req.GetName() - md, _ := metadata.FromIncomingContext(ctx) + httpPort := env.GetDefault("HTTP_PORT", "8080") + httpsPort := env.GetDefault("HTTP_PORT", "8443") + grpcPort := env.GetDefault("HTTP_PORT", "50051") + grpcsPort := env.GetDefault("HTTP_PORT", "50053") - response := &pb.Response{ - Message: fmt.Sprintf("Hello, %s!", name), - Headers: fmt.Sprintf("%s", md), - } - return response, nil -} + var wg sync.WaitGroup + wg.Add(1) + + // HTTP server setup + go func() { + defer wg.Done() + + httpHandler := http.NewServeMux() + httpHandler.HandleFunc("/", HttpPing) -func HttpPing() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - path := r.URL.Path - host := r.Host - method := r.Method - protocol := r.Proto - hostname, _ := os.Hostname() - var scheme string = "http" - if r.TLS != nil { - scheme = "https" + httpServer := &http.Server{ + Addr: fmt.Sprintf(":%s", httpPort), + Handler: httpHandler, + } + log.Printf("HTTP Server is listening on port %s", httpPort) + if err := httpServer.ListenAndServe(); err != nil { + log.Fatalf("failed to serve HTTP: %v", err) } - body, _ := io.ReadAll(r.Body) + }() - headers := []string{} - for key, values := range r.Header { - headers = append(headers, fmt.Sprintf("%s: %s", key, strings.Join(values, " "))) + // HTTPS server setup + go func() { + defer wg.Done() + + httpsHandler := http.NewServeMux() + httpsHandler.HandleFunc("/", HttpPing) + + certFile := "certs/server.crt" + keyFile := "certs/server.key" + + certificate, _ := tls.LoadX509KeyPair(certFile, keyFile) + tlsConfig := &tls.Config{ + Certificates: []tls.Certificate{certificate}, } - // Send headers, path, and body in the response - fmt.Fprintln(w, "Host:", host) - fmt.Fprintln(w, "Path:", path) - fmt.Fprintln(w, "Method:", method) - fmt.Fprintln(w, "Protocol:", protocol) - fmt.Fprintln(w, "Scheme:", scheme) - fmt.Fprintln(w, "Hostname:", hostname) - fmt.Fprintln(w, "Headers:\n\t", strings.Join(headers[:], "\n\t ")) - fmt.Fprintln(w, "Body:\n\t", string(body)) - }) - //http.HandleFunc("/", handler) -} -func main() { - httpPort, ok := syscall.Getenv("HTTP_PORT") - if !ok { - httpPort = "8080" - } - httpsPort, ok := syscall.Getenv("HTTPS_PORT") - if !ok { - httpsPort = "8443" - } - grpcPort, ok := syscall.Getenv("GRPC_PORT") - if !ok { - grpcPort = "50051" - } - grpcsPort, ok := syscall.Getenv("GRPCS_PORT") - if !ok { - grpcsPort = "50053" - } - HttpPing() - var wg sync.WaitGroup - wg.Add(1) + httpsServer := &http.Server{ + Addr: fmt.Sprintf(":%s", httpsPort), + Handler: httpsHandler, + TLSConfig: tlsConfig, + } + http.HandleFunc("/", HttpPing) + log.Printf("HTTPS Server is listening on port %s", httpsPort) + if err := httpsServer.ListenAndServeTLS(certFile, keyFile); err != nil { + log.Fatalf("failed to serve HTTPS: %v", err) + } + }() // GRPC server setup go func() { defer wg.Done() @@ -129,37 +117,55 @@ func main() { } }() - // HTTP server setup - go func() { - defer wg.Done() + wg.Wait() +} - log.Printf("HTTP Server is listening on port %s", httpPort) - if err := http.ListenAndServe(fmt.Sprintf(":%s", httpPort), nil); err != nil { - log.Fatalf("failed to serve HTTP: %v", err) - } - }() +func HttpPing(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + host := r.Host + method := r.Method + protocol := r.Proto + hostname, _ := os.Hostname() + var scheme string = "http" + if r.TLS != nil { + scheme = "https" + } + body, _ := io.ReadAll(r.Body) - // HTTPS server setup - go func() { - defer wg.Done() + headers := []string{} + for key, values := range r.Header { + headers = append(headers, fmt.Sprintf("%s: %s", key, strings.Join(values, " "))) + } + // Send headers, path, and body in the response + fmt.Fprintln(w, "Host:", host) + fmt.Fprintln(w, "Path:", path) + fmt.Fprintln(w, "Method:", method) + fmt.Fprintln(w, "Protocol:", protocol) + fmt.Fprintln(w, "Scheme:", scheme) + fmt.Fprintln(w, "Hostname:", hostname) + fmt.Fprintln(w, "Headers:\n\t", strings.Join(headers[:], "\n\t ")) + fmt.Fprintln(w, "Body:\n\t", string(body)) +} - certFile := "certs/server.crt" - keyFile := "certs/server.key" +type server struct { + pb.EchoServerServer +} - certificate, _ := tls.LoadX509KeyPair(certFile, keyFile) +func (s *server) GrpcPing(ctx context.Context, req *pb.Request) (*pb.Response, error) { + name := req.GetName() + md, _ := metadata.FromIncomingContext(ctx) - tlsConfig := &tls.Config{ - Certificates: []tls.Certificate{certificate}, - } + response := &pb.Response{ + Message: fmt.Sprintf("Hello, %s!", name), + Headers: fmt.Sprintf("%s", md), + } + return response, nil +} - httpsServer := &http.Server{ - Addr: fmt.Sprintf(":%s", httpsPort), - TLSConfig: tlsConfig, - } - log.Printf("HTTPS Server is listening on port %s", httpsPort) - if err := httpsServer.ListenAndServeTLS(certFile, keyFile); err != nil { - log.Fatalf("failed to serve HTTPS: %v", err) - } - }() - wg.Wait() +func getEnv(key, defaultValue string) string { + value := os.Getenv(key) + if len(value) == 0 { + return defaultValue + } + return value }