Skip to content

Instantly share code, notes, and snippets.

@nexus166
Created July 29, 2019 10:24
Show Gist options
  • Select an option

  • Save nexus166/823fac02e97f7fcae2a2b4168c758ba0 to your computer and use it in GitHub Desktop.

Select an option

Save nexus166/823fac02e97f7fcae2a2b4168c758ba0 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"net/http"
"strconv"
)
const (
HTMLStyle = `<style>*{line-height:1.2;margin:0;}html{color:#888;display:table;font-family:sans-serif;height:100%;text-align:center;width:100%;}body{display:table-cell;vertical-align:middle;margin:2em auto;}h1{color:#555;font-size:2em;font-weight:400;}p{margin:0 auto;width:280px;}@media only screen and (max-width: 280px){body,p{width:95%;}h1{font-size:1.5em;margin:0 0 0.3em;}}</style>`
)
func SimpleMessageHTML(t, h1, p string) string {
return `
<!doctype html><html lang="en"><head><meta charset="utf-8"><title>` + t + `</title><meta name="viewport" content="width=device-width, initial-scale=1">` + HTMLStyle + `</head>
<body><h1>` + h1 + `</h1>
<p>` + p + `</p></body></html>
`
}
func HTTPHandler(w http.ResponseWriter, s string, c int) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(c)
w.Header().Set("Content-Length", strconv.Itoa(len(s)))
fmt.Fprint(w, s)
}
func handler(w http.ResponseWriter, r *http.Request) {
HTTPHandler(w, SimpleMessageHTML("page title", "title", "text"), http.StatusOK)
fmt.Println("got req to " + r.RemoteAddr + " for " + r.RequestURI)
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
@nexus166
Copy link
Copy Markdown
Author

this is just an experiment, move along

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