Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
http-echo
=========
# http-echo

HTTP Echo is a small go web server that serves the contents it was started with
as an HTML page.

The default port is 5678, but this is configurable via the `-listen` flag:

```
```bash
http-echo -listen=:8080 -text="hello world"
```

Then visit http://localhost:8080/ in your browser.
Then visit <http://localhost:8080/> in your browser.

## Configuration

| Argument | Required | Default | Description |
| -------------- | --------- | --------------------------- | ----------------------------------- |
| `text` | ✅ | | Text to put on the response |
| `listen` | ❌ | `:5678` | Address and port to listen |
| `content-type` | ❌ | `text/plain; charset=utf-8` | The Content-Type header of response |
| `status-code` | ❌ | 200 | HTTP response code, e.g.: 200 |
10 changes: 6 additions & 4 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ import (
)

const (
httpHeaderAppName string = "X-App-Name"
httpHeaderAppVersion string = "X-App-Version"
httpHeaderAppName string = "X-App-Name"
httpHeaderAppVersion string = "X-App-Version"
httpHeaderContentType string = "Content-Type"

httpLogDateFormat string = "2006/01/02 15:04:05"
httpLogFormat string = "%v %s %s \"%s %s %s\" %d %d \"%s\" %v\n"
)

// withAppHeaders adds application headers such as X-App-Version and X-App-Name.
func withAppHeaders(c int, h http.HandlerFunc) http.HandlerFunc {
// withAppHeaders adds application headers such as X-App-Version, X-App-Name and Content-Type.
func withAppHeaders(c int, h http.HandlerFunc, t string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(httpHeaderAppName, version.Name)
w.Header().Set(httpHeaderAppVersion, version.Version)
w.Header().Set(httpHeaderContentType, t)
w.WriteHeader(c)
h(w, r)
}
Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (
)

var (
listenFlag = flag.String("listen", ":5678", "address and port to listen")
textFlag = flag.String("text", "", "text to put on the webpage")
versionFlag = flag.Bool("version", false, "display version information")
statusFlag = flag.Int("status-code", 200, "http response code, e.g.: 200")
contentTypeFlag = flag.String("content-type", "text/plain; charset=utf-8", "the Content-Type header of response")
listenFlag = flag.String("listen", ":5678", "address and port to listen")
textFlag = flag.String("text", "", "text to put on the webpage")
versionFlag = flag.Bool("version", false, "display version information")
statusFlag = flag.Int("status-code", 200, "http response code, e.g.: 200")

// stdoutW and stderrW are for overriding in test.
stdoutW = os.Stdout
Expand Down Expand Up @@ -57,10 +58,10 @@ func main() {

// Flag gets printed as a page
mux := http.NewServeMux()
mux.HandleFunc("/", httpLog(stdoutW, withAppHeaders(*statusFlag, httpEcho(echoText))))
mux.HandleFunc("/", httpLog(stdoutW, withAppHeaders(*statusFlag, httpEcho(echoText), *contentTypeFlag)))

// Health endpoint
mux.HandleFunc("/health", withAppHeaders(200, httpHealth()))
mux.HandleFunc("/health", withAppHeaders(200, httpHealth(), *contentTypeFlag))

server := &http.Server{
Addr: *listenFlag,
Expand Down