Skip to content

Instantly share code, notes, and snippets.

Created February 18, 2017 11:21
What would you like to do?
HTTP/2 Push example
// HTTP/2 Push example using violetear
// Create certificate with:
// go run generate_cert.go -host localhost, (will create cer.pem and key.pem)
// To test
// go run main.go and open browser using https://localhost:8000
package main
import (
const mainJS = `console.log("hello world");`
const indexHTML = `<html>
<script src="/main.js"></script>
func rootJS(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, mainJS)
func root(w http.ResponseWriter, r *http.Request) {
pusher, ok := w.(http.Pusher)
if ok {
if err := pusher.Push("/main.js", nil); err != nil {
log.Printf("Failed to push: %v", err)
fmt.Fprintf(w, indexHTML)
func main() {
router := violetear.New()
router.LogRequests = true
router.HandleFunc("/", root)
router.HandleFunc("main.js", rootJS)
log.Fatal(http.ListenAndServeTLS(":8000", "cert.pem", "key.pem", router))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment