This kept growing and I wanted to add some pictures, so I moved it to a blog post:
Last active
March 26, 2024 10:51
-
-
Save bmcculley/055518d703899ec3a91f7aae732b04d2 to your computer and use it in GitHub Desktop.
A Go embed example of serving static files at the root URL path.
This file contains hidden or 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" | |
"fmt" | |
"io/fs" | |
"log" | |
"net/http" | |
"os" | |
) | |
//go:embed public | |
var staticFiles embed.FS | |
func main() { | |
var staticFS = fs.FS(staticFiles) | |
htmlContent, err := fs.Sub(staticFS, "public") | |
if err != nil { | |
log.Fatal(err) | |
} | |
fs := http.FileServer(http.FS(htmlContent)) | |
// Serve static files | |
http.Handle("/", fs) | |
port := os.Getenv("PORT") | |
if port == "" { | |
port = "3000" | |
} | |
log.Printf("Listening on :%s...\n", port) | |
err = http.ListenAndServe(fmt.Sprintf(":%s", port), nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
This file contains hidden or 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
<!doctype html> | |
<!-- public/index.html --> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Go embed example</title> | |
<meta name="description" content="Go embed example"> | |
<meta name="author" content="bmcculley"> | |
<link rel="stylesheet" href="css/style.css"> | |
</head> | |
<body> | |
<p>A Go <a href="https://golang.org/pkg/embed/">embed</a> example of serving static files at the root URL path.</p> | |
</body> | |
</html> |
This file contains hidden or 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" | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
"strings" | |
) | |
//go:embed public | |
var staticFiles embed.FS | |
var staticDir = "public" | |
func rootPath(h http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
// add header(s) | |
w.Header().Set("Cache-Control", "no-cache") | |
if r.URL.Path == "/" { | |
r.URL.Path = fmt.Sprintf("/%s/", staticDir) | |
} else { | |
b := strings.Split(r.URL.Path, "/")[0] | |
if b != staticDir { | |
r.URL.Path = fmt.Sprintf("/%s%s", staticDir, r.URL.Path) | |
} | |
} | |
h.ServeHTTP(w, r) | |
}) | |
} | |
func main() { | |
var staticFS = http.FS(staticFiles) | |
fs := rootPath(http.FileServer(staticFS)) | |
// Serve static files | |
http.Handle("/", fs) | |
port := os.Getenv("PORT") | |
if port == "" { | |
port = "3000" | |
} | |
log.Printf("Listening on :%s...\n", port) | |
err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
This file contains hidden or 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
/* public/css/style.css */ | |
body { | |
margin: 20px; | |
font-family: sans-serif; | |
font-size: 1rem; | |
font-weight: 400; | |
line-height: 1.5; | |
color: #212529; | |
background-color: #fff; | |
-webkit-text-size-adjust: 100%; | |
-webkit-tap-highlight-color: rgba(0, 0, 0, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment