Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New sample application for keploy/sample-go #133

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
- S3-Keploy
- sse-svelte
- users-profile
- book-store-inventory

steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ This repo contains the sample for [Keploy's](https://keploy.io)🐰 with Golang.
14. [SSE-Svelte](https://github.com/keploy/samples-go/tree/main/sse-svelte)
15. [Users-Profile](https://github.com/keploy/samples-go/tree/main/users-profile)
16. [HTTP-PokeAPI](https://github.com/keploy/samples-go/tree/main/http-pokeapi)
17. [book-store-inventory (`gin + sqlite`) ](https://github.com/keploy/samples-go/tree/main/book-store-inventory)


## Community Support ❤️
Expand Down
32 changes: 32 additions & 0 deletions book-store-inventory/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Binaries and build artifacts
/book-store-inventory
/tmp/
*.exe
*.out
*.test

# Logs and debugging files
*.log
*.trace

# Environment and config files
.env
config.yaml

# Go module files (optional, but recommended for clean repo)
# /go.sum

# Compiled Python files (if any scripts are used)
__pycache__/
*.pyc
*.pyo
*.pyd

# OS-specific files
.DS_Store
Thumbs.db

# Editor and IDE-specific files
.vscode/
.idea/
*.iml
Binary file added book-store-inventory/DB/book_inventory.db
Binary file not shown.
209 changes: 209 additions & 0 deletions book-store-inventory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
<div align="center">
<!-- <img src="./images/api_logo.png" alt="API Logo" width="80" height="80"/> -->
</div>

<h1 align="center">Book Store Inventory</h1>

<div align="center">
<img src="https://img.shields.io/badge/Sample%20Project%20for%20-Keploy%20-ea580c?style=for-the-badge" alt="Showcase Project">
<br>
<img src="https://img.shields.io/badge/License-MIT-ed8796.svg?style=for-the-badge" alt="MIT License">
</div>
<br>

A simple book store inventory backend built using the Gin framework and SQLite as the database.

## :computer: Tech Stack
- [**Go**](https://go.dev/) : Programming language
- [**Gin**](https://github.com/gin-gonic/gin) : Web framework
- [**SQLite3**](https://github.com/mattn/go-sqlite3) : A sqlite3 database driver

<br>

## :book: How to Use / Run on Your Machine

- ### Prerequisites:
- Install Go (version >= 1.23.3): https://golang.org/dl/
- API Testing Tool
- [Postman](https://www.postman.com/downloads/)
- [Httpie](https://httpie.io/download)

> I am using [**HttPie**](https://httpie.io/download) for API testing
> you can use any tool as per your requirement
> and `curl` is fine too :D

> [!IMPORTANT]
> I am using WSL (Ubuntu). I recommend using Linux/WSL or macOS.

<br>

### :package: Keploy Installation
for detailed installation info visit
https://keploy.io/docs/server/installation/#keploy-installation

```sh
curl --silent -O -L https://keploy.io/install.sh && source install.sh
```

<br>

### :toolbox: Setup Project Locally:

- Clone the repository:
```sh
git clone https://github.com/keploy/samples-go.git
```

- Navigate to the project directory:
```sh
cd book-store-inventory
```

- Install dependencies:
```sh
go mod download
```
- Generate Database with some default data:

> [!IMPORTANT]
>
> `OPTIONAL STEP` as I have already included the `book_inventory.db`
>
> delete the `book_inventory.db` in `DB/book_inventory.db` and use below command to generate new one

```sh
go run migrations/migration.go
```

- Building the Application Binary:
```sh
go build
```

> :rocket: You're all set! Now you have `book-store-inventory`


<br>

### :inbox_tray: Capture the test cases using Keploy

```sh
sudo -E keploy record -c "./book-store-inventory
```
> [!TIP]
> If any error occur try above command without `sudo -E`


<br>

### :satellite: Open the Postman/Httpie and Make Request

<div align="center">
<img src="./docs/assets/all_books.png" alt="GET" style="width: 48%;">
<img src="./docs/assets/book_by_id.png" alt="READ" style="width: 48%;">
</div>

<div align="center">
<img src="./docs/assets/add_book.png" alt="PUT" style="width: 48%;">
<img src="./docs/assets/delete_book.png" alt="DELETE" style="width: 48%;">
</div>

<br>

### :bookmark_tabs: Available REST API End Points

| Method | Endpoint | Description |
|---------|-------------------------------------------------------|---------------------------------|
| **GET** | `http://localhost:9090/api/v1/books/` | Get all books |
| **GET** | `http://localhost:9090/api/v1/book/id/:book_id` | Get a book by ID |
| **POST** | `http://localhost:9090/api/v1/book/add/` | Add a new book |
| **DELETE** | `http://localhost:9090/api/v1/book/delete/` | Delete a book |
| **PATCH** | `http://localhost:9090/api/v1/book/update/` | Update a book |
| **GET** | `http://localhost:9090/api/test-api` | Test API (New Endpoint) |


<br>

> [!TIP]
>
> for now just call `GET` for `http://localhost:9090/api/v1/books/`
>
> and record this test

<br>

### :jigsaw: Usage Examples

> [!IMPORTANT]
> Add these json value as body while making request

- #### POST (Add New Book)

body of the request
```json
{
"title": "Gunaho ka Devta",
"author": "Dharamvir Bharti",
"price": 599.99,
"published_date": "1949-01-01T00:00:00Z",
"stock_quantity": 5
}
```

- #### DELETE (Delete Book by ID)

body of the request
```json
{
"book_id": 6
}
```

- #### PATCH (Update Book by ID)

body of the request
```json
{
"book_id": 6,
"title": "Gunaho ka God (latest edition)",
"author": "Dharamvir Bharti Ji",
"price": 599.99,
"published_date": "1949-01-01T00:00:00Z",
"stock_quantity": 5
}
```

<div align="center">
<img src="./docs/assets/keploy_test_capture.png" alt="TEST API">
</div>

<br>

### :test_tube: Run the captured test case

> [!IMPORTANT]
> Stop the test capturing by pressing `ctrl+c` or `ctrl+z`

start the test using keploy
```sh
sudo -E keploy test -c "./book-store-inventory" --delay 1
```

<div align="center">
<img src="./docs/assets/keploy_test_run_1.png" alt="PUT" style="width: 48%;">
<img src="./docs/assets/keploy_test_run_2.png" alt="DELETE" style="width: 48%;">
</div>

<br>

## :seedling: Todo / Future Improvements
- [x] Show All Books Data
- [x] Get Particular Book By ID
- [x] Add New Book Data
- [x] Delete Book Data
- [x] Update Book Data
- [ ] UI for this Application (NextJS/Vite-React)


## :compass: About
This project was created as a sample to understand the working of Keploy.
Binary file added book-store-inventory/docs/assets/add_book.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added book-store-inventory/docs/assets/book_by_id.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions book-store-inventory/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module github.com/saketV8/book-store-inventory

go 1.23.3

require (
github.com/gin-gonic/gin v1.10.0
github.com/mattn/go-sqlite3 v1.14.24
github.com/pressly/goose/v3 v3.24.1
github.com/sirupsen/logrus v1.9.3
)

require (
github.com/bytedance/sonic v1.12.6 // indirect
github.com/bytedance/sonic/loader v0.2.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.7 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.23.0 // indirect
github.com/goccy/go-json v0.10.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.12.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/protobuf v1.36.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading