61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"html/template"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
var (
|
|
listenPort string = "1302"
|
|
|
|
pages = map[string]string{
|
|
"/": "assets/index.html",
|
|
}
|
|
)
|
|
|
|
//go:embed assets
|
|
var assetsFolder embed.FS
|
|
|
|
func getRoot(w http.ResponseWriter, r *http.Request) {
|
|
page, ok := pages[r.URL.Path]
|
|
if !ok {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
tpl, err := template.ParseFS(assetsFolder, page)
|
|
if err != nil {
|
|
log.Printf("Error occurred parsing template: %v\n", err)
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.WriteHeader(http.StatusOK)
|
|
data := map[string]interface{}{
|
|
"userAgent": r.UserAgent(),
|
|
}
|
|
if err := tpl.Execute(w, data); err != nil {
|
|
log.Printf("Great: %v\n", err)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
serverRoot, err := fs.Sub(assetsFolder, "assets")
|
|
if err != nil {
|
|
log.Fatalf("Error occurred setting HTML root: %v\n", err)
|
|
}
|
|
http.FileServer(http.FS(serverRoot))
|
|
|
|
http.HandleFunc("/", getRoot)
|
|
|
|
fmt.Printf("Listening on port %v...\n", listenPort)
|
|
|
|
err = http.ListenAndServe(":"+listenPort, nil)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|