@@ -9,9 +9,7 @@ Built as a [learning project](https://roadmap.sh/projects/blogging-platform-api)
99- [ Features] ( #features )
1010- [ Tech Stack] ( #tech-stack )
1111- [ Prerequisites] ( #prerequisites )
12- - [ Installation] ( #installation )
13- - [ Database Setup] ( #database-setup )
14- - [ Running the Application] ( #running-the-application )
12+ - [ Getting Started - Local Development Setup] ( #getting-started---local-development-setup )
1513- [ API Endpoints] ( #api-endpoints )
1614 - [ Create Blog Post] ( #create-blog-post )
1715 - [ Update Blog Post] ( #update-blog-post )
@@ -45,64 +43,183 @@ Built as a [learning project](https://roadmap.sh/projects/blogging-platform-api)
4543## Prerequisites
4644
4745- Go 1.24 or higher
48- - PostgreSQL database
46+ - PostgreSQL 12 or higher
4947- Git
5048
51- ## Installation
49+ ## Getting Started - Local Development Setup
50+
51+ Follow these steps to set up the project on your local machine:
52+
53+ ### Step 1: Install Prerequisites
54+
55+ #### Install Go
56+ - ** macOS:**
57+ ``` bash
58+ brew install go
59+ ```
60+ - ** Linux (Ubuntu/Debian):**
61+ ``` bash
62+ sudo apt update
63+ sudo apt install golang-go
64+ ```
65+ - ** Windows:** Download and install from [ golang.org/dl] ( https://golang.org/dl/ )
66+
67+ Verify installation:
68+ ``` bash
69+ go version
70+ ```
71+
72+ #### Install PostgreSQL
73+ - ** macOS:**
74+ ``` bash
75+ brew install postgresql@15
76+ brew services start postgresql@15
77+ ```
78+ - ** Linux (Ubuntu/Debian):**
79+ ``` bash
80+ sudo apt update
81+ sudo apt install postgresql postgresql-contrib
82+ sudo systemctl start postgresql
83+ sudo systemctl enable postgresql
84+ ```
85+ - ** Windows:** Download and install from [ postgresql.org/download] ( https://www.postgresql.org/download/ )
86+
87+ Verify installation:
88+ ``` bash
89+ psql --version
90+ ```
91+
92+ ### Step 2: Clone the Repository
5293
53- 1 . Clone the repository:
5494``` bash
5595git clone https://github.com/architxkumar/Blogging-Platform-API.git
5696cd Blogging-Platform-API
5797```
5898
59- 2 . Install dependencies:
99+ ### Step 3: Install Go Dependencies
100+
60101``` bash
61102go mod download
62103```
63104
64- 3 . Configure environment variables:
105+ ### Step 4: Set Up PostgreSQL Database
106+
107+ 1 . ** Create a PostgreSQL user (if needed):**
108+ ``` bash
109+ # On macOS/Linux, switch to postgres user
110+ sudo -u postgres psql
111+
112+ # Or connect directly
113+ psql postgres
114+ ```
115+
116+ Inside PostgreSQL prompt:
117+ ``` sql
118+ CREATE USER your_username WITH PASSWORD ' your_password' ;
119+ ALTER USER your_username CREATEDB;
120+ \q
121+ ```
122+
123+ 2 . ** Create the database:**
124+ ``` bash
125+ # Connect to PostgreSQL
126+ psql -U your_username -d postgres
127+ ```
128+
129+ Inside PostgreSQL prompt:
130+ ``` sql
131+ CREATE DATABASE blogs ;
132+ \q
133+ ```
134+
135+ 3 . ** Run the schema to create tables:**
136+ ``` bash
137+ psql -U your_username -d blogs -f internal/db/schema.sql
138+ ```
139+
140+ 4 . ** (Optional) Load sample data for testing:**
141+ ``` bash
142+ psql -U your_username -d blogs -f internal/db/sample_posts_insert.sql
143+ ```
144+
145+ ### Step 5: Configure Environment Variables
146+
147+ Create a ` .env.development ` file in the root directory of the project:
148+
149+ ``` bash
150+ # Create the file
151+ touch .env.development
152+ ```
153+
154+ Add the following configuration (update with your PostgreSQL credentials):
65155
66- Create or update the ` .env.development ` file in the root directory:
67156``` env
68157PGHOST=localhost
69158PGPORT=5432
70- PGUSER=your_postgres_user
71- PGPASSWORD=your_postgres_password
159+ PGUSER=your_username
160+ PGPASSWORD=your_password
72161PGSSLNEGOTIATION=disable
73162PGDATABASE=blogs
74163```
75164
76- ## Database Setup
77-
78- 1 . Create the PostgreSQL database:
79- ``` sql
80- CREATE DATABASE blogs ;
165+ ** Example:**
166+ ``` env
167+ PGHOST=localhost
168+ PGPORT=5432
169+ PGUSER=architxkumar
170+ PGPASSWORD=architxkumar
171+ PGSSLNEGOTIATION=disable
172+ PGDATABASE=blogs
81173```
82174
83- 2 . Run the schema to create the posts table:
175+ ### Step 6: Run the Application
176+
177+ Start the server:
84178``` bash
85- psql -U your_postgres_user -d blogs -f internal/db/schema.sql
179+ go run main.go
86180```
87181
88- The schema includes:
89- - ` posts ` table with auto-incrementing ID
90- - Automatic ` created_at ` and ` updated_at ` timestamp management
91- - Support for arrays (tags)
182+ You should see output indicating the server is running. The API will be available at ` http://localhost:8080 `
92183
93- 3 . (Optional) Load sample data:
94- ``` bash
95- psql -U your_postgres_user -d blogs -f internal/db/sample_posts_insert.sql
96- ```
184+ ### Step 7: Test the API
97185
98- ## Running the Application
186+ Test that the API is working by making a request:
99187
100- Start the server:
188+ ** Using curl: **
101189``` bash
102- go run main.go
190+ # Get all posts
191+ curl http://localhost:8080/posts
192+
193+ # Create a new post
194+ curl -X POST http://localhost:8080/posts \
195+ -H " Content-Type: application/json" \
196+ -d ' {
197+ "title": "My First Blog Post",
198+ "content": "This is the content of my first blog post.",
199+ "category": "Technology",
200+ "tags": ["Tech", "Programming"]
201+ }'
103202```
104203
105- The API will be available at ` http://localhost:8080 `
204+ ** Using a browser:**
205+ - Open your browser and navigate to ` http://localhost:8080/posts `
206+ - You should see a JSON response with all blog posts
207+
208+ ### Troubleshooting
209+
210+ ** Database connection errors:**
211+ - Verify PostgreSQL is running: ` pg_isready `
212+ - Check your credentials in ` .env.development `
213+ - Ensure the database ` blogs ` exists: ` psql -U your_username -l `
214+
215+ ** Port already in use:**
216+ - The application runs on port 8080 by default
217+ - Check if another application is using the port: ` lsof -i :8080 ` (macOS/Linux)
218+ - Kill the process or modify the port in ` main.go `
219+
220+ ** Go module errors:**
221+ - Delete ` go.sum ` and run ` go mod download ` again
222+ - Run ` go mod tidy ` to clean up dependencies
106223
107224## API Endpoints
108225
0 commit comments