Skip to content

Commit

Permalink
chore: documentation updated
Browse files Browse the repository at this point in the history
  • Loading branch information
rellyson committed Jan 23, 2025
1 parent 6bc0e85 commit 04f9ea7
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 15 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [0.2.0] - 2025-01-23

### Added

Application handlers
Application middlewares
Utils added

## [0.1.0] - 2025-01-22

Initial project release
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
<div align="center">
<img src="./assets/echo-logo.jpg" alt="Echo Logo" width="200">
<h2>HTTP Echo</h2>
<p>A simple HTTP server that echoes back the request headers and body.</p>
<p>A simple HTTP server that echoes back request information.</p>

![license](https://img.shields.io/badge/license-MIT-green.svg)
![go](https://img.shields.io/badge/golang-1.23-blue.svg?logo=go)
[![gh-stars](https://img.shields.io/github/stars/rellyson/http-echo?logo=github)](https://github.com/rellyson/http-echo/stargazers)
[![docker-pulls](https://img.shields.io/docker/pulls/rellyson/http-echo.svg?logo=docker)](https://hub.docker.com/repository/docker/rellyson/http-echo)
</div>

This project is a simple HTTP server that echoes back request information. It is useful
for testing and debugging deployments and networking configurations by providing a simple
interface to inspect requests.

## Features

The HTTP Echo server provides the following features:

- Accepts HTTP methods `GET`, `POST`, `PUT`, `PATCH` and `DELETE`.
- Echoes back request information in `json` format:
- **host**: `hostname` and `ip`
- **headers**: request headers
- **queries**: query parameters
- **params**: path parameters
- **body**: request body
- Returns a custom status code for a request (provided via query) - **TBD**.
- Exports metrics in `prometheus` format for monitoring.

## Usage

The server accepts the following parameters:

- `-listen`: the address to listen on (default: `:3000`)
- `-metrics`: the path to expose metrics on (default: `/metrics`)

## Running the server

You can run the server using via the following methods:

### Local using `go`

Yuu can run the server locally using the following command:

```bash
# execute via go run
$ go run cmd/server/main.go

# or build and execute
$ go build -o htt-echo cmd/server/main.go
$ ./http-echo
```

### Docker

There is also a [docker image](https://hub.docker.com/repository/docker/rellyson/http-echo)
available on _Docker Hub_.

```bash
# run using docker
$ docker run -p 3000:3000 rellyson/http-echo:latest
```

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.2.0
4 changes: 3 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
Expand All @@ -17,6 +18,7 @@ import (

var (
listenFlag = flag.String("listen", ":3000", "Address to listen on")
metricsPathFlag = flag.String("metrics", "/metrics", "Path to expose metrics")
defaultReadTimeout = 3 * time.Second
defaultWriteTimeout = 3 * time.Second
)
Expand All @@ -25,7 +27,7 @@ func main() {
flag.Parse()

mux := http.NewServeMux()
mux.Handle("GET /metrics", handlers.Metrics())
mux.Handle(fmt.Sprintf("GET %s", *metricsPathFlag), handlers.Metrics())
mux.HandleFunc("GET /health", handlers.HealthCheck)
mux.HandleFunc("/", handlers.Echo)

Expand Down
16 changes: 8 additions & 8 deletions internal/handlers/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ type HostInfoResponse struct {

// HttpInfo contains the HTTP information.
type HttpInfoResponse struct {
Headers map[string]string `json:"headers"` // request headers
Queries map[string]string `json:"queries,omitempty"` // query parameters
PathParams []string `json:"pathParams,omitempty"` // path parameters
Body interface{} `json:"body,omitempty"` // request body
Headers map[string]string `json:"headers"` // request headers
Queries map[string]string `json:"queries,omitempty"` // query parameters
Params []string `json:"params,omitempty"` // path parameters
Body interface{} `json:"body,omitempty"` // request body
}

// Handles the echo request.
Expand All @@ -54,10 +54,10 @@ func Echo(w http.ResponseWriter, r *http.Request) {
IP: hostinfo.IP.String(),
},
HttpInfo: HttpInfoResponse{
Headers: mapHeaders(r.Header),
Queries: mapQuery(r.URL.Query()),
PathParams: mapPathParams(r.URL.Path),
Body: mapBody(r.Body),
Headers: mapHeaders(r.Header),
Queries: mapQuery(r.URL.Query()),
Params: mapPathParams(r.URL.Path),
Body: mapBody(r.Body),
},
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/netutils/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ func isPrivateIP(ip net.IP) bool {
var privateIPBlocks []*net.IPNet

for _, cidr := range []string{
"127.0.0.0/8", // IPv4 loopback
"::1/128", // IPv6 loopback
"fe80::/10", // IPv6 link-local
//"127.0.0.0/8", // IPv4 loopback
//"::1/128", // IPv6 loopback
//"fe80::/10", // IPv6 link-local
"10.0.0.0/8", // RFC1918
"172.16.0.0/12", // RFC1918
"192.168.0.0/16", // RFC1918
Expand Down

0 comments on commit 04f9ea7

Please sign in to comment.