Skip to content

ale-neto/golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

147 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Golang

This is a basic user CRUD (Create, Read, Update, Delete) API built in Go, designed to demonstrate clean architecture, layered structure, and proper use of Go with MongoDB and Swagger. The project is based on teachings from the HunCoding YouTube channel and can be freely used, modified, or studied by others.

βœ… Inspired and guided by the amazing work of HunCoding

πŸ“Œ Project Information

βœ… Prerequisites

Before getting started, ensure the following tools are installed:

πŸš€ Getting Started

1. Clone the repository

git clone https://github.com/ale-neto/golang.git
cd golang

2. Run the project with Docker Compose

docker compose up

3. Or run MongoDB manually and start the app

docker container run --name golang-mongo -p 27017:27017 -d mongo
go run main.go

The app will be running at: http://localhost:8080


πŸ§ͺ API Testing

Once the application is running, you can test it using Swagger UI:

http://localhost:8080/swagger/index.html#

Or via tools like curl and Postman.

Examples with curl:

  • Create user:
curl -X POST -H "Content-Type: application/json" \
-d '{"name":"Alexandre", "email":"alexandre@example.com", "age":30, "password":"Test@123"}' \
http://localhost:8080/user/create
  • Update user:
curl -X PUT -H "Content-Type: application/json" \
-d '{"name":"Alexandre Neto"}' \
http://localhost:8080/user/create/{id}
  • Delete user:
curl -X DELETE http://localhost:8080/user/delete/{id}

🧾 Data Models

request.UserLogin

email     string // required, must be valid email
password  string // required, min 6 chars, at least one special (!@#$%*)

request.UserRequest

name      string // required, 4–100 chars
email     string // required, valid email
age       int    // required, between 1 and 140
password  string // required, min 6 chars, at least one special

request.UserUpdateRequest

name string // required
age  int    // required

response.UserResponse

id     string
name   string
email  string
age    int

rest_err.RestErr

code     int
error    string
message  string
causes   []struct {
  field   string
  message string
}

πŸ“‘ API Endpoints

For protected endpoints, include the token in the Authorization header: Bearer <your-access-token>

POST /user/create

  • Create a new user
  • Body: UserRequest
  • Response: 200 OK, 400 Bad Request, 500 Internal Server Error

DELETE /user/delete/{id}

  • Delete user by ID
  • Path param: id
  • Response: 200 OK, 400, 500

GET /user/email/{email}

  • Get user by email
  • Path param: email
  • Response: 200 OK, 404 Not Found, 400 Invalid ID

GET /user/id/{id}

  • Get user by ID
  • Path param: id
  • Response: 200 OK, 404 Not Found, 400 Invalid ID

POST /login

  • Login with credentials
  • Body: UserLogin
  • Response: 200 OK (returns token), 403 Forbidden

PUT /user/update/{id}

  • Update user info by ID
  • Path param: id
  • Body: UserUpdateRequest
  • Response: 200 OK, 400, 500

🀝 Contributing

Feel free to fork the project, open issues or submit pull requests.


🧑 Credits

This project is heavily based on the work of HunCoding. Special thanks for all the knowledge shared freely on his YouTube channel. This repo exists thanks to that!


πŸ“„ License

This project is open and free to use β€” no license restrictions apply.

Enjoy the code and happy coding with Go!

About

This is a basic user CRUD (Create, Read, Update, Delete) API built in Golang.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors