Categorygithub.com/ThembinkosiThemba/go-project-starter

# README

πŸš€ Golang Project Starter Kit

This is the Golang Project Starter Kit! πŸŽ‰ This a simple repository that contains starter code for a Golang project, following a Domain-Driven Design (DDD) approach. It simplifies the process of setting up a new project from scratch, allowing you to focus on building your application's core functionality.

In this setup, we use the User entity as an example, but you can easily replicate and add new entities as required for your specific project needs.

Table of Contents

  1. Features
  2. Getting Started
  3. Project Setup
  4. Database Setup
  5. Docker Setup
  6. Mixpanel
  7. Logger
  8. Customization
  9. Testing
  10. Contributing
  11. License

🌟 Features

  • πŸ—οΈ Domain-Driven Design architecture
  • βœ… Complete CRUD (Create, Read, Update, Delete) operations
  • πŸ“… Event tracking using Mixpanel
  • 🐳 Docker support which makes it easy to deploy project on multiple platforms like GCP, AWS etc
  • πŸ—„οΈ Database setup and integration
    • πŸƒ MongoDB support
    • 🐬My SQL and 🐘PostgreSQL support with advanced features:
      • Transaction handling
      • Prepared statements
      • Database migrations
  • 🌐 HTTP REST APIs using Gin-Gonic framework
    • Custom response handling
  • πŸ›‘οΈ Basic input validation
  • 🧩 Modular and extensible codebase

πŸš€ Getting Started

  1. Clone the repository:
git clone https://github.com/ThembinkosiThemba/go-project-starter.git
cd golang-project-starter
  1. Install dependencies and run the roject:
go mod tidy
make run

If you don't have Make installed, you can run

go run cmd/main.go
  1. Set up your environment variables (copy .env.example to .env and fill in your variables)

πŸ—οΈ Project Setup

πŸ’Ύ Database Setup

Depending on which database you are going to be using, make sure you update the main.go initialization lines so it works perfectly for your choice. By default, this project uses Mongo DB and this code is as follows:

userRepo, err := config.InitializeRepositoriesMongo()
if err != nil {
  log.Fatal(err)
}

userUsecase := config.InitializeUsecasesMongo(userRepo)

Notice we are using these two mongo functions which are InitializeRepositoriesMongo and InitializeUsecasesMongo

If for example you want to use Postgres, you will update these functions and use InitializeRepositoriesPostgres and InitializeUsecasesPostgres:

userRepo, err := config.InitializeRepositoriesPostgres()
if err != nil {
  log.Fatal(err)
}

userUsecase := config.InitializeUsecasesPostgres(userRepo)

MongoDB

  1. Ensure you have MongoDB installed and running. Alternatively, you can use Mongo DB Atlas, create a project, and get the connection string.
  2. Update the MongoDB connection string in your .env file

PostgreSQL

  1. Install PostgreSQL if you haven't already.
  2. Create a new database for your project.
  3. Update the PostgreSQL connection details in your .env file.
  4. Use the migration endpoint via curl or postman to run all necessary migrations.

Alternatively, you can use solutions like Aiven which has completely hosted db solutions. Think of it as Atlas, and it's completely free.

MYSQL

  1. Install MYSQL if you haven't already. You can also run mysql via docker and use that connection instead.
  2. Create a new database for your project.
  3. Update the MYSQL connection details in your .env file.
  4. Use the migration endpoint via curl or postman to run all necessary migrations.

Docker setup

Open the Dockerfile and rename make changes to the following line (/go-project-starter) to reflect the name of the project you are building.

RUN CGO_ENABLED=0 GOOS=linux go build -o /go-project-starter ./cmd/main.go

and

CMD [ "/go-project-starter" ]

If you are building say social-media-app, then you should have:

RUN CGO_ENABLED=0 GOOS=linux go build -o /social-media-app ./cmd/main.go

and

CMD [ "social-media-app" ]

Mixpanel

This project also has support for event tracking using Mixpanel. Login to Mixpanel and create a project, get the project id in the settings and update your env file as well.

Logger

This projects now supports a custom logger. Features include:

  • saving logs to files (errors, warnings and infos). These logs can be stored in their separate files. Check /logs folder once you use any of the logs.
  • print's out logs in the terminal

πŸ› οΈ Customization

To add a new entity:

  1. Create a new file in internal/entity for your entity
  2. Implement repository interfaces in internal/repository and choose either database.
  3. Create use cases in internal/application/usecase
  4. Add HTTP handlers in internal/routes/handler
  5. Update routes in internal/routes/handler/routes.go

πŸ§ͺ Testing

Coming soon...

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

# Packages

No description provided by the author
No description provided by the author