Skip to content

[Suggestion] Embed docs into the binary #15

@fabricionaweb

Description

@fabricionaweb

Hi, would it a good idea to embed the docs into the build/binary?
This way it would produce a single binary that does not need/depends on the "docs" static files

--- a/main.go
+++ b/main.go
@@ -2,7 +2,9 @@ package main
 
 import (
 	"crypto/tls"
+	"embed"
 	"fmt"
+	"io/fs"
 	"log"
 	"net"
 	"net/http"
@@ -13,6 +15,9 @@ import (
 	"golang.org/x/net/publicsuffix"
 )
 
+//go:embed docs
+var embedDocs embed.FS
+
 func main() {
 	port := os.Getenv("PORT")
 	if port == "" {
@@ -30,8 +35,9 @@ func main() {
 }
 
 func router(middlewares ...Middleware) http.Handler {
+	docsRoot, _ := fs.Sub(embedDocs, "docs")
 	mux := http.NewServeMux()
-	mux.Handle("/", http.FileServer(http.Dir("docs")))
+	mux.Handle("/", http.FileServer(http.FS(docsRoot)))
 	mux.HandleFunc("GET /{room}/announce", announce)
 	mux.HandleFunc("GET /{room}/scrape", scrape)
 	return chainMiddleware(mux, middlewares...)

Just a suggestion that I think makes it easy to run and to package the project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions