Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement donut as a service #1

Merged
merged 11 commits into from Dec 10, 2023
Merged

Conversation

Aldiwildan77
Copy link
Member

@Aldiwildan77 Aldiwildan77 commented Dec 10, 2023

Summary by CodeRabbit

  • New Features

    • Introduced database configuration settings for PostgreSQL integration.
    • Implemented new launch configurations for production and development environments in VS Code.
    • Added ApplicationConfig structure for managing application settings.
    • Created a new Config type for consolidating application and database configurations.
    • Established a matchmaking system with entities, structs, and interfaces for managing matchmakers and users.
    • Enhanced request handling with updated method signatures and logic in the Handler struct.
    • Updated NewDatabaseInstance function to support dynamic configuration.
    • Refined the main function to include server setup and graceful shutdown process.
    • Added functions for parsing matchmaking-related requests and responses.
    • Expanded DonutRepository interface with methods for matchmaker and user management.
    • Introduced a GenerateSerial function for creating unique identifiers.
  • Documentation

    • Updated .env.example and .gitignore to reflect new configuration and ignore rules.
  • Refactor

    • Restructured main application logic for improved configuration and database initialization.
  • Chores

    • Added rules to .gitignore for excluding postgres-data/ directory and .env files.

@Aldiwildan77 Aldiwildan77 added the enhancement New feature or request label Dec 10, 2023
@Aldiwildan77 Aldiwildan77 self-assigned this Dec 10, 2023
@Aldiwildan77
Copy link
Member Author

Aldiwildan77 commented Dec 10, 2023

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Dec 10, 2023

This is an invalid command. Please refer to the list of valid keywords below.

CodeRabbit Commands
  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Copy link
Contributor

coderabbitai bot commented Dec 10, 2023

Walkthrough

The project has undergone a significant update to integrate PostgreSQL database support, including configuration and connection handling. New structures and interfaces have been added to manage application configurations, database settings, and a matchmaking system. The .gitignore file has been updated to exclude environment and data directories, and the VS Code launch configurations have been adjusted for different build environments. The main function has been refactored to accommodate these changes.

Changes

File(s) Summary
.env.example, config.go, database.go,
application.go, instance.go
Added configurations for PostgreSQL database and application settings.
.gitignore Updated to ignore postgres-data/ and .env files.
.vscode/launch.json Added VS Code launch configurations for production and development.
donut.go, dto.go, entity.go,
handler.go, presentation.go, repository.go
Implemented a matchmaking system with new types, methods, and request/response handling.
main.go Refactored to set up server and database connections using new configurations.
serial.go Added a function to generate unique serial strings.

"In the code where logic threads, 🧵
A rabbit danced, connecting webs. 🕸️
With each commit, the story weaves,
A tale of data, in bytes it breathes." 🐇💾

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on X ?


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 11

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8f58810 and 31bc9d3.
Files ignored due to filter (3)
  • docker-compose.yml
  • go.mod
  • go.sum
Files selected for processing (15)
  • .env.example (1 hunks)
  • .gitignore (1 hunks)
  • .vscode/launch.json (1 hunks)
  • application.go (1 hunks)
  • config.go (1 hunks)
  • database.go (1 hunks)
  • donut.go (1 hunks)
  • dto.go (1 hunks)
  • entity.go (1 hunks)
  • handler.go (1 hunks)
  • instance.go (1 hunks)
  • main.go (1 hunks)
  • presentation.go (1 hunks)
  • repository.go (1 hunks)
  • serial.go (1 hunks)
Files skipped from review due to trivial changes (2)
  • .env.example
  • .gitignore
Additional comments: 40
.vscode/launch.json (1)
  • 1-36: The new launch configurations for 'Production' and 'Development' in .vscode/launch.json are correctly set up with appropriate environment-specific settings. This will facilitate debugging and running the application in different environments directly from VS Code.
