From 35f60e5a3460e82ba324e38c40ece2446f21b526 Mon Sep 17 00:00:00 2001 From: Arash Hatami Date: Sat, 7 Mar 2026 16:26:15 +0330 Subject: [PATCH 1/2] feat: support content-type --- handlers.go | 10 ++++++---- main.go | 13 +++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/handlers.go b/handlers.go index d8e6711..8a47053 100644 --- a/handlers.go +++ b/handlers.go @@ -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) } diff --git a/main.go b/main.go index 4707753..cf4439b 100644 --- a/main.go +++ b/main.go @@ -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 @@ -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, From 8336779ee78985e717ac555ebfee66bc0bbc3bbc Mon Sep 17 00:00:00 2001 From: Arash Hatami Date: Sat, 7 Mar 2026 16:28:25 +0330 Subject: [PATCH 2/2] docs: improve readme --- README.md | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 81be8ff..367a4ab 100644 --- a/README.md +++ b/README.md @@ -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 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 |