Skip to content

Instantly share code, notes, and snippets.

@linuxoid69
Created December 28, 2021 20:22
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 linuxoid69/827c27261c398bcefdc1cb638543c6a2 to your computer and use it in GitHub Desktop.
Save linuxoid69/827c27261c398bcefdc1cb638543c6a2 to your computer and use it in GitHub Desktop.
web server with embed content
package main
import (
"embed"
"log"
"net/http"
)
//go:embed static/*
var content embed.FS
//go:embed templates/index.html
var index embed.FS
func main() {
http.HandleFunc("/", indexPage)
http.Handle("/static/", http.FileServer(http.FS(content)))
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Printf("%v", err)
return
}
}
func indexPage(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html")
f, _ := index.ReadFile("templates/index.html")
_, err := w.Write(f)
if err != nil {
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment