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
4 changes: 4 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
Expand Down
2 changes: 1 addition & 1 deletion middleware/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 7 additions & 5 deletions servers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -180,15 +182,15 @@ 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)
}
}

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)
}
}

Expand Down