# 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
- Features
- Getting Started
- Project Setup
- Database Setup
- Docker Setup
- Mixpanel
- Logger
- Customization
- Testing
- Contributing
- 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
- Clone the repository:
git clone https://github.com/ThembinkosiThemba/go-project-starter.git
cd golang-project-starter
- 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
- 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
- Ensure you have MongoDB installed and running. Alternatively, you can use Mongo DB Atlas, create a project, and get the connection string.
- Update the MongoDB connection string in your
.env
file
PostgreSQL
- Install PostgreSQL if you haven't already.
- Create a new database for your project.
- Update the PostgreSQL connection details in your
.env
file. - 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
- Install MYSQL if you haven't already. You can also run mysql via docker and use that connection instead.
- Create a new database for your project.
- Update the MYSQL connection details in your
.env
file. - 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:
- Create a new file in
internal/entity
for your entity - Implement repository interfaces in
internal/repository
and choose either database. - Create use cases in
internal/application/usecase
- Add HTTP handlers in
internal/routes/handler
- 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.