Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions code/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,27 +194,6 @@ services:
networks:
- threadit-network

keycloak:
image: quay.io/keycloak/keycloak:21.1
container_name: keycloak
restart: always
command:
- start-dev
- --import-realm
environment:
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: ${KEYCLOAK_ADMIN_PASSWORD}
KC_HOSTNAME_STRICT: false
KC_HOSTNAME_STRICT_HTTPS: false
KC_HTTP_ENABLED: "true"
KC_PROXY: edge
volumes:
- ./keycloak/realm-export.json:/opt/keycloak/data/import/realm.json:ro
ports:
- "${KEYCLOAK_PORT}:8080"
networks:
- threadit-network

volumes:
db_data:
driver: local
Expand Down
103 changes: 27 additions & 76 deletions code/grpc-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"threadit/grpc-gateway/middleware"
"google.golang.org/protobuf/types/known/emptypb"
)

Expand Down Expand Up @@ -109,24 +108,8 @@ func handleHealthCheck(w http.ResponseWriter, r *http.Request) {
}

func main() {
ctx := context.Background()
mux := runtime.NewServeMux()

// Initialize auth handler
authHandler := middleware.NewAuthHandler(
os.Getenv("KEYCLOAK_URL"),
os.Getenv("KEYCLOAK_CLIENT_ID"),
os.Getenv("KEYCLOAK_CLIENT_SECRET"),
os.Getenv("KEYCLOAK_REALM"),
)

// Create a new ServeMux for both gRPC-Gateway and auth routes
httpMux := http.NewServeMux()
gwmux := runtime.NewServeMux()

// Register auth routes
authHandler.RegisterRoutes(httpMux)

// gRPC dial options with message size configurations
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(
Expand All @@ -135,80 +118,48 @@ func main() {
),
}

// Register gRPC-Gateway routes with auth middleware
httpMux.Handle("/api", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Auth middleware for API routes
authMiddleware := middleware.NewAuthMiddleware(middleware.KeycloakConfig{
Realm: os.Getenv("KEYCLOAK_REALM"),
ClientID: os.Getenv("KEYCLOAK_CLIENT_ID"),
ClientSecret: os.Getenv("KEYCLOAK_CLIENT_SECRET"),
KeycloakURL: os.Getenv("KEYCLOAK_URL"),
})

authMiddleware.Handler(mux).ServeHTTP(w, r)
}))

// Register service handlers
if err := registerServices(ctx, mux, opts); err != nil {
log.Fatalf("Failed to register services: %v", err)
}

port := os.Getenv("GRPC_GATEWAY_PORT")
if port == "" {
log.Fatalf("missing GRPC_GATEWAY_PORT env var")
err := communitypb.RegisterCommunityServiceHandlerFromEndpoint(context.Background(), gwmux, getGrpcServerAddress("COMMUNITY_SERVICE_HOST", "COMMUNITY_SERVICE_PORT"), opts)
if err != nil {
log.Fatalf("Failed to register gRPC gateway: %v", err)
}

log.Printf("gRPC Gateway server listening on :%s", port)
if err := http.ListenAndServe(":"+port, httpMux); err != nil {
log.Fatalf("Failed to serve: %v", err)
err = threadpb.RegisterThreadServiceHandlerFromEndpoint(context.Background(), gwmux, getGrpcServerAddress("THREAD_SERVICE_HOST", "THREAD_SERVICE_PORT"), opts)
if err != nil {
log.Fatalf("Failed to register gRPC gateway: %v", err)
}
}

func registerServices(ctx context.Context, mux *runtime.ServeMux, opts []grpc.DialOption) error {
// Register Community Service
if err := communitypb.RegisterCommunityServiceHandlerFromEndpoint(
ctx, mux, getGrpcServerAddress("COMMUNITY_SERVICE_HOST", "COMMUNITY_SERVICE_PORT"), opts,
); err != nil {
return fmt.Errorf("failed to register community service: %v", err)
err = commentpb.RegisterCommentServiceHandlerFromEndpoint(context.Background(), gwmux, getGrpcServerAddress("COMMENT_SERVICE_HOST", "COMMENT_SERVICE_PORT"), opts)
if err != nil {
log.Fatalf("Failed to register gRPC gateway: %v", err)
}

// Register Thread Service
if err := threadpb.RegisterThreadServiceHandlerFromEndpoint(
ctx, mux, getGrpcServerAddress("THREAD_SERVICE_HOST", "THREAD_SERVICE_PORT"), opts,
); err != nil {
return fmt.Errorf("failed to register thread service: %v", err)
err = votepb.RegisterVoteServiceHandlerFromEndpoint(context.Background(), gwmux, getGrpcServerAddress("VOTE_SERVICE_HOST", "VOTE_SERVICE_PORT"), opts)
if err != nil {
log.Fatalf("Failed to register gRPC gateway: %v", err)
}

// Register Comment Service
if err := commentpb.RegisterCommentServiceHandlerFromEndpoint(
ctx, mux, getGrpcServerAddress("COMMENT_SERVICE_HOST", "COMMENT_SERVICE_PORT"), opts,
); err != nil {
return fmt.Errorf("failed to register comment service: %v", err)
err = searchpb.RegisterSearchServiceHandlerFromEndpoint(context.Background(), gwmux, getGrpcServerAddress("SEARCH_SERVICE_HOST", "SEARCH_SERVICE_PORT"), opts)
if err != nil {
log.Fatalf("Failed to register gRPC gateway: %v", err)
}

// Register Vote Service
if err := votepb.RegisterVoteServiceHandlerFromEndpoint(
ctx, mux, getGrpcServerAddress("VOTE_SERVICE_HOST", "VOTE_SERVICE_PORT"), opts,
); err != nil {
return fmt.Errorf("failed to register vote service: %v", err)
err = popularpb.RegisterPopularServiceHandlerFromEndpoint(context.Background(), gwmux, getGrpcServerAddress("POPULAR_SERVICE_HOST", "POPULAR_SERVICE_PORT"), opts)
if err != nil {
log.Fatalf("Failed to register gRPC gateway: %v", err)
}

http.HandleFunc("/health", handleHealthCheck)

http.Handle("/", gwmux)

// Register Search Service
if err := searchpb.RegisterSearchServiceHandlerFromEndpoint(
ctx, mux, getGrpcServerAddress("SEARCH_SERVICE_HOST", "SEARCH_SERVICE_PORT"), opts,
); err != nil {
return fmt.Errorf("failed to register search service: %v", err)
port := os.Getenv("GRPC_GATEWAY_PORT")
if port == "" {
log.Fatalf("missing GRPC_GATEWAY_PORT env var")
}

// Register Popular Service
if err := popularpb.RegisterPopularServiceHandlerFromEndpoint(
ctx, mux, getGrpcServerAddress("POPULAR_SERVICE_HOST", "POPULAR_SERVICE_PORT"), opts,
); err != nil {
return fmt.Errorf("failed to register popular service: %v", err)
log.Printf("gRPC Gateway server listening on :%s", port)
err = http.ListenAndServe(fmt.Sprintf(":%s", port), nil)
if err != nil {
log.Fatalf("Failed to start HTTP server: %v", err)
}

return nil
}
163 changes: 0 additions & 163 deletions code/grpc-gateway/middleware/auth.go

This file was deleted.

Loading