|
| 1 | + |
| 2 | +# GRPC-CRUD |
| 3 | + |
| 4 | +A simple user management service built using Golang, ScyllaDB, gRPC and served through HTTP using gRPC-gateway which provides a RESTful API interface that proxies HTTP requests to compatible gRPC actions. |
| 5 | + |
| 6 | + |
| 7 | +## Tech Stack |
| 8 | + |
| 9 | +- **Go**: Primary programming language |
| 10 | +- **gRPC**: High-performance RPC framework |
| 11 | +- **gRPC-Gateway**: Proxies HTTP APIs to gRPC |
| 12 | +- **Protocol Buffers**: Interface definition language |
| 13 | +- **ScyllaDB**: Performant NoSQL database |
| 14 | +- **docker**/**docker-compose**: Containerization and environment setup |
| 15 | +- **GitHub Actions**: CI/CD pipeline for auto deployment |
| 16 | +- **Buf**: Protocol buffer compiler and dependency management |
| 17 | + |
| 18 | + |
| 19 | +## Project Structure |
| 20 | + |
| 21 | +``` |
| 22 | +. |
| 23 | +├── buf.gen.yaml |
| 24 | +├── buf.lock |
| 25 | +├── buf.yaml |
| 26 | +├── compose.yaml |
| 27 | +├── Dockerfile |
| 28 | +├── go.mod |
| 29 | +├── go.sum |
| 30 | +├── main.go |
| 31 | +├── proto |
| 32 | +│ └── users |
| 33 | +│ ├── users_grpc.pb.go |
| 34 | +│ ├── users.pb.go |
| 35 | +│ ├── users.pb.gw.go |
| 36 | +│ └── users.proto |
| 37 | +├── README.md |
| 38 | +└── src |
| 39 | + ├── db |
| 40 | + │ └── db.go |
| 41 | + ├── models |
| 42 | + │ └── User.go |
| 43 | + └── services |
| 44 | + └── userService.go |
| 45 | +``` |
| 46 | + |
| 47 | + |
| 48 | +## Prerequisites |
| 49 | + |
| 50 | +- Go 1.23.0 or later |
| 51 | +- Docker and Docker Compose |
| 52 | +- Buf CLI |
| 53 | +- Git |
| 54 | + |
| 55 | + |
| 56 | +## Setup and Installation |
| 57 | + |
| 58 | +### 1. Clone the Repository |
| 59 | + |
| 60 | +```bash |
| 61 | +git clone https://github.com/2k4sm/grpc-crud.git |
| 62 | +cd grpc-crud |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | +### 2. Start ScyllaDB with Docker Compose |
| 67 | + |
| 68 | +#### Set env variables into `.env`. |
| 69 | +```bash |
| 70 | +touch .env |
| 71 | +echo "SDB_URI=localhost" >> .env |
| 72 | +``` |
| 73 | +Then run |
| 74 | +```bash |
| 75 | +docker-compose up -d |
| 76 | +``` |
| 77 | + |
| 78 | + |
| 79 | +This will start a ScyllaDB instance as defined in the `compose.yaml` file. |
| 80 | + |
| 81 | +### 3. Install Dependencies |
| 82 | + |
| 83 | +```bash |
| 84 | +go mod download |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | +### 4. Install Protobuf deps and Generate Protocol Buffer Code using buf |
| 89 | + |
| 90 | +```bash |
| 91 | +buf dep update |
| 92 | +``` |
| 93 | +and |
| 94 | +```bash |
| 95 | +buf generate |
| 96 | +``` |
| 97 | + |
| 98 | +### 5. Build and Run the Application |
| 99 | + |
| 100 | +```bash |
| 101 | +go build -o grpc-crud . |
| 102 | +./grpc-crud |
| 103 | +``` |
| 104 | + |
| 105 | +Alternatively, you can use: |
| 106 | + |
| 107 | +```bash |
| 108 | +go run main.go |
| 109 | +``` |
| 110 | + |
| 111 | +### Local Ports |
| 112 | +- grpc-gateway(Http) -> 6969 |
| 113 | +- grpc(tcp) -> 8080 |
| 114 | + |
| 115 | +### Deployed to EC2 |
| 116 | + |
| 117 | +This is configured for automatic deployment to EC2 using GitHub Actions. When changes are pushed to the main branch, the CI/CD pipeline will: |
| 118 | + |
| 119 | +1. Build the application |
| 120 | +2. Deploy to the EC2 instance |
| 121 | + |
| 122 | +### Deployed URL: https://grpc-crud.2k4sm.tech |
| 123 | + |
| 124 | +## HTTP Endpoints |
| 125 | +- GET /users?email={email}: Get a user by email |
| 126 | + |
| 127 | + ```bash |
| 128 | + curl -X GET "http://localhost:6969/[email protected]" |
| 129 | + ``` |
| 130 | + |
| 131 | + or |
| 132 | + |
| 133 | + ```bash |
| 134 | + curl -X GET "http://localhost:6969/users?ph_number=1234567890" |
| 135 | + ``` |
| 136 | +- POST /users: Create a new user |
| 137 | + |
| 138 | + ```bash |
| 139 | + curl -X POST http://localhost:6969/users \ |
| 140 | + -H "Content-Type: application/json" \ |
| 141 | + -d '{ |
| 142 | + "first_name": "John", |
| 143 | + "last_name": "Doe", |
| 144 | + "gender": "MALE", |
| 145 | + "dob": "1990-01-01", |
| 146 | + "ph_number": "1234567890", |
| 147 | + |
| 148 | + "access": "UNBLOCKED" |
| 149 | + }' |
| 150 | + ``` |
| 151 | +- PUT /users/{email}: Update a user by email |
| 152 | + |
| 153 | + ```bash |
| 154 | + curl -X PUT http://localhost:6969/users/[email protected] \ |
| 155 | + -H "Content-Type: application/json" \ |
| 156 | + -d '{ |
| 157 | + "first_name": "John", |
| 158 | + "last_name": "Doe", |
| 159 | + "gender": "MALE", |
| 160 | + "dob": "1990-01-01", |
| 161 | + "ph_number": "0987654321", |
| 162 | + |
| 163 | + }' |
| 164 | + ``` |
| 165 | +- PATCH /users/{curr_email}: Update a user's email or phone number |
| 166 | +
|
| 167 | + ```bash |
| 168 | + curl -X PATCH http://localhost:6969/users/[email protected] \ |
| 169 | + -H "Content-Type: application/json" \ |
| 170 | + -d '{ |
| 171 | + |
| 172 | + "new_ph_number": "1122334455" |
| 173 | + }' |
| 174 | + ``` |
| 175 | +- POST /users/{email}/block: Block a user |
| 176 | +
|
| 177 | + ```bash |
| 178 | + curl -X POST http://localhost:6969/users/[email protected]/block |
| 179 | + ``` |
| 180 | +- POST /users/{email}/unblock: Unblock a user |
| 181 | +
|
| 182 | + ```bash |
| 183 | + curl -X POST http://localhost:6969/users/[email protected]/unblock |
| 184 | + ``` |
| 185 | +
|
| 186 | +# Thank You for trying out grpc-crud. |
0 commit comments