Skip to content

The Expense Tracker API is a RESTful service built with the Gin framework in Go

License

Notifications You must be signed in to change notification settings

letsmakecakes/expense-tracker-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Expense Tracker API

The Expense Tracker API is a RESTful service built with the Gin framework in Go. It provides endpoints to manage user credentials and track expenses, including features like JWT-based authentication, secure password hashing, and CRUD operations on expense records.

Features

  • Authentication:
    • User Signup and Login with JWT-based token generation.
  • Expense Management:
    • Add, update, delete, and retrieve expenses.
    • Filter expenses by categories like Groceries, Electronics, etc.
  • Security:
    • Passwords are hashed for secure storage.
    • Token validation with middleware for protected routes.

Table of Contents

  1. Requirements
  2. Setup
  3. API Endpoints
  4. Environment Variables
  5. Run Locally
  6. Testing with cURL
  7. License

Requirements

  • Go (v1.20+ recommended)
  • PostgreSQL/MySQL/SQLite (as the database backend)
  • cURL (for testing API endpoints)
  • Git (for version control)

Setup

  1. Clone the repository:

    git clone https://github.com/username/expense-tracker-api.git
    cd expense-tracker-api
  2. Install dependencies:

    go mod download
  3. Set up the database:

    • Create a database for the project.
    • Update the connection string in the config.yml file or as an environment variable.
  4. Run the server:

    go run main.go

API Endpoints

Auth Endpoints

Method Endpoint Description Auth Required
POST /signup User Signup No
POST /login User Login (JWT Token) No

Expense Endpoints

Method Endpoint Description Auth Required
POST /expenseAPI/expense Add a new expense Yes
GET /expenseAPI/expense Get all expenses Yes
GET /expenseAPI/expense/:id Get expense by ID Yes
PUT /expenseAPI/expense/:id Update expense by ID Yes
DELETE /expenseAPI/expense/:id Delete expense by ID Yes

Environment Variables

Variable Description
PORT Server listening port (default: 8080)
DATABASE_URL Connection string for the database
JWT_SECRET Secret key for signing JWT tokens

Run Locally

  1. Start the server:

    go run main.go
  2. Access the API: Visit http://localhost:8080 to access the API.


Testing with cURL

1. Sign Up

curl -X POST http://localhost:8080/signup \
-H "Content-Type: application/json" \
-d '{"username": "testuser", "password": "testpassword"}'

2. Log In

curl -X POST http://localhost:8080/login \
-H "Content-Type: application/json" \
-d '{"username": "testuser", "password": "testpassword"}'

3. Add an Expense

curl -X POST http://localhost:8080/expenseAPI/expense \
-H "Authorization: Bearer <JWT_TOKEN>" \
-H "Content-Type: application/json" \
-d '{"category": "Groceries", "amount": 50.75, "description": "Lunch", "date": "2024-11-13"}'

4. Get All Expenses

curl -X GET http://localhost:8080/expenseAPI/expense \
-H "Authorization: Bearer <JWT_TOKEN>"

License

This project is licensed under the MIT License.

Feel free to contribute to this project or use it as a reference for your own projects! 🚀

About

The Expense Tracker API is a RESTful service built with the Gin framework in Go

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published