https://github.com/dracuxan/StocksAPI
How did I make API using Golang PostgreSQL?
https://github.com/dracuxan/StocksAPI
api go golang middleware postgresql restful restful-api
Last synced: about 1 year ago
JSON representation
How did I make API using Golang PostgreSQL?
- Host: GitHub
- URL: https://github.com/dracuxan/StocksAPI
- Owner: dracuxan
- License: bsd-3-clause
- Created: 2024-09-16T18:49:04.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-01T06:42:48.000Z (almost 2 years ago)
- Last Synced: 2025-03-23T06:57:48.578Z (over 1 year ago)
- Topics: api, go, golang, middleware, postgresql, restful, restful-api
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Stocks Management System
This project is a simple stocks management system using Go and PostgreSQL.
## Project Structure
```
.
├── .github # GitHub-related configuration files
├── cmd # Contains the main application code
│ ├── middleware # Middlewares and Request handlers for handling requests
│ │ ├── handlers.go
│ │ └── middleware.go
│ ├── models # Database models
│ │ └── models.go
│ ├── router # API routing
│ │ └── router.go
│ └── main.go # Application entry point
├── .env # Environment variables
├── .gitignore # Gitignore file
├── LICENSE # License file
├── Makefile # Makefile for running commands
├── README.md # Project README
├── go.mod # Go modules file
├── go.sum # Dependencies checksum
└── script.sql # SQL script to set up the database
```
## Prerequisites
- Go (version 1.16 or higher)
- PostgreSQL
- Environment variables specified in the `.env` file
## Installation and Setup
1. Clone the repository:
```
git clone https://github.com/Nisarg2061/StocksAPI.git && cd StocksAPI
```
2. Set up the database:
Make sure PostgreSQL is running locally. You can use the script.sql file to set up the database:
```
psql -h localhost -U postgres -d stocksdb -a -f script.sql
```
This will create the required tables and populate initial data into the stocksdb database.
3. Set up environment variables
Create a .env file in the root directory and set the required variables:
```
POSTGRES_URL=your_postgres_connection_url
```
Make sure to replace `your_postgres_connection_url` with your actual PostgreSQL connection string (e.g., `postgres://username:password@localhost:5432/stocksdb`).
4. Run the application
You can use the Makefile to build and run the project. To start the server, run:
```
make run
```
The application should now be running on the configured port.
## Usage
- The API routes are defined in router/router.go.
- Handlers for various routes are in handlers.go.
- Middleware logic is found in middleware/middleware.go.
- Database models are in models/models.go.