This is a simple url shortener written in Golang and MySQL
- Golang How to install Golang
- Docker How to install Docker?
Run the following commands to setup the project
git clone https://github.com/keploy/samples-go.git && cd samples-go/mux-mysql
go mod download
curl -O https://raw.githubusercontent.com/keploy/keploy/main/keploy.sh && source keploy.sh
-
Spin up the mysql docker container
docker run -p 3306:3306 --rm --name mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
-
The MySQL Database takes some time to be ready after spinning up the container, you can wait a minute or just run the following command to monitor the logs
watch -n 1 docker logs mysql
-
Once we get the following message in the logs we are ready to start our golang server
2024-01-19 13:53:43+00:00 [Note] [Entrypoint]: Stopping temporary server 2024-01-19T13:53:43.854798Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.2.0). 2024-01-19T13:54:00.148602Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.2.0) MySQL Community Server - GPL.
-
Run the following command to build the executable out of our golang code
export ConnectionString="root:my-secret-pw@tcp(localhost:3306)/mysql" go build -o main -cover
If you get some error like this after running
./main
,[mysql] 2024/01/19 21:28:04 packets.go:37: unexpected EOF [mysql] 2024/01/19 21:28:04 packets.go:37: unexpected EOF [mysql] 2024/01/19 21:28:04 packets.go:37: unexpected EOF 2024/01/19 21:28:04 Couldnt create store driver: bad connection
This means that the mysql db is not ready yet, watch out for logs in Step 2 and wait for 20-30s
-
To capture test cases for our application use the following command
keploy record -c "./main"
-
To generate test cases, we can simply use cURL or Postman.
curl -X POST localhost:8080/create \ -d '{"link":"https://google.com"}'
This should return the following output
{ "message":"Converted", "link":"http://localhost:8080/link/1", "status":true }
-
To get all the links, run
curl localhost:8080/all
-
To get redirected to the real url, just use the shortened link in browser or run
curl localhost:8080/links/1
-
All these test cases are captured by Keploy
-
Just one simple command, run
keploy test -c "./main" --goCoverage
- Done..
-
Spin up the mysql docker container
docker run -p 3306:3306 --rm --name mysql --network keploy-network -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
-
The MySQL Database takes some time to be ready after spinning up the container, you can wait a minute or just run the following command to monitor the logs
watch -n 1 docker logs mysql
-
Once we get the following message in the logs we are ready to start our golang server
2024-01-19 13:53:43+00:00 [Note] [Entrypoint]: Stopping temporary server 2024-01-19T13:53:43.854798Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.2.0). 2024-01-19T13:54:00.148602Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.2.0) MySQL Community Server - GPL.
-
Build the docker image of the application
docker build -t url-short .
-
To start the server with keploy, run
keploy record -c "docker run -p 8080:8080 --name urlshort --rm --network keploy-network url-short:latest"
There you go, keploy has started capturing incomming requests
-
Make some API Requests: -
curl -X POST localhost:8080/create \ -d '{"link":"https://google.com"}'
This should return the following output
{ "message":"Converted", "link":"http://localhost:8080/link/1", "status":true }
To get all the links, run
curl localhost:8080/all
-
To run the tests, run
keploy test -c "docker run -p 8080:8080 --name urlshort --rm --network keploy-network url-short:latest"