summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2022-04-21 11:05:38 -0600
committermo khan <mo@mokhan.ca>2022-04-21 11:05:38 -0600
commited2a4c435a23b5bd3ee53803eefc22f1a2a1c3a7 (patch)
tree9a8491ad6710471230280914b363119fdf41446e
parentdbfc15c512b852306bacc6aa7c487132050196d0 (diff)
add some notes on golang httpHEADmain
-rw-r--r--learn/golang/README.md49
1 files changed, 49 insertions, 0 deletions
diff --git a/learn/golang/README.md b/learn/golang/README.md
index 6561585..59aa2b7 100644
--- a/learn/golang/README.md
+++ b/learn/golang/README.md
@@ -84,3 +84,52 @@ The `go.mod` file has 3 responsibilities:
1. Specifies the desired version of golang.
1. Specifies other modules that this module depends on.
* this is represented as both direct and indirect dependencies.
+
+## HTTP
+
+Go makes it very easy to create an http(s) server and provides a default
+mechanism for routing requests to specific `handler` functions. The default
+router is called a `Mux` which is short for multiplexer. The `ServeMux` struct
+is described as a `HTTP request multiplexer`.
+
+```golang
+// ServeMux is an HTTP request multiplexer.
+// It matches the URL of each incoming request against a list of registered
+// patterns and calls the handler for the pattern that
+// most closely matches the URL.
+```
+
+The interface used by the `mux` is `Handler` which is defined as:
+
+```golang
+// A Handler responds to an HTTP request.
+//
+// ServeHTTP should write reply headers and data to the ResponseWriter
+// and then return. Returning signals that the request is finished; it
+// is not valid to use the ResponseWriter or read from the
+// Request.Body after or concurrently with the completion of the
+// ServeHTTP call.
+//
+// Depending on the HTTP client software, HTTP protocol version, and
+// any intermediaries between the client and the Go server, it may not
+// be possible to read from the Request.Body after writing to the
+// ResponseWriter. Cautious handlers should read the Request.Body
+// first, and then reply.
+//
+// Except for reading the body, handlers should not modify the
+// provided Request.
+//
+// If ServeHTTP panics, the server (the caller of ServeHTTP) assumes
+// that the effect of the panic was isolated to the active request.
+// It recovers the panic, logs a stack trace to the server error log,
+// and either closes the network connection or sends an HTTP/2
+// RST_STREAM, depending on the HTTP protocol. To abort a handler so
+// the client sees an interrupted response but the server doesn't log
+// an error, panic with the value ErrAbortHandler.
+type Handler interface {
+ ServeHTTP(ResponseWriter, *Request)
+}
+```
+
+This means that any `type` that implements the function `ServeHTTP(w, *r)` can
+be used as a `Handler` for the HTTP Mux.