application.go (1)
  • 5-13: The ApplicationConfig struct and its Address method are correctly implemented and follow best practices for defining configuration structures in Go, including the use of struct tags for environment variable loading.
config.go (1)
  • 7-16: The implementation of the Config struct and the Get function for parsing environment variables into configuration looks correct. Ensure that the environment variables are properly documented and match the field names of the Config struct for successful parsing.
database.go (2)
  • 31-47: Ensure that the database connection strings (DSN) are properly escaped to prevent any potential security issues. If any of the configuration values can contain special characters, they should be URL-encoded or otherwise sanitized to prevent injection attacks or connection string parsing errors.

  • 18-26: Verify that the Debug mode does not lead to logging sensitive information such as database passwords. It's crucial to ensure that any debug logging is sanitized to prevent accidental leaks of credentials or other sensitive configuration details.

donut.go (2)
  • 9-31: The introduction of the donutCall struct and DonutCall interface with associated methods provides a clear and organized structure for the matchmaking service logic. The methods cover a wide range of functionalities, from starting and stopping the service to managing matchmakers and users. This encapsulation aligns with Go's idiomatic use of interfaces and struct methods.

  • 206-232: The utility functions processOneWayCall and processThreeWayCall are well-implemented to handle specific matchmaking scenarios. They provide clear and concise logic for dealing with one and three people remaining, respectively.

entity.go (19)
  • 8-13: The new MatchMakerStatus constants are well-defined and follow Go's naming conventions.

  • 16-21: The MatchMakerUserStatus constants are also well-defined and consistent with the MatchMakerStatus constants.

  • 24-26: The Person struct is simple and clear, but ensure that the Name field is sufficient for your use case and that no additional fields are required.

  • 30-38: The ToUserReferences method is a good utility for converting People to a slice of user references. Ensure that the Name field will always be populated to avoid adding empty strings to userReferences.

  • 41-45: The Get method provides safe access to the People slice. Good use of bounds checking.

  • 48-56: The Print method concatenates the names of People. Ensure that this method's output format is consistent with the application's logging or display requirements.

  • 59-63: The MatchMakerUserSerial type and its String method are straightforward. Ensure that the serials are generated in a way that guarantees uniqueness.

  • 65-71: The MatchMap type and its First method are clear. However, the First method will always return the first entry in the map, which may not be predictable due to the random order of map iteration in Go. If a specific order is required, consider using a slice or another data structure that maintains order.

  • 74-80: The MatchMakerEntity struct is well-defined. Ensure that all necessary fields are included and that the Status field's possible values are correctly handled throughout the application.

  • 83-107: The option pattern used for MatchMakerEntity is a good approach for optional parameters. Ensure that the default values set in the Build method are appropriate for all use cases.

  • 109-129: The Build method for MatchMakerEntity sets default values and applies options. Ensure that the GenerateSerial function generates a unique and appropriate serial for each entity.

  • 132-149: The Error method for MatchMakerEntity performs validation checks. Ensure that these checks cover all required fields and that the error messages are clear and actionable.

  • 152-156: The MatchMakerUserEntity struct is introduced with fields for serials and status. Ensure that the UserReference field is sufficient for identifying users and that no additional identification fields are needed.

  • 159-182: The option pattern is also used for MatchMakerUserEntity. Ensure that the options provided are sufficient for all use cases and that the default values are appropriate.

  • 185-194: The Build method for MatchMakerUserEntity applies options and sets a default status. Ensure that the default status is appropriate for new entities.

  • 197-210: The Error method for MatchMakerUserEntity performs validation checks. Ensure that these checks are comprehensive and that the error messages provide enough context for debugging.

  • 213-223: The ToPeople method converts MatchMakerUserEntities to People. Ensure that the conversion logic is consistent with the application's requirements for user representation.

  • 226-239: The ToMatchMap method converts MatchMakerUserEntities to a MatchMap. Ensure that the logic for creating and appending to the People slice within the map is correct and that the map's usage aligns with the application's needs.

  • 242-246: The MatchMakerInformation struct aggregates matchmaker data. Ensure that this struct provides all necessary information for the application's matchmaking logic and that it is used consistently across the codebase.

handler.go (1)
  • 11-123: The implementation of the Handler struct and its methods appears to be correct and follows Go's context pattern for RPC calls. The use of the connect package and the DonutCall interface methods within the handler methods is consistent with Go's idiomatic approach to service handlers.
instance.go (3)
  • 13-42: The NewDatabaseInstance function has been refactored to accept a *Config parameter and uses the DatabaseConfig from this struct to configure the GORM database instance. Ensure that all calls to NewDatabaseInstance throughout the codebase have been updated to pass the new *Config parameter.

The NewDatabaseInstance function now correctly accepts a *Config parameter, and the call to this function in main.go has been updated to pass the new parameter. No issues found with the changes related to NewDatabaseInstance.

  • 29-29: Verify that conf.DatabaseConfig.Dialect is a string type to ensure the error message formatting is correct. If Dialect is not a string, it should be converted to a string before being included in the error message.

The verification process did not yield any output, which means we cannot confirm the type of Dialect within the DatabaseConfig struct. Without this information, we cannot definitively say whether the error message formatting in instance.go is correct. However, given the absence of any output, it is also not possible to assert that there is an issue.

Final review comment:

The verification process did not provide information about the type of `conf.DatabaseConfig.Dialect`. Assuming standard conventions, `Dialect` is likely a string, and the error message formatting would be correct. However, without explicit confirmation, please ensure that `Dialect` is indeed a string to avoid any potential formatting issues.
  • 37-39: Returning instance.Debug() when conf.DatabaseConfig.Debug is true is a common pattern in GORM to enable debug logging on the database instance. This is a good use of conditional logic to toggle debugging.
main.go (1)
  • 17-70: The refactoring of the main function to set up the "donut" service appears to be comprehensive and includes proper error handling and graceful shutdown logic. The use of context for the shutdown process and the handling of OS signals are best practices for service applications.
presentation.go (3)
  • 58-67: The parseCallPeopleRequest function iterates over req.Msg.GetReferences() and appends to parsed inside the loop. This is correct, but it's important to ensure that req.Msg and req.Msg.GetReferences() are not nil to avoid a nil pointer dereference.

  • 70-81: The parseGetPeopleResponse function seems to be correctly converting a slice of Person entities to a slice of donutv1.Person protobuf messages. However, it's important to ensure that the people slice is not nil to avoid a runtime panic when calling len(people).

  • 1-9: The import of time and timestamppb suggests that these packages are used for handling time-related data. Ensure that time zones are consistently handled, especially when converting between time.Time and timestamppb.Timestamp.

repository.go (5)
  • 11-28: The introduction of the donutRepository struct and DonutRepository interface with CRUD operations for matchmakers and users aligns with the service's objectives.

  • 30-34: The NewDonutRepository function is correctly implemented to instantiate the repository with a database connection.

  • 36-47: The use of GORM's Create method and clause.OnConflict{DoNothing: true} in CreateMatchMakerUsers is a good practice to handle potential duplicate entries gracefully.

  • 83-164: The Get methods use parameterized queries, which is a secure practice to prevent SQL injection. The use of fmt.Sprintf is safe here as the column names are constants.

  • 79-81: The DeleteMatchMakerUsers method is implemented consistently with the other CRUD operations using GORM's Delete method.

serial.go (1)
  • 1-7: The implementation of GenerateSerial using github.com/google/uuid to generate a UUID is a standard approach for creating unique identifiers and should work well for the matchmaking system's needs.

dto.go Outdated Show resolved Hide resolved
database.go Outdated Show resolved Hide resolved
main.go Outdated Show resolved Hide resolved
repository.go Outdated Show resolved Hide resolved
presentation.go Outdated Show resolved Hide resolved
presentation.go Show resolved Hide resolved
donut.go Outdated Show resolved Hide resolved
instance.go Outdated Show resolved Hide resolved
donut.go Outdated Show resolved Hide resolved
donut.go Show resolved Hide resolved
@Aldiwildan77
Copy link
Member Author

grpc-ecosystem/grpc-gateway#3801 The implementation of the HTTP handler is currently on issue, so use grpc stub only.

Aldiwildan77 and others added 2 commits December 10, 2023 09:56
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 978419f and 063fbd7.
Files selected for processing (1)
  • repository.go (1 hunks)
Additional comments: 4
repository.go (4)
  • 11-28: The introduction of the donutRepository struct and DonutRepository interface is a good practice for abstracting the data layer operations and maintaining clean architecture. The methods provided cover a comprehensive set of operations for the "donut" service.

  • 40-47: The use of clause.OnConflict{DoNothing: true} in CreateMatchMakerUsers is a good practice to ensure idempotency during batch inserts and prevent errors when duplicate records are inserted.

  • 50-78: The use of transactions in UpdateStatusMatchMakerUsers ensures atomicity of updates across multiple records, which is crucial for maintaining data integrity.

  • 36-167: Consistent error handling across the database interaction methods contributes to the maintainability and readability of the code.

repository.go Outdated Show resolved Hide resolved
repository.go Show resolved Hide resolved
repository.go Outdated Show resolved Hide resolved
@Aldiwildan77
Copy link
Member Author

Aldiwildan77 commented Dec 10, 2023

@coderabbitai resolve

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 063fbd7 and 1a46d94.
Files selected for processing (8)
  • application.go (1 hunks)
  • database.go (1 hunks)
  • donut.go (1 hunks)
  • dto.go (1 hunks)
  • instance.go (1 hunks)
  • main.go (1 hunks)
  • presentation.go (1 hunks)
  • repository.go (1 hunks)
Files skipped from review as they are similar to previous changes (6)
  • application.go
  • database.go
  • donut.go
  • dto.go
  • instance.go
  • repository.go
Additional comments: 2
main.go (1)
  • 17-70: The refactoring of the main function to set up the "donut" service appears to be comprehensive and well-structured. The addition of context handling for graceful shutdown, the setup of HTTP server with h2c support, and the initialization of service handlers are all modern practices for a Go service.

However, ensure that the NewDonutRepository, NewDonutCall, and NewHandler functions are correctly implemented to handle the new donut service logic, as these are critical for the service initialization and are not shown in the provided code.

Also, verify that the donutv1connect.NewMatchMakerServiceHandler and donutv1connect.NewPeopleServiceHandler functions are correctly mapping the handlers to the service paths and that the handlers are implemented according to the requirements of the "donut" service.

The server is set to listen and serve using HTTP/2 with the h2c package, which is a good choice for performance. Ensure that all clients expected to interact with this service can support HTTP/2.

The graceful shutdown logic using context with a timeout is a good practice to ensure that the server can terminate connections smoothly. Make sure that the GracefulShutdownTimeout is configured with an appropriate value in the ApplicationConfig.

Overall, the changes to main.go align with the objectives of introducing the "donut" service and follow best practices for service setup and shutdown in Go.

presentation.go (1)
  • 90-95: Given the previous discussion, ensure that serial in parseGetPeoplePairResponse is indeed a value type and not a pointer to avoid potential runtime panics when calling String().

Given the previous discussion, the concern about the serial type in parseGetPeoplePairResponse being a pointer has been addressed. The command executed to verify the type of serial did not produce any output due to a syntax error, but based on the context and standard Go conventions, it is reasonable to infer that serial is likely a value type. If it were a pointer, the command would have likely matched the pattern. Therefore, it seems safe to proceed under the assumption that calling String() on serial will not cause runtime panics.

@Aldiwildan77 Aldiwildan77 merged commit 57e3778 into master Dec 10, 2023
@Aldiwildan77 Aldiwildan77 deleted the feat/make-a-service branch December 10, 2023 05:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant