Skip to content

Instantly share code, notes, and snippets.

@84adam
Created October 16, 2022 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 84adam/f25e30df7ffb9937c1ced9f0d9133d72 to your computer and use it in GitHub Desktop.
Save 84adam/f25e30df7ffb9937c1ced9f0d9133d72 to your computer and use it in GitHub Desktop.
Simple Go Website
// a simple Go-based website
// based on: https://www.digitalocean.com/community/tutorials/how-to-make-an-http-server-in-go
// with 100 bytes of CSS from: https://gist.github.com/JoeyBurzynski/617fb6201335779f8424ad9528b72c41
package main
import (
"errors"
"fmt"
"io"
"net/http"
"os"
)
func getRoot(w http.ResponseWriter, r *http.Request) {
fmt.Printf("got '/' request: \n--- %s ---\n", r)
homeString := "<html><head><style>" +
"html {max-width:70ch;padding:3em 1em;margin:auto;line-height:1.75;font-size:1.25em;}" +
"</style></head><body>" +
"<h1>Simple Go Website</h1>" +
"<h2>Welcome!</h2>" +
"<a href='/hello'>Hello</a>" +
"</body></html>"
io.WriteString(w, homeString)
}
func getHello(w http.ResponseWriter, r *http.Request) {
fmt.Printf("got '/hello' request: \n--- %s ---\n", r)
helloString := "<html><head><style>" +
"html {max-width:70ch;padding:3em 1em;margin:auto;line-height:1.75;font-size:1.25em;}" +
"</style></head><body>" +
"<h1>Simple Go Website</h1>" +
"<h2>Hello, HTTP!</h2>" +
"<a href='/'>Home</a>" +
"</body></html>"
io.WriteString(w, helloString)
}
func main() {
http.HandleFunc("/", getRoot)
http.HandleFunc("/hello", getHello)
err := http.ListenAndServe(":3333", nil)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("server closed\n")
} else if err != nil {
fmt.Printf("error starting server: %s\n", err)
os.Exit(1)
}
}
@84adam
Copy link
Author

84adam commented Oct 16, 2022

example of requests printed to console:

got '/' request: 
--- &{GET / HTTP/1.1 %!s(int=1) %!s(int=1) map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8] Accept-Encoding:[gzip, deflate, br] Accept-Language:[en-US,en;q=0.5] Connection:[keep-alive] Cookie:[csrftoken=Omj...2H6; sessionid=9w1...o5u] Referer:[http://127.0.0.1:3333/hello] Sec-Fetch-Dest:[document] Sec-Fetch-Mode:[navigate] Sec-Fetch-Site:[same-origin] Sec-Fetch-User:[?1] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (X11; Linux x86_64; rv:....0) Gecko/... Firefox/....0]] {} %!s(func() (io.ReadCloser, error)=<nil>) %!s(int64=0) [] %!s(bool=false) 127.0.0.1:3333 map[] map[] %!s(*multipart.Form=<nil>) map[] 127.0.0.1:40474 / %!s(*tls.ConnectionState=<nil>) %!s(<-chan struct {}=<nil>) %!s(*http.Response=<nil>) %!s(*context.cancelCtx=&{0xc00007c240 {0 0} {<nil>} map[] <nil>})} ---
got '/hello' request: 
--- &{GET /hello HTTP/1.1 %!s(int=1) %!s(int=1) map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8] Accept-Encoding:[gzip, deflate, br] Accept-Language:[en-US,en;q=0.5] Connection:[keep-alive] Cookie:[csrftoken=Omj...2H6; sessionid=9w1...o5u] Referer:[http://127.0.0.1:3333/] Sec-Fetch-Dest:[document] Sec-Fetch-Mode:[navigate] Sec-Fetch-Site:[same-origin] Sec-Fetch-User:[?1] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (X11; Linux x86_64; rv:....0) Gecko/... Firefox/....0]] {} %!s(func() (io.ReadCloser, error)=<nil>) %!s(int64=0) [] %!s(bool=false) 127.0.0.1:3333 map[] map[] %!s(*multipart.Form=<nil>) map[] 127.0.0.1:40474 /hello %!s(*tls.ConnectionState=<nil>) %!s(<-chan struct {}=<nil>) %!s(*http.Response=<nil>) %!s(*context.cancelCtx=&{0xc00007c240 {0 0} {<nil>} map[] <nil>})} ---

home:
sgw-home

hello:
sgw-hello

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment