|
| 1 | +<h1 align="center"> Keploy Go Samples Apps </h1> |
| 2 | +<p align="center"> |
1 | 3 | ## Introduction
|
| 4 | +<a href="CODE_OF_CONDUCT.md" alt="Contributions welcome"> |
| 5 | + <img src="https://img.shields.io/badge/Contributions-Welcome-brightgreen?logo=github" /></a> |
2 | 6 |
|
| 7 | + <a href="https://join.slack.com/t/keploy/shared_invite/zt-12rfbvc01-o54cOG0X1G6eVJTuI_orSA" alt="Slack"> |
| 8 | + <img src=".github/slack.svg" /></a> |
| 9 | + |
| 10 | + <a href="https://opensource.org/licenses/Apache-2.0" alt="License"> |
| 11 | + <img src=".github/License-Apache_2.0-blue.svg" /></a> |
3 | 12 | A sample user authentication to test Keploy integration capabilities using [Gin](https://gin-gonic.com/) and [Redis](https://redis.io/).
|
4 | 13 |
|
5 | 14 | ## Setup URL shortener
|
@@ -29,24 +38,99 @@ We need create an alias for Keploy:
|
29 | 38 | alias keploy='sudo docker run --pull always --name keploy-v2 -p 16789:16789 --privileged --pid=host -it -v $(pwd):$(pwd) -w $(pwd) -v /sys/fs/cgroup:/sys/fs/cgroup -v /sys/kernel/debug:/sys/kernel/debug -v /sys/fs/bpf:/sys/fs/bpf -v /var/run/docker.sock:/var/run/docker.sock -v '"$HOME"'/.keploy-config:/root/.keploy-config -v '"$HOME"'/keploy-config:/root/keploy-config --rm ghcr.io/keploy/keploy'
|
30 | 39 | ```
|
31 | 40 |
|
32 |
| -### Update the Host |
| 41 | +### Update the Redis Connection Settings |
| 42 | + |
| 43 | +> **Important:** When running in Docker, the Redis URL should be `redis:6379` instead of `localhost:6379`. This needs to be updated in `helpers/redis/redisConnect.go` file. |
| 44 | +
|
| 45 | +```go |
| 46 | +// Change this: |
| 47 | +Addr: "localhost:6379" |
33 | 48 |
|
34 |
| -> **Since, we are on the docker image the Redis URL will be myredis:6379 instead of localhost:6379. This needs to be updated in `helpers/redis/redisConnect.go` file** |
| 49 | +// To this: |
| 50 | +Addr: "redis:6379" |
| 51 | +``` |
| 52 | + |
| 53 | +This change is necessary because in Docker, `localhost` points to the container itself, not the Redis service. Docker Compose uses an internal network where each service can be accessed via its container name. |
35 | 54 |
|
36 | 55 | ### Create a Docker network
|
37 | 56 | ```
|
38 | 57 | sudo docker network create <networkName>
|
39 | 58 | ```
|
40 | 59 |
|
41 |
| -### Let's start the Redis Instance |
42 |
| -Using the docker-compose file we will start our Redis instance:- |
| 60 | +### Using Docker Compose |
| 61 | + |
| 62 | +Create a `docker-compose.yml` file with the following content: |
| 63 | + |
| 64 | +```yaml |
| 65 | +version: '3.7' |
| 66 | +services: |
| 67 | + go-app: |
| 68 | + build: |
| 69 | + context: . |
| 70 | + container_name: ginRedisApp |
| 71 | + ports: |
| 72 | + - "3001:3001" |
| 73 | + environment: |
| 74 | + - REDIS_HOST=redis |
| 75 | + - REDIS_PORT=6379 |
| 76 | + depends_on: |
| 77 | + - redis |
| 78 | + redis: |
| 79 | + image: redis |
| 80 | + container_name: myredis |
| 81 | + ports: |
| 82 | + - "6379:6379" |
| 83 | +``` |
| 84 | +
|
| 85 | +This setup properly manages the dependency between the Go application and Redis server. |
| 86 | +
|
| 87 | +To start the application using Docker Compose: |
| 88 | +
|
43 | 89 | ```bash
|
44 |
| -sudo docker run -p 6379:6379 -d --network <networkName> --name myredis redis |
| 90 | +docker-compose up |
45 | 91 | ```
|
| 92 | + |
| 93 | +### Using individual Docker containers |
| 94 | + |
| 95 | +Alternatively, you can start Redis manually and build the application: |
| 96 | + |
46 | 97 | ```bash
|
| 98 | +sudo docker run -p 6379:6379 -d --network <networkName> --name myredis redis |
47 | 99 | docker build -t gin-app:1.0 .
|
48 | 100 | ```
|
49 | 101 |
|
| 102 | +### Communicating with Dockerized App Natively |
| 103 | + |
| 104 | +If you want to run your application in Docker but interact with it from your host machine or other environments, follow these steps: |
| 105 | + |
| 106 | +#### Option 1: Port Mapping (Already included above) |
| 107 | + |
| 108 | +The Docker Compose and individual container setups both include port mapping (`3001:3001`), which allows you to access the containerized application via `localhost:3001` from your host machine. |
| 109 | + |
| 110 | +#### Option 2: Using Docker's IP Address |
| 111 | + |
| 112 | +Find the Docker container's IP address: |
| 113 | + |
| 114 | +```bash |
| 115 | +docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ginRedisApp |
| 116 | +``` |
| 117 | + |
| 118 | +Then use this IP to communicate with the application: |
| 119 | + |
| 120 | +```bash |
| 121 | +curl --location 'http://<container-ip>:3001/api/[email protected]&username=shivamsourav' |
| 122 | +``` |
| 123 | + |
| 124 | +#### Option 3: Docker Host Network (Linux only) |
| 125 | + |
| 126 | +Run your container using the host network: |
| 127 | + |
| 128 | +```bash |
| 129 | +docker run --network host --name ginRedisApp gin-app:1.0 |
| 130 | +``` |
| 131 | + |
| 132 | +This will make the container share the host's network stack, allowing you to access it via `localhost:3001`. |
| 133 | + |
50 | 134 | ### Capture the Testcases
|
51 | 135 |
|
52 | 136 | ```shell
|
@@ -101,28 +185,60 @@ Now that we have our testcase captured, run the test file.
|
101 | 185 | ```shell
|
102 | 186 | keploy test -c "sudo docker run -p 3001:3001 --rm --network <networkName> --name ginRedisApp gin-app:1.0" --delay 10
|
103 | 187 | ```
|
| 188 | +**Note** :- Issue Creation is disabled on this Repository, please visit [here](https://github.com/keploy/keploy/issues/new/choose) to submit Issue. |
104 | 189 |
|
105 | 190 | So no need to setup dependencies like Redis, web-go locally or write mocks for your testing.
|
106 | 191 |
|
| 192 | +# Keploy Go Samples |
| 193 | +This repo contains the sample for [Keploy's](https://keploy.io)🐰 with Golang. Please feel free to contribute if you'd like submit a sample for another use-case or library. |
| 194 | + |
107 | 195 | **The application thinks it's talking to Redis 😄**
|
108 | 196 |
|
109 | 197 | We will get output something like this:
|
110 | 198 |
|
| 199 | +## Go Sample Apps with Keploy |
111 | 200 | 
|
112 | 201 |
|
| 202 | +1. [Echo-MySQL](https://github.com/keploy/samples-go/tree/main/echo-mysql) |
| 203 | +2. [Echo-SQL](https://github.com/keploy/samples-go/tree/main/echo-sql) |
| 204 | +3. [Fasthttp-Postgress](https://github.com/keploy/samples-go/tree/main/fasthttp-postgres) |
| 205 | +4. [Gin-Mongo](https://github.com/keploy/samples-go/tree/main/gin-mongo) |
| 206 | +5. [Gin-Redis](https://github.com/keploy/samples-go/tree/main/gin-redis) |
| 207 | +6. [Go-Grpc](https://github.com/keploy/samples-go/tree/main/go-grpc) |
| 208 | +7. [Go-Jwt](https://github.com/keploy/samples-go/tree/main/go-jwt) |
| 209 | +8. [Go-Twilio](https://github.com/keploy/samples-go/tree/main/go-twilio) |
| 210 | +9. [GraphQL-SQL](https://github.com/keploy/samples-go/tree/main/graphql-sql) |
| 211 | +10. [Mux-SQL](https://github.com/keploy/samples-go/tree/main/mux-sql) |
| 212 | +11. [Mux-Elasticsearch](https://github.com/keploy/samples-go/tree/main/mux-elasticsearch) |
| 213 | +12. [Mux-MySQL](https://github.com/keploy/samples-go/tree/main/mux-mysql) |
| 214 | +13. [S3-Keploy](https://github.com/keploy/samples-go/tree/main/S3-Keploy) |
| 215 | +14. [SSE-Svelte](https://github.com/keploy/samples-go/tree/main/sse-svelte) |
| 216 | +15. [Users-Profile](https://github.com/keploy/samples-go/tree/main/users-profile) |
| 217 | +16. [HTTP-PokeAPI](https://github.com/keploy/samples-go/tree/main/http-pokeapi) |
| 218 | +17. [book-store-inventory (`gin + sqlite`) ](https://github.com/keploy/samples-go/tree/main/book-store-inventory) |
| 219 | + |
113 | 220 | #### Let's add token to Noisy field:
|
114 | 221 |
|
115 | 222 | In `test-2.yml` go to the noisefield and `-body.token` in noise. Now, it's the time to run the test cases again.
|
116 | 223 |
|
| 224 | +## Community Support ❤️ |
117 | 225 | ```bash
|
118 | 226 | keploy test -c "sudo docker run -p 3001:3001 --rm --network <networkName> --name ginRedisApp gin-app:1.0" --delay 10
|
119 | 227 | ```
|
120 |
| - |
| 228 | +### 🤔 Questions? |
| 229 | +Reach out to us. We're here to help! |
121 | 230 | This time all the test cases will pass.
|
122 | 231 | 
|
123 | 232 |
|
| 233 | +[](https://join.slack.com/t/keploy/shared_invite/zt-12rfbvc01-o54cOG0X1G6eVJTuI_orSA) |
| 234 | +[](https://www.linkedin.com/company/keploy/) |
| 235 | +[](https://www.youtube.com/channel/UC6OTg7F4o0WkmNtSoob34lg) |
| 236 | +[](https://twitter.com/Keployio) |
| 237 | + |
124 | 238 | ## Run app Natively on local machine
|
125 | 239 |
|
| 240 | +### 💖 Let's Build Together! |
| 241 | +Whether you're a newbie coder or a wizard 🧙♀️, your perspective is golden. Take a peek at our: |
126 | 242 | Keploy can be installed on Linux directly and on Windows with the help of WSL. Based on your system archieture, install the keploy latest binary release
|
127 | 243 |
|
128 | 244 | **1. AMD Architecture**
|
@@ -240,4 +356,66 @@ sudo -E keploy test -c "./gin-redis" --delay 10
|
240 | 356 |
|
241 | 357 | This time all the test cases will pass.
|
242 | 358 |
|
243 |
| - |
| 359 | + |
| 360 | + |
| 361 | +## Known Issues and Fixes |
| 362 | + |
| 363 | +### Redis Connectivity Issues in Dockerized Gin Application |
| 364 | + |
| 365 | +When using Docker, you might face connectivity issues between the Gin application and Redis. Here's how to resolve them: |
| 366 | + |
| 367 | +#### Problem 1: Redis Connection Settings in Go Application |
| 368 | +**Issue:** |
| 369 | +The Go application might be configured to connect to Redis using: |
| 370 | +```go |
| 371 | +Addr: "localhost:6379" |
| 372 | +``` |
| 373 | +This configuration works when running the application locally (without Docker) but fails in a containerized environment. |
| 374 | + |
| 375 | +**Solution:** |
| 376 | +Update the Redis address in the application code to: |
| 377 | +```go |
| 378 | +Addr: "redis:6379" |
| 379 | +``` |
| 380 | +Where `redis` matches the service name defined in `docker-compose.yml`. |
| 381 | + |
| 382 | +#### Problem 2: Docker Compose Configuration |
| 383 | +**Issue:** |
| 384 | +The Docker Compose setup might not properly manage the dependency between the Go application and the Redis server. |
| 385 | + |
| 386 | +**Solution:** |
| 387 | +Improve the `docker-compose.yml` to properly manage service dependencies as shown in the Docker Compose section above. |
| 388 | + |
| 389 | +#### Environment-Variable Based Configuration (Recommended) |
| 390 | + |
| 391 | +For a more flexible setup, modify your Go code to use environment variables: |
| 392 | + |
| 393 | +```go |
| 394 | +func RedisConnect() *redis.Client { |
| 395 | + redisHost := os.Getenv("REDIS_HOST") |
| 396 | + if redisHost == "" { |
| 397 | + redisHost = "localhost" // Default fallback |
| 398 | + } |
| 399 | + |
| 400 | + redisPort := os.Getenv("REDIS_PORT") |
| 401 | + if redisPort == "" { |
| 402 | + redisPort = "6379" // Default fallback |
| 403 | + } |
| 404 | + |
| 405 | + rdb := redis.NewClient(&redis.Options{ |
| 406 | + Addr: redisHost + ":" + redisPort, |
| 407 | + Password: "", |
| 408 | + DB: 0, |
| 409 | + }) |
| 410 | + |
| 411 | + return rdb |
| 412 | +} |
| 413 | +``` |
| 414 | +📜 [Contribution Guidelines](https://github.com/keploy/keploy/blob/main/CONTRIBUTING.md) |
| 415 | +This way, you can easily configure the Redis connection for different environments without changing the code. |
| 416 | + |
| 417 | + |
| 418 | +❤️ [Code of Conduct](https://github.com/keploy/keploy/blob/main/CODE_OF_CONDUCT.md) |
| 419 | +**Future Improvements:** |
| 420 | +- Implement retry logic in the Go application to handle situations where Redis is not immediately available. |
| 421 | +- Add health checks to the Docker Compose file to ensure Redis is fully initialized before starting the Go application. |
0 commit comments