diff --git a/cmd/serve.go b/cmd/serve.go index 031ac3f..1a1a74a 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -30,6 +30,10 @@ var serveCmd = &cobra.Command{ Use: "serve", Short: "Launches the api servers", Run: func(cmd *cobra.Command, args []string) { + // PORT is the port the HTTP gateway dials to reach the in-process gRPC server. + // It is NOT a listen port: both the gRPC server (:10000) and HTTP gateway (:11000) listen + // ports are hardcoded in servers/server.go. PORT must match the hardcoded gRPC port (10000) + // for the gateway-to-gRPC dial to succeed. server := servers.New(fmt.Sprintf("0.0.0.0:%s", viper.GetString("port"))) server.Start() }, diff --git a/middleware/cache.go b/middleware/cache.go index 97dd5b1..b3717ed 100644 --- a/middleware/cache.go +++ b/middleware/cache.go @@ -19,7 +19,7 @@ var ( func CacheMiddleware(cache *cache.RedisCache, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if strings.HasPrefix(r.URL.Path, "/api/v1/tickets") { + if r.URL.Path == "/api/v1/tickets" || strings.HasPrefix(r.URL.Path, "/api/v1/tickets/") { next.ServeHTTP(w, r) return } diff --git a/servers/server.go b/servers/server.go index 54711cf..b1ccd9b 100644 --- a/servers/server.go +++ b/servers/server.go @@ -52,6 +52,9 @@ type MicroServer struct { } // New initializes a new Backend struct. +// addr is the gRPC dial target consumed by the HTTP gateway (from $PORT). +// Listen ports for both gRPC (:10000) and HTTP (:11000) are hardcoded in Start(); +// addr must match the gRPC literal for the gateway-to-gRPC dial to succeed. func New(addr string) *MicroServer { return &MicroServer{ @@ -80,8 +83,7 @@ func setupRedis() *cache.RedisCache { redisPassword := viper.GetString("REDIS_PASSWORD") if redisPassword == "" { - Error.Println("no redis password provided") - os.Exit(1) + Info.Println("REDIS_PASSWORD empty — connecting without AUTH") } return cache.NewRedisCache(redisHost, redisPort, redisPassword) @@ -136,7 +138,7 @@ func (server *MicroServer) Start() { httpL, err := net.Listen("tcp", "0.0.0.0:11000") if err != nil { - Error.Fatalf("Failed to listen on %s: %w", server.addr, err) + Error.Fatalf("Failed to listen on %s: %v", server.addr, err) } ds := setupDatasource() @@ -180,7 +182,7 @@ func servGRPC(server *MicroServer, lis net.Listener, grpcOpts []grpc.ServerOptio milpacs.RegisterTicketsServiceServer(server.grpcServer, ticketsService) if err := server.grpcServer.Serve(lis); err != nil { - Error.Fatalf("unable to start external gRPC servers: ", err) + Error.Fatalf("unable to start external gRPC servers: %v", err) } } @@ -188,7 +190,7 @@ func servHTTP(server *MicroServer, lis net.Listener, ds datastores.Datastore) { service := httpServices.Service{Address: server.addr, Cache: server.cache, Datastore: ds} server.httpServer = service.Server() if err := server.httpServer.Serve(lis); err != nil { - Error.Fatalf("unable to start HTTP servers: ", err) + Error.Fatalf("unable to start HTTP servers: %v", err) } }