Skip to content

Commit

Permalink
feat: remove support for grpc
Browse files Browse the repository at this point in the history
Signed-off-by: Smuu <18609909+Smuu@users.noreply.github.com>
  • Loading branch information
smuu committed Oct 26, 2023
1 parent 81def73 commit a5f48dc
Showing 1 changed file with 1 addition and 104 deletions.
105 changes: 1 addition & 104 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ package main

import (
"bytes"
"context"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"io"
"log"
"net/http"
"os"
"strings"

"google.golang.org/grpc"
)

var (
Expand Down Expand Up @@ -103,104 +96,8 @@ func handleHttpRequest(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(statusCode)
io.Copy(w, buffer)
}

func proxyGrpcRequest(ctx context.Context, fullSubdomain, method string, r *http.Request) (codes.Code, map[string]string, []byte, error) {
target := "https://" + fullSubdomain + ".lunaroasis.net" + method
conn, err := grpc.Dial(target, grpc.WithInsecure())
if err != nil {
return 0, nil, nil, err
}
defer conn.Close()

// Convert http.Header to map[string]string
headerMap := make(map[string]string)
for key, values := range r.Header {
headerMap[key] = strings.Join(values, ",")
}

// Create metadata from the header map
md := metadata.New(headerMap)

// Create a new outgoing context with the metadata
ctx = metadata.NewOutgoingContext(ctx, md)

stream, err := conn.NewStream(ctx, &grpc.StreamDesc{ServerStreams: true}, method)
if err != nil {
return 0, nil, nil, err
}

// Assume r.Body is a io.Reader containing the serialized request message
if err := stream.SendMsg(r.Body); err != nil {
return 0, nil, nil, err
}

// Create a buffer to hold the serialized response message
buffer := new(bytes.Buffer)
if err := stream.RecvMsg(buffer); err != nil {
return 0, nil, nil, err
}

// Collect headers from the gRPC response
headers, _ := metadata.FromIncomingContext(ctx)
responseHeaderMap := make(map[string]string)
for key, values := range headers {
responseHeaderMap[key] = strings.Join(values, ",")
}

if err := stream.RecvMsg(buffer); err != nil {
st, ok := status.FromError(err)
if ok {
return st.Code(), nil, buffer.Bytes(), err
}
return codes.Unknown, nil, nil, err
}
return codes.Unknown, nil, nil, fmt.Errorf("unexpected error")
}

func handleGrpcRequest(w http.ResponseWriter, r *http.Request) {
infoLog.Printf("Received gRPC request from %s", r.Host)
hostParts := strings.Split(r.Host, ".")
if len(hostParts) < 3 {
errorLog.Printf("Invalid domain: %s", r.Host)
http.Error(w, "Invalid domain", http.StatusBadRequest)
return
}

// This line signifies the start of the HTTP response with a status code of 200 OK.
// The actual gRPC status will be conveyed in the trailers.
w.WriteHeader(http.StatusOK)

subdomain := hostParts[0] // Extract original domain
//originalDomain := strings.Join(hostParts[1:], ".")

statusCode, headers, responseBody, err := proxyGrpcRequest(r.Context(), subdomain+".statescale", r.RequestURI, r)
debugLog.Printf("Received status code %d", statusCode)
if err != nil || statusCode != codes.OK {
debugLog.Printf("Proxying request to %s", subdomain+".snapscale")
backupStatusCode, backupHeaders, backupResponseBody, _ := proxyGrpcRequest(r.Context(), subdomain+".snapscale", r.RequestURI, r)
debugLog.Printf("Received status code %d", backupStatusCode)

for key, value := range backupHeaders {
w.Header().Set(key, value)
}
w.Write(backupResponseBody) // Write the response body directly
return
}

for key, value := range headers {
w.Header().Set(key, value)
}

// Assume response body is in buffer
w.Write(responseBody) // Write the response body directly
}

func handleRequest(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Content-Type") == "application/grpc" {
handleGrpcRequest(w, r)
} else {
handleHttpRequest(w, r)
}
handleHttpRequest(w, r)
}

func main() {
Expand Down

0 comments on commit a5f48dc

Please sign in to comment.