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

go-api: Postgres & sqlc #80

Merged
merged 11 commits into from
Jun 10, 2024
Merged

go-api: Postgres & sqlc #80

merged 11 commits into from
Jun 10, 2024

Conversation

darioscrivano
Copy link
Contributor

@darioscrivano darioscrivano commented Feb 22, 2024

What's this PR do?

Go API using PostgresQL as DB and sqlc as a query generator.

Summary by CodeRabbit

  • New Features

    • Introduced a multi-stage Dockerfile for building and running a Golang application.
    • Added Docker Compose configuration for setting up a PostgreSQL database and Golang API service.
    • Implemented REST API endpoints for user management (list, get, create users).
  • Documentation

    • Updated README with instructions for containerizing a Go API and PostgreSQL database using Docker Compose.
  • Chores

    • Updated .gitignore to include patterns for ignoring binaries, test outputs, and other development artifacts.
    • Added configuration for SQLC code generation and SQL linting with sqlfluff.

Copy link

coderabbitai bot commented Feb 22, 2024

Walkthrough

This update introduces a comprehensive setup for a Golang API with a PostgreSQL database. It includes a multi-stage Dockerfile, Docker Compose configuration, database migrations, SQL queries, and a REST API with user management capabilities. The project is structured to leverage sqlc for SQL query generation and includes handlers, services, and repositories to manage user data.

Changes

File(s) Change Summary
.gitignore Updated to ignore binaries, test outputs, dependency directories, workspace files, and specific directories like users/ and data/.
Dockerfile New multi-stage Dockerfile for building and running the Golang application using gcr.io/distroless/base-debian12.
README.md Instructions for containerizing the Go API and PostgreSQL database using Docker Compose, with emphasis on sqlc for SQL queries and models generation.
compose.yml Docker Compose configuration for setting up PostgreSQL and Golang API services.
db/database.go Defines DB struct and New function for database connection initialization.
db/migrations/schema.sql SQL script for creating a users table with user details.
db/query/user.sql SQL queries for user operations like getting a user by ID, listing all users, and creating a new user.
db/sqlc/db.go, db/sqlc/models.go, db/sqlc/querier.go, db/sqlc/user.sql.go Definitions for database transactions, user struct, querier interface, and generated code for database interaction.
handler/handler.go Functionality for handling HTTP requests related to user management, including listing users, getting user by ID, and creating new users.
main.go Sets up a REST API using Gorilla Mux, connects to PostgreSQL, performs migrations, and defines routes for user operations.
model/model.go Defines a User struct for user details.
repository/repository.go Introduces IUserRepository interface and UserRepository struct for user data access methods.
service/service.go Defines IUserService interface and UserService struct for user-related operations using a repository.
sqlc.yaml Configuration settings for SQLC code generation for the Golang API with PostgreSQL.
.sqlfluff Configuration setting for sqlfluff to specify the SQL dialect as postgres.

In code we trust, our API's might,
With Docker's touch, we launch the flight.
PostgreSQL stands strong, a data knight,
sqlc crafts queries, pure delight.
Users join, our app ignites,
A rabbit's joy, code's shining light. 🐇✨


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 your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

github-actions bot commented Feb 22, 2024

Warnings
⚠️ This PR is changing more than 200 lines.
⚠️ This PR is changing more than 10 files.
Messages
📖 Thanks for updating docs! We ❤️ documentation!

Generated by 🚫 dangerJS against 44ad8f0

Copy link

@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: 5

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 124df78 and 197ecee.
Files ignored due to path filters (4)
  • examples/golang-api-with-postgres-and-sqlc/compose.yml is excluded by: !**/*.yml
  • examples/golang-api-with-postgres-and-sqlc/go.mod is excluded by: !**/*.mod
  • examples/golang-api-with-postgres-and-sqlc/go.sum is excluded by: !**/*.sum
  • examples/golang-api-with-postgres-and-sqlc/sqlc.yaml is excluded by: !**/*.yaml
Files selected for processing (8)
  • examples/golang-api-with-postgres-and-sqlc/.gitignore (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/Dockerfile (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/README.md (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/db/database.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/handlers/handlers.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/main.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/query.sql (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/schema.sql (1 hunks)
Files skipped from review due to trivial changes (2)
  • examples/golang-api-with-postgres-and-sqlc/.gitignore
  • examples/golang-api-with-postgres-and-sqlc/README.md
Additional comments: 1
examples/golang-api-with-postgres-and-sqlc/query.sql (1)
  • 1-14: The SQL queries for user operations are correctly parameterized, which helps prevent SQL injection. Good job following best practices here. Ensure that the username column is indexed to optimize the performance of the ListUsers query, as mentioned in the review of schema.sql.

examples/golang-api-with-postgres-and-sqlc/schema.sql Outdated Show resolved Hide resolved
examples/golang-api-with-postgres-and-sqlc/main.go Outdated Show resolved Hide resolved
examples/golang-api-with-postgres-and-sqlc/Dockerfile Outdated Show resolved Hide resolved
examples/golang-api-with-postgres-and-sqlc/db/database.go Outdated Show resolved Hide resolved
Copy link

@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 197ecee and 56b4e59.
Files ignored due to path filters (2)
  • examples/golang-api-with-postgres-and-sqlc/compose.yml is excluded by: !**/*.yml
  • examples/golang-api-with-postgres-and-sqlc/sqlc.yaml is excluded by: !**/*.yaml
Files selected for processing (2)
  • examples/golang-api-with-postgres-and-sqlc/README.md (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/schema.sql (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • examples/golang-api-with-postgres-and-sqlc/README.md
  • examples/golang-api-with-postgres-and-sqlc/schema.sql

Copy link

@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.

Actionable comments posted: 8

Out of diff range and nitpick comments (1)
examples/golang-api-with-postgres-and-sqlc/README.md (1)

14-19: Correct typographical error in the prerequisites section.

There's a repeated word "Docker" in the prerequisites section. Consider correcting this to improve the clarity of the documentation.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 56b4e59 and 9f1a61b.
Files ignored due to path filters (2)
  • examples/golang-api-with-postgres-and-sqlc/go.mod is excluded by !**/*.mod
  • examples/golang-api-with-postgres-and-sqlc/go.sum is excluded by !**/*.sum
Files selected for processing (17)
  • examples/golang-api-with-postgres-and-sqlc/.gitignore (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/Dockerfile (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/README.md (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/compose.yml (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/db/database.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/db/migrations/schema.sql (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/db/query/user.sql (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/db/sqlc/db.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/db/sqlc/models.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/db/sqlc/querier.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/db/sqlc/user.sql.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/handler/handler.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/main.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/model/model.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/repository/repository.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/service/service.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/sqlc.yaml (1 hunks)
Files skipped from review due to trivial changes (7)
  • examples/golang-api-with-postgres-and-sqlc/.gitignore
  • examples/golang-api-with-postgres-and-sqlc/Dockerfile
  • examples/golang-api-with-postgres-and-sqlc/compose.yml
  • examples/golang-api-with-postgres-and-sqlc/db/migrations/schema.sql
  • examples/golang-api-with-postgres-and-sqlc/db/sqlc/querier.go
  • examples/golang-api-with-postgres-and-sqlc/model/model.go
  • examples/golang-api-with-postgres-and-sqlc/service/service.go
Additional Context Used
LanguageTool (1)
examples/golang-api-with-postgres-and-sqlc/README.md (1)

Near line 16: Possible typo: you repeated a word
Context: ...erall performance. ## Prerequisites - Docker - Docker Compose - sqlc ## Setup 1. **Clone ...

Additional comments not posted (5)
examples/golang-api-with-postgres-and-sqlc/sqlc.yaml (1)

1-20: Configuration settings in sqlc.yaml are correctly set up.

The SQLC configuration is appropriately set for PostgreSQL with pgx/v5, and the settings like emit_interface and emit_empty_slices are beneficial for the generated code structure.

examples/golang-api-with-postgres-and-sqlc/db/sqlc/db.go (1)

14-32: Database transaction setup in db.go is well-implemented.

The DBTX interface and Queries struct are correctly set up for handling database transactions using pgx, and the use of context in these operations is a best practice.

examples/golang-api-with-postgres-and-sqlc/db/sqlc/user.sql.go (2)

46-74: The implementation of GetUser is correct and follows best practices.


76-116: The implementation of ListUsers is correct and efficiently handles database rows and errors.

examples/golang-api-with-postgres-and-sqlc/handler/handler.go (1)

59-79: The implementation of GetUser correctly handles user ID parsing and error responses.

Copy link

@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.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 9f1a61b and 7fe166d.
Files selected for processing (2)
  • examples/golang-api-with-postgres-and-sqlc/.gitignore (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/Dockerfile (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • examples/golang-api-with-postgres-and-sqlc/.gitignore
  • examples/golang-api-with-postgres-and-sqlc/Dockerfile

Copy link

@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.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 7fe166d and 80835fa.
Files selected for processing (3)
  • examples/golang-api-with-postgres-and-sqlc/handler/handler.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/repository/repository.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/service/service.go (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • examples/golang-api-with-postgres-and-sqlc/handler/handler.go
  • examples/golang-api-with-postgres-and-sqlc/repository/repository.go
  • examples/golang-api-with-postgres-and-sqlc/service/service.go

Copy link

@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.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 80835fa and a30be16.
Files selected for processing (1)
  • examples/golang-api-with-postgres-and-sqlc/.sqlfluff (1 hunks)
Files skipped from review due to trivial changes (1)
  • examples/golang-api-with-postgres-and-sqlc/.sqlfluff

Copy link

@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.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between a30be16 and 44ad8f0.
Files selected for processing (2)
  • examples/golang-api-with-postgres-and-sqlc/db/database.go (1 hunks)
  • examples/golang-api-with-postgres-and-sqlc/main.go (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • examples/golang-api-with-postgres-and-sqlc/db/database.go
  • examples/golang-api-with-postgres-and-sqlc/main.go

@ulises-jeremias ulises-jeremias merged commit cf44a24 into main Jun 10, 2024
13 of 19 checks passed
@ulises-jeremias ulises-jeremias deleted the go/api-postgres-sqlc branch June 10, 2024 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants