diff --git a/fasthttp-postgres/Dockerfile b/fasthttp-postgres/Dockerfile new file mode 100644 index 00000000..d26ecb22 --- /dev/null +++ b/fasthttp-postgres/Dockerfile @@ -0,0 +1,21 @@ +# ---------- Build stage ---------- +FROM golang:1.22-alpine AS builder + +WORKDIR /app + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main main.go + +# ---------- Run stage ---------- +FROM alpine:3.19 + +WORKDIR /root/ + +COPY --from=builder /app/main . +COPY --from=builder /app/migrations ./migrations + +EXPOSE 8080 +CMD ["./main"] diff --git a/fasthttp-postgres/README.md b/fasthttp-postgres/README.md index 4bbadc90..76c0b5f5 100644 --- a/fasthttp-postgres/README.md +++ b/fasthttp-postgres/README.md @@ -6,7 +6,6 @@ A sample application that get, create, update, and delete the data of a user in ``` git clone https://github.com/keploy/samples-go && cd fasthttp-postgres -go mod download ``` ## Installation @@ -19,50 +18,107 @@ Keploy can be used on Linux, Windows and MacOS through [Docker](https://docs.doc > Note: To run Keploy on MacOS through [Docker](https://docs.docker.com/desktop/release-notes/#4252) the version must be ```4.25.2``` or above. -### Let's start the MongoDB Instance -Using the docker-compose file we will start our mongodb instance:- +### Option 1: Run with Docker + +#### Capture testcases: +```shell +keploy record -c "docker compose up" --container-name=fasthttp_app +``` + + +##### To genereate testcases we just need to make some API calls. You can use [Postman](https://www.postman.com/), [Hoppscotch](https://hoppscotch.io/), or simply `curl`: - + +###### 1. Post Requests +```shell +curl -X POST -H "Content-Type: application/json" -d '{"name":"Author Name"}' http://localhost:8080/authors +``` + +```shell +curl -X POST -H "Content-Type: application/json" -d '{"title":"Book Title","author_id":1}' http://localhost:8080/books +``` + +###### 2. Get Requests ```bash -sudo docker-compose up -d postgres +curl -i http://localhost:8080/books ``` -Now, we will create the docker image of our application:- +![Keploy Testcases](./img/testcases.png) + +#### Run captured tests: + +Now that we have our testcase captured, run the test file. + +```shell +keploy test -c "docker compose up" --container-name=fasthttp_app --delay 10 +``` + +![alt text](./img/testrun.png) + + +### Option 2: Run Without Docker + +> Note: This application requires the following database environment variables +> to be set in order to run correctly. +> +> Create a .env file in this directory with the following values: +> +> ```env +> DB_HOST=localhost +> DB_PORT=5432 +> DB_USER=postgres +> DB_PASSWORD=password +> DB_NAME=db +> ``` + +#### Start the Postgres container: ```bash -go build -cover +docker compose up -d postgres ``` -### Capture the Testcases +### Build the Application +```bash +go build -o app +``` +#### Capture testcases: ```shell keploy record -c "./app" ``` -To genereate testcases we just need to make some API calls. You can use [Postman](https://www.postman.com/), [Hoppscotch](https://hoppscotch.io/), or simply `curl`: - +> Note: The server would be running on http://localhost:8080 + -1. Post Requests +##### To genereate testcases we just need to make some API calls. You can use [Postman](https://www.postman.com/), [Hoppscotch](https://hoppscotch.io/), or simply `curl`: - + +###### 1. Post Requests ```shell curl -X POST -H "Content-Type: application/json" -d '{"name":"Author Name"}' http://localhost:8080/authors - +``` +```shell curl -X POST -H "Content-Type: application/json" -d '{"title":"Book Title","author_id":1}' http://localhost:8080/books ``` -2. Get Requests +###### 2. Get Requests ```bash curl -i http://localhost:8080/books ``` + ![Keploy Testcases](./img/testcases.png) -### Run captured tests +#### Run captured tests Now that we have our testcase captured, run the test file. ```shell -keploy test -c "./app" --goCoverage --delay 10 +keploy test -c "./app" --delay 10 ``` ![alt text](./img/testrun.png) + _Voila! Our testcases have passed🥳_ . We can also notice that by capturing just few API calls we got around 88.5% of aggregated coverage with keploy generated testcases If you like the sample application, Don't forget to star us ✨ + \ No newline at end of file diff --git a/fasthttp-postgres/docker-compose.yaml b/fasthttp-postgres/docker-compose.yaml index 2984fc0f..431b7921 100644 --- a/fasthttp-postgres/docker-compose.yaml +++ b/fasthttp-postgres/docker-compose.yaml @@ -1,14 +1,29 @@ version: '3.7' services: - postgres: - image: postgres:10.5 - container_name: postgresDB - restart: always - environment: - - POSTGRES_DB=db - - POSTGRES_USER=postgres - - POSTGRES_PASSWORD=password - ports: - - '5432:5432' - volumes: - - ./migrations:/docker-entrypoint-initdb.d \ No newline at end of file + postgres: + image: postgres:10.5 + container_name: postgresDB + restart: always + environment: + - POSTGRES_DB=db + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=password + ports: + - '5432:5432' + volumes: + - ./migrations:/docker-entrypoint-initdb.d + + app: + build: . + container_name: fasthttp_app + ports: + - "8080:8080" + depends_on: + - postgres + environment: + - DB_HOST=postgres + - DB_PORT=5432 + - DB_USER=postgres + - DB_PASSWORD=password + - DB_NAME=db + restart: always diff --git a/fasthttp-postgres/internal/app/app.go b/fasthttp-postgres/internal/app/app.go index 42446c15..ab8f652a 100644 --- a/fasthttp-postgres/internal/app/app.go +++ b/fasthttp-postgres/internal/app/app.go @@ -5,6 +5,7 @@ import ( "database/sql" "fasthttp-postgres/internal/handlers" "fasthttp-postgres/internal/repository" + "fmt" "log" "net/http" "os" @@ -13,13 +14,38 @@ import ( "time" "github.com/fasthttp/router" + // Import pq driver for PostgreSQL + _ "github.com/lib/pq" "github.com/valyala/fasthttp" ) func InitApp() error { time.Sleep(2 * time.Second) + // Database connection initialization - uri := "postgresql://postgres:password@localhost:5432/db?sslmode=disable" + dbHost := os.Getenv("DB_HOST") + if dbHost == "" { + dbHost = "localhost" + } + dbPort := os.Getenv("DB_PORT") + if dbPort == "" { + dbPort = "5432" + } + dbUser := os.Getenv("DB_USER") + if dbUser == "" { + dbUser = "postgres" + } + dbPassword := os.Getenv("DB_PASSWORD") + if dbPassword == "" { + dbPassword = "password" + } + dbName := os.Getenv("DB_NAME") + if dbName == "" { + dbName = "db" + } + + uri := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s?sslmode=disable", + dbUser, dbPassword, dbHost, dbPort, dbName) db, err := sql.Open("postgres", uri) if err != nil { log.Print("Error connecting to database:", err)