Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
elops-od committed Dec 15, 2023
1 parent 63130d7 commit a43434e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_tag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 #######
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
178 changes: 92 additions & 86 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand Down Expand Up @@ -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
}

0 comments on commit a43434e

Please sign in to comment.