Skip to content

Commit b3c4146

Browse files
committed
docs: added usage example in the readme
1 parent 5a5cf8e commit b3c4146

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

README.md

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
A minimal HTTP server implemented using Go’s net/http package. Implemented to understand how handlers, middleware, concurrency, and context propagation work in Go.
77

8-
## Features (in the current stage)
8+
## Features
99
### Endpoints:
10-
- **`/`**: endpoint that is protected by an authentication middleware. It performs no task other than printing a message.
10+
- **`/health`**: Returns OK if server is running.
1111

1212
- **`/stats`**: endpoint that returns the request metrics like total requests, successful requests and Unauthorized requests in JSON.
1313

@@ -16,8 +16,10 @@ For example `http://localhost:4000/work?limit=1000` will compute the first 1000
1616

1717
- **`/upload`**: Recieves the file in the POST request form field. Creates a temporary file with a unique name in the `./uploads` directory using `os.CreateTemp` and copies the file to it using `io.Copy()`. Renames the temp file using os.Rename() with a safe name created by a helper method in the format `./uploads/uuid_originalFileName.extension`. `uuid` is a unique ID generated by `google/uuid` to maintain unique file names. Performs clean up when the function returns.
1818

19+
- **`/download`**: Recieves the file name from the incoming request's `file` query. Checks if the file exists, using `os.Stat()`. If the file exists, it uses `http.ServeFile()` to send the file the client.
20+
1921
### Middlewares:
20-
- **`AuthMiddleware()`**: protects the endpoints and returns 401 if the Basic Auth credentials are invalid.
22+
- **`AuthMiddleware()`**: protects the endpoints and returns 401 if the Basic Auth credentials are invalid. The password has been hardcoded because this is just a practice/learning project and I wanted to focus on other important concepts such as file uploading and downloading.
2123

2224
- **`LoggingMiddleware()`**: logs the request's method, path, timestamp and time taken to complete the request.
2325

@@ -42,5 +44,42 @@ For example `http://localhost:4000/work?limit=1000` will compute the first 1000
4244

4345
---
4446

45-
> # Important Note !
46-
> **This project is still under development. The README will be updated with new features and endpoints in the coming days.**
47+
## Usage example
48+
- **Clone Repository**
49+
```bash
50+
git clone https://github.com/aDiThYa-808/golang-http-server.git
51+
```
52+
- **Start server**
53+
```bash
54+
go run ./cmd/server
55+
```
56+
57+
- **Upload file**
58+
```bash
59+
curl -X POST http://localhost:4000/upload -F "file=@example.pdf"
60+
```
61+
62+
- **Download file**
63+
```bash
64+
curl -o output.pdf http://localhost:4000/download?file=example.pdf
65+
```
66+
67+
- **View metrics**
68+
```bash
69+
curl -u admin:pass http://localhost:4000/stats
70+
```
71+
> Note: the password is hardcoded only because this project was made for learning purpose.
72+
73+
- **Simulate work(compute prime numbers)**
74+
```bash
75+
curl http://localhost:4000/work?limit=50000
76+
```
77+
78+
- **Health check**
79+
```bash
80+
curl -u admin:pass http://localhost:4000/health
81+
```
82+
83+
---
84+
85+
# Thank you

0 commit comments

Comments
 (0)