“Not every man who bears the mark of the castaway…”

HTTP servers in Go

This example is for plain HTTP. You should use HTTPS, either in Go directly or by putting the Go server behind a reverse proxy.

import (
    "fmt"
    "log"
    "net/http"
)

func handleIndex(writer http.ResponseWriter, req *http.Request) {
    // req.URL.Query()
    // req.URL.Path
    // req.Header["Hostname"]
    // req.RemoteAddr

    // writer.WriteHeader(http.StatusInternalServerError)
    // writer.Header().Set("Content-Type", "application/json")
}

handler := &http.ServeMux{}
handler.HandleFunc("/", handleIndex)
addr := fmt.Sprintf(":%d", port)
server := &http.Server{
    ReadHeaderTimeout: readHeaderTimeout,
    ReadTimeout:       readTimeout,
    WriteTimeout:      writeTimeout,
    IdleTimeout:       idleTimeout,
    Addr:              addr,
    Handler:           handler,
}
log.Fatal("server failed", server.ListenAndServe())

Examples: