diff --git a/.air.toml b/.air.toml index 162718c..e7575f9 100644 --- a/.air.toml +++ b/.air.toml @@ -3,7 +3,7 @@ testdata_dir = "testdata" tmp_dir = "tmp" [build] - bin = "tmp\\main.exe" + bin = "./tmp/main.exe" cmd = "go build -o ./tmp/main.exe ./cmd/server/main.go" delay = 1000 exclude_dir = ["assets", "tmp", "vendor", "testdata"] diff --git a/Golang_API.postman_collection.json b/Golang_API.postman_collection.json new file mode 100644 index 0000000..5d299b6 --- /dev/null +++ b/Golang_API.postman_collection.json @@ -0,0 +1,309 @@ +{ + "info": { + "_postman_id": "112e818d-eb80-4ac7-bd1f-181e32484495", + "name": "Golang_API", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "14791724" + }, + "item": [ + { + "name": "Auth", + "item": [ + { + "name": "Register", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"email\": \"admin@admin.com\",\r\n \"name\": \"Admin\",\r\n \"photo\": \"default.png\",\r\n \"password\": \"password123\",\r\n \"passwordConfirm\": \"password123\"\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:8000/api/auth/register", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "api", + "auth", + "register" + ] + } + }, + "response": [] + }, + { + "name": "Refresh Token", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "localhost:8000/api/auth/refresh", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "api", + "auth", + "refresh" + ] + } + }, + "response": [] + }, + { + "name": "Logout", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "localhost:8000/api/auth/logout", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "api", + "auth", + "logout" + ] + } + }, + "response": [] + }, + { + "name": "Login", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"email\": \"admin@admin.com\",\r\n \"password\": \"password123\"\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:8000/api/auth/login", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "api", + "auth", + "login" + ] + } + }, + "response": [] + }, + { + "name": "Verify Email Address", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "{{host}}/api/auth/verifyemail/M3ypLxqQMEMgRgWHwxee", + "host": [ + "{{host}}" + ], + "path": [ + "api", + "auth", + "verifyemail", + "M3ypLxqQMEMgRgWHwxee" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "User", + "item": [ + { + "name": "Get Me", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "localhost:8000/api/users/me", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "api", + "users", + "me" + ] + } + }, + "response": [] + } + ] + }, + { + "name": "Post", + "item": [ + { + "name": "Create Post", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"title\": \"My first Python FASTAPI app project\",\r\n \"category\": \"Python\",\r\n \"content\": \"My content haha My content haha\",\r\n \"image\": \"default.png\"\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:8000/api/posts", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "api", + "posts" + ] + } + }, + "response": [] + }, + { + "name": "Get Post", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "localhost:8000/api/posts/328cd0be-eeac-4be4-b764-7d3dc95695b2", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "api", + "posts", + "328cd0be-eeac-4be4-b764-7d3dc95695b2" + ] + } + }, + "response": [] + }, + { + "name": "Update Post", + "request": { + "method": "PATCH", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"title\": \"Updated My first golang GORM app project\",\r\n \"category\": \"Golang\"\r\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:8000/api/posts/e891ac2e-5371-45e3-a0d4-781e8c365905", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "api", + "posts", + "e891ac2e-5371-45e3-a0d4-781e8c365905" + ] + } + }, + "response": [] + }, + { + "name": "Delete Post", + "request": { + "method": "DELETE", + "header": [], + "url": { + "raw": "localhost:8000/api/posts/66e1374e-d1f5-4e70-ad61-1a014ba8309b", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "api", + "posts", + "66e1374e-d1f5-4e70-ad61-1a014ba8309b" + ] + } + }, + "response": [] + }, + { + "name": "Get All Posts", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "localhost:8000/api/posts?page=1&limit=10", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "api", + "posts" + ], + "query": [ + { + "key": "page", + "value": "1" + }, + { + "key": "limit", + "value": "10" + } + ] + } + }, + "response": [] + }, + { + "name": "Health Checker", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:8000/api/healthchecker", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "8000", + "path": [ + "api", + "healthchecker" + ] + } + }, + "response": [] + } + ] + } + ] +} \ No newline at end of file diff --git a/cmd/client/main.go b/cmd/client/main.go index 049fa45..07d7cb9 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -71,7 +71,7 @@ func main() { } // Create Post - if false { + if true { createPostClient := client.NewCreatePostClient(conn) args := &pb.CreatePostRequest{ diff --git a/cmd/server/main.go b/cmd/server/main.go index d6fdb20..e31da03 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -111,8 +111,12 @@ func main() { defer mongoclient.Disconnect(ctx) - // startGinServer(config) - startGrpcServer(config) + // Start gRPC server in a separate goroutine + go startGrpcServer(config) + + // Start Gin server in the main goroutine + startGinServer(config) + } func startGrpcServer(config config.Config) { diff --git a/readMe.md b/readMe.md index 65a9e2d..e0befc5 100644 --- a/readMe.md +++ b/readMe.md @@ -1,10 +1,180 @@ -# Build CRUD gRPC Server API & Client with Golang and MongoDB +# Build gRPC Server API & Client with Golang and MongoDB + +## 1. API with Golang + MongoDB + Redis + Gin Gonic: Project Setup + +In this article, you'll learn how to set up a Golang application with MongoDB-Go-driver, Gin Gonic, and Go Redis. Later, we'll access both the Redis and MongoDB databases directly in VS Code using a MySQL VS Code extension. + +![API with Golang + MongoDB + Redis + Gin Gonic: Project Setup](https://codevoweb.com/wp-content/uploads/2022/05/API-with-Golang-MongoDB-Redis-and-Gin-Gonic-Project-Setup.webp) + +### Topics Covered + +- Setup Golang with MongoDB and Redis +- Creating MongoDB and Redis Database with Docker-compose +- Setup Environment Variables +- How to Connect Golang App to Redis and MongoDB +- Test the Golang API +- How to Connect to MongoDB and Redis Servers in VS Code + +Read the entire article here: [https://codevoweb.com/api-golang-mongodb-gin-gonic-project-setup](https://codevoweb.com/api-golang-mongodb-gin-gonic-project-setup) + + +## 2. Golang & MongoDB: JWT Authentication and Authorization + +In this article, you'll learn how to implement RS256 JWT (JSON Web Token) Authentication and Authorization with Golang, Gin Gonic, MongoDB-Go-driver, and Docker-compose. + +![Golang & MongoDB: JWT Authentication and Authorization](https://codevoweb.com/wp-content/uploads/2022/05/Golang-and-MongoDB-JWT-Authentication-and-Authorization.webp) + +### Topics Covered + +- Golang & MongoDB JWT Authentication Overview +- JWT Authentication Example with Golang and MongoDB +- How to Generate Public and Private Keys +- Update Environment Variables with Viper +- Creating the User models with structs +- Creating an Auth and User Interfaces + - Authentication Interface + - User Interface +- Create utility functions to hash and verify password +- Create services that interact with the database + - Auth interface implementation + - User interface implementation +- Create a utility function to sign and verify JWT tokens + - Create Json Web Token + - Verify JSON Web Token +- Create the authentication controllers + - Signup user controller + - Login user controller + - Refresh access token controller + - Logout user controller +- Authentication Middleware Guard +- Create the user controllers +- Create API Routes with Gin + - Auth Routes + - User Routes +- Add the Routes to the Gin Middleware Pipeline + +Read the entire article here: [https://codevoweb.com/golang-mongodb-jwt-authentication-authorization](https://codevoweb.com/golang-mongodb-jwt-authentication-authorization) + +## 3. API with Golang + MongoDB: Send HTML Emails with Gomail + +In this article, you'll learn how to send HTML emails with Golang, Gomail, MongoDB-Go-Driver, Redis, and Docker-compose. Also, you'll learn how to generate HTML templates with the standard Golang html/template package. + +![API with Golang + MongoDB: Send HTML Emails with Gomail](https://codevoweb.com/wp-content/uploads/2022/05/API-with-Golang-MongoDB-Send-HTML-Emails-with-Gomail.webp) + +### Topics Covered + +- Send Emails with Golang, MongoDB, and Gomail Overview +- Creating the HTML Email Templates with Golang +- Create an SMTP Provider Account +- Load and Validate Environment Variables Viper +- Create a Utility Function to Send the Emails +- Update the SignUp Controller + +Read the entire article here: [https://codevoweb.com/api-golang-mongodb-send-html-emails-gomail](https://codevoweb.com/api-golang-mongodb-send-html-emails-gomail) + + +## 4. API with Golang, Gin Gonic & MongoDB: Forget/Reset Password + +In this article, you'll learn how to implement forget/reset password functionality with Golang, Gin Gonic, Gomail, MongoDB-Go-driver, Redis, and Docker-compose. + +![API with Golang, Gin Gonic & MongoDB: Forget/Reset Password](https://codevoweb.com/wp-content/uploads/2022/05/API-with-Golang-Gin-Gonic-MongoDB-Forget-Reset-Password.webp) + +### Topics Covered + +- Forget/Reset Password with Golang, Gin, and MongoDB +- Create the MongoDB Model Structs +- Create the HTML Email Templates with Golang +- Define a Utility Function to Parse the HTML Templates +- Create a Function to Send the HTML Emails +- Add the Forgot Password Controller +- Add the Reset Password Controller +- Register the Gin API Routes + +Read the entire article here: [https://codevoweb.com/api-golang-gin-gonic-mongodb-forget-reset-password](https://codevoweb.com/api-golang-gin-gonic-mongodb-forget-reset-password) + + +## 5. Build Golang gRPC Server and Client: SignUp User & Verify Email + +In this article, you'll learn how to create a gRPC server to register a user and verify their email address using Golang, MongoDB-Go-driver, Gomail, and Docker-compose. + +![Build Golang gRPC Server and Client: SignUp User & Verify Email](https://codevoweb.com/wp-content/uploads/2022/05/Build-Golang-gRPC-Server-and-Client-SignUp-User-Verify-Email.webp) + +### Topics Covered + +- gRPC Project setup in Golang +- Create the gRPC Request and Response Messages + - Define the gRPC User messages + - Define the gRPC Request and Response Message to SignUp User +- Create the gRPC Service Methods +- Generate the gRPC client and server interfaces +- Start the gRPC Server +- Test the gRPC API Server with Golang Evans +- Create the gRPC API Controllers + - Register User gRPC Controller + - Verify User gRPC Controller +- Create the gRPC Client to Register a User + +Read the entire article here: [https://codevoweb.com/golang-grpc-server-and-client-signup-user-verify-email](https://codevoweb.com/golang-grpc-server-and-client-signup-user-verify-email) + +## 6. Build Golang gRPC Server and Client: Access & Refresh Tokens + +In this article, you'll learn how to implement JWT access and refresh tokens with gRPC using Golang, MongoDB-Go-driver, Gomail, Docker, and Docker-compose. + +![Build Golang gRPC Server and Client: Access & Refresh Tokens](https://codevoweb.com/wp-content/uploads/2022/05/Build-Golang-gRPC-Server-and-Client-Access-Refresh-Tokens.webp) + +### Topics Covered + +- Create the gRPC Request and Response Messages + - Create the gRPC User messages + - Define the gRPC Request and Response Message to Login User + - Update the Authentication gRPC Service + - Create a gRPC User Service +- Create the gRPC Controllers +- Create the gRPC Servers +- Register the gRPC Servers +- Create the gRPC Clients in Golang +- Connect the gRPC Client to the gRPC Server + +Read the entire article here: [https://codevoweb.com/golang-grpc-server-and-client-access-refresh-tokens](https://codevoweb.com/golang-grpc-server-and-client-access-refresh-tokens) + +## 7. Build CRUD RESTful API Server with Golang, Gin, and MongoDB + +In this article, you'll learn how to build a CRUD RESTful API server with Golang, Gin Gonic, MongoDB-Go-driver, Docker, and Docker-compose. + +![Build CRUD RESTful API Server with Golang, Gin, and MongoDB](https://codevoweb.com/wp-content/uploads/2022/05/Build-CRUD-RESTful-API-Server-with-Golang-Gin-and-MongoDB.webp) + +### Topics Covered + +- Golang, Gin Gonic, MongoDB CRUD RESTful API Overview +- Create the Models with Structs +- Create the Service Interface +- Create Methods to Implement the Interface + - Initialize the Service Struct + - Define a Service to Create a Post + - Define a Service to Update Post + - Define a Service to Delete Post + - Define a Service to Get Single Post + - Define a Service to Get All Posts +- Create Controllers to Perform the CRUD Operations + - Initialize the Controller Struct + - Define a Controller to Create a Post + - Define a Controller to Update a Post + - Define a Controller to Delete a Post + - Define a Controller to Get a Single Post + - Define a Controller to Get All Posts +- Create the Routes for the Controllers +- Initialize the Constructors and Start the Gin Server + +Read the entire article here: [https://codevoweb.com/crud-restful-api-server-with-golang-and-mongodb](https://codevoweb.com/crud-restful-api-server-with-golang-and-mongodb) + + +## 8. Build CRUD gRPC Server API & Client with Golang and MongoDB In this article, you'll learn how to build a CRUD gRPC API server with Golang, MongoDB-Go-driver, and Docker-compose. You'll also build a gRPC client to interact with the gRPC API. ![Build CRUD gRPC Server API & Client with Golang and MongoDB](https://codevoweb.com/wp-content/uploads/2022/06/Build-CRUD-gRPC-Server-API-Client-with-Golang-and-MongoDB.webp) -## Topics Covered +### Topics Covered - Define the Models with Structs - Create the ProtoBuf Messages @@ -36,37 +206,3 @@ In this article, you'll learn how to build a CRUD gRPC API server with Golang, M - Register the gRPC Services Read the entire article here: [https://codevoweb.com/crud-grpc-server-api-client-with-golang-and-mongodb](https://codevoweb.com/crud-grpc-server-api-client-with-golang-and-mongodb) - -Articles in this series: - -### 1. API with Golang + MongoDB + Redis + Gin Gonic: Project Setup - -[API with Golang + MongoDB + Redis + Gin Gonic: Project Setup](https://codevoweb.com/api-golang-mongodb-gin-gonic-project-setup) - -### 2. Golang & MongoDB: JWT Authentication and Authorization - -[Golang & MongoDB: JWT Authentication and Authorization](https://codevoweb.com/golang-mongodb-jwt-authentication-authorization) - -### 3. API with Golang + MongoDB: Send HTML Emails with Gomail - -[API with Golang + MongoDB: Send HTML Emails with Gomail](https://codevoweb.com/api-golang-mongodb-send-html-emails-gomail) - -### 4. API with Golang, Gin Gonic & MongoDB: Forget/Reset Password - -[API with Golang, Gin Gonic & MongoDB: Forget/Reset Password](https://codevoweb.com/api-golang-gin-gonic-mongodb-forget-reset-password) - -### 5. Build Golang gRPC Server and Client: SignUp User & Verify Email - -[Build Golang gRPC Server and Client: SignUp User & Verify Email](https://codevoweb.com/golang-grpc-server-and-client-signup-user-verify-email) - -### 6. Build Golang gRPC Server and Client: Access & Refresh Tokens - -[Build Golang gRPC Server and Client: Access & Refresh Tokens](https://codevoweb.com/golang-grpc-server-and-client-access-refresh-tokens) - -### 7. Build CRUD RESTful API Server with Golang, Gin, and MongoDB - -[Build CRUD RESTful API Server with Golang, Gin, and MongoDB](https://codevoweb.com/crud-restful-api-server-with-golang-and-mongodb) - -### 8. Build CRUD gRPC Server API & Client with Golang and MongoDB - -[Build CRUD gRPC Server API & Client with Golang and MongoDB](https://codevoweb.com/crud-grpc-server-api-client-with-golang-and-mongodb) diff --git a/tmp/build-errors.log b/tmp/build-errors.log deleted file mode 100644 index b9a46ca..0000000 --- a/tmp/build-errors.log +++ /dev/null @@ -1 +0,0 @@ -exit status 1exit status 0xc000013aexit status 1exit status 1exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 2exit status 1exit status 1exit status 2exit status 1exit status 1exit status 2exit status 1exit status 2exit status 1exit status 2exit status 1exit status 1exit status 2exit status 2exit status 0xc000013aexit status 2 \ No newline at end of file