Skip to content

Commit

Permalink
add di sequence to run server
Browse files Browse the repository at this point in the history
  • Loading branch information
raphoester committed Aug 28, 2024
1 parent f21883c commit cf79c07
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
29 changes: 25 additions & 4 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
package main

import (
"fmt"
"time"
"net"

bookingsv1 "github.com/raphoester/space-trouble-api/generated/proto/bookings/v1"
"github.com/raphoester/space-trouble-api/internal/domain/commands/book_ticket"
"github.com/raphoester/space-trouble-api/internal/infrastructure/primary/controller"
"github.com/raphoester/space-trouble-api/internal/infrastructure/secondary/inmemory_bookings_storage"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)

func main() {
fmt.Println("running")
time.Sleep(999 * time.Hour)
bookingsRepo := inmemory_bookings_storage.New()
ticketBooker := book_ticket.NewTicketBooker(bookingsRepo)
ctr := controller.New(ticketBooker)

server := grpc.NewServer()
bookingsv1.RegisterBookingsServiceServer(server, ctr)
reflection.Register(server)

listener, err := net.Listen("tcp", ":8080")
if err != nil {
panic(err)
}

err = server.Serve(listener)
if err != nil {
panic(err)
}
}
41 changes: 41 additions & 0 deletions internal/infrastructure/primary/controller/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package controller

import (
"context"
"fmt"

bookingsv1 "github.com/raphoester/space-trouble-api/generated/proto/bookings/v1"
"github.com/raphoester/space-trouble-api/internal/domain/commands/book_ticket"
)

func New(ticketBooker *book_ticket.TicketBooker) *Controller {
return &Controller{
ticketBooker: ticketBooker,
}
}

type Controller struct {
bookingsv1.UnimplementedBookingsServiceServer
ticketBooker *book_ticket.TicketBooker
}

func (c *Controller) BookTicket(ctx context.Context,
req *bookingsv1.BookTicketRequest) (*bookingsv1.BookTicketResponse, error) {

err := c.ticketBooker.Execute(ctx, book_ticket.BookTicketParams{
ID: req.GetId(),
FirstName: req.GetFirstName(),
LastName: req.GetLastName(),
Gender: req.GetGender(),
Birthday: req.GetBirthday(),
LaunchpadID: req.GetLaunchpadId(),
DestinationID: req.GetDestinationId(),
LaunchDate: req.GetLaunchDate(),
})

if err != nil {
return nil, fmt.Errorf("failed to book ticket: %w", err)
}

return &bookingsv1.BookTicketResponse{}, nil
}

0 comments on commit cf79c07

Please sign in to comment.