modulepackage
0.0.0-20250108000632-4dc1058d97ce
Repository: https://github.com/dargasht/gocrud.git
Documentation: pkg.go.dev
# README
gocrud
Template for go crud applications we use. Also some generic handler functions and helpers we use.
Installation
go get github.com/dargasht/gocrud
What we use
- go-fiber for http server
- pgx/v5 for database
- pgxpool for database connection pool
- sonic for json marshall/unmarshall
- zap for logging
- sqlc for database code generation
- sqlc for some wierd queries that can't be done with sqlc
- goose for database migrations
what we enjoy
- excessive use of postgres json_agg function
- simple architecture with no magic (maybe some magic, but not too much)
- no ORM
- writing raw sql queries
- simplicity simplicity and simplicity
Folder structure
.
├── bin # for binaries (should be added in gitignore)
├── cfg # for configs
├── cmd
│ └── api
│ └── main.go # the main entry point
├── database
│ ├── migration # for database migrations
│ ├── query # for database queries
│ ├── repo # for sqlc generated queries and models
│ └── store # for specific database implementations that uses sqlx
├── handler # for http handlers (data transformation and validation)
├── middleware # for http middlewares (if needed)
├── model # for service models and handler models (DTOs)
├── router # for instantiating handlers and injecting dependencies
├── service # for business logic (if needed)
└── utils # for utility functions
Things to keep in mind:
- services should be stateless and not depend on external services
- use service layer if business logic is not simple otherwise use handler layer
- go to each folder in the internal folder and read the README.md
Usefull stuff in this package are:
-
-
GoCRUDConfig: THIS IS VERY IMPORTANT, YOU SHOULD SET THIS TO YOUR NEEDS IN YOUR MAIN FILE 🔥🔥🔥🔥🔥
-
-
Error helper stuff: Just read comments in code
-
handler helper stuff: This contains stuff usefull to use in your handlers, they are (Just read comments in code)
-
jwt helper stuff: Just read comments in code
-
kavenegar helper stuff: 2 functions
- SendOTP: For sending otp
- ValidateOTP: For validating otp
- Seems simple enough 🤷♂️🤷 not much to explain
-
S3 helpers: 2 functions
- SetupS3Client: For setting up the s3 client, you should call this in the main file and set it to gocrud.GoCRUDConfig Object
- UploadFormFileToS3: For uploading files to s3
# Functions
This is Very usefull for authenticating the user Usefull for most crud applications.
This function for handling errors that are returned from the handlers Just use it.
This is for decoding the jwt not much to explain.
EnsureAdmin checks if the user is admin You can make this in a middleware This is just an example for a system who has admin role You can use it as a template for your system and roles.
This is for generating tokens not much to explain.
This is for getting the jwt from header not much to explain.
GetPagination returns page, limit and offset.
GetPaginationMeta returns pagination meta For putting in the response.
GetPaginationNoLimit returns page, limit and offset Use in rare cases that user wants to get alot of data.
For cases that a request can contain or not contain a token Use this to get the user id.
No description provided by the author
No description provided by the author
Only use when there is no error and want to create a new error I barely use this one.
------------------------------------------------------------------ ------------------- Database Errors ------------------------------ ------------------------------------------------------------------.
No description provided by the author
No description provided by the author
Creates a new handler config This is not recommanded to use this just write your own.
No description provided by the author
------------------------------------------------------------------ ---------------- JSON and Validation Errors ---------------------- ------------------------------------------------------------------.
Used for 404.
------------------------------------------------------------------ ------------------- Other Errors --------------------------------- ------------------------------------------------------------------.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
This is for sending otp.
SetConfig sets the config Look at the main file in the example.
This is for setting up the db call this in the main file and set it to gocrud.GoCRUDConfig Object.
UploadToS3 uploads a file to S3.
This is for validating otp.
Options.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ERROR MESSAGES.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Variables
Some Simple errors.
GoCRUDConfig is the default config You should set this to your needs in your main file Look at the main file in the example.
# Structs
Config for GoCRUD.
No description provided by the author
Example of what a handler config should look like It is not recommanded to use this just write your own.
No description provided by the author
No description provided by the author
No description provided by the author
Standard response suitable for most handlers.
Standard response suitable for handlers who do pagination In go 1.24 when the new omitzero json tag will be introduced we can merge the 2 responses probably.
# Type aliases
No description provided by the author