Quantcast
Channel: Recent Gists from 84adam
Viewing all articles
Browse latest Browse all 34

Simple Go Website

$
0
0
main.go
// 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"
)
funcgetRoot(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)
}
funcgetHello(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)
}
funcmain() {
http.HandleFunc("/", getRoot)
http.HandleFunc("/hello", getHello)
err:=http.ListenAndServe(":3333", nil)
iferrors.Is(err, http.ErrServerClosed) {
fmt.Printf("server closed\n")
} elseiferr!=nil {
fmt.Printf("error starting server: %s\n", err)
os.Exit(1)
}
}

Viewing all articles
Browse latest Browse all 34

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>