43 lines
906 B
Go
43 lines
906 B
Go
package frontendhandler
|
|
|
|
import (
|
|
"embed"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
//go:embed templates
|
|
var templatesFs embed.FS
|
|
|
|
//go:embed templates/static
|
|
var staticFiles embed.FS
|
|
|
|
func StartStaticFileServer() {
|
|
staticFS, err := fs.Sub(staticFiles, "templates/static")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
http.HandleFunc("/", IndexHandler)
|
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))))
|
|
|
|
err = http.ListenAndServe(":8000", nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
|
fileContent, err := templatesFs.ReadFile("templates/index.html")
|
|
if err != nil {
|
|
log.Printf("Error Reading index.html %v", err)
|
|
http.Error(w, "Could not read index.html", 500)
|
|
}
|
|
_, err = w.Write(fileContent)
|
|
if err != nil {
|
|
log.Printf("Could not Write to ReponseWrite %v", err)
|
|
http.Error(w, "Could not write data", 500)
|
|
}
|
|
}
|