Created
December 28, 2021 20:22
-
-
Save linuxoid69/827c27261c398bcefdc1cb638543c6a2 to your computer and use it in GitHub Desktop.
web server with embed content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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