84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"text/template"
|
|
)
|
|
|
|
func Game(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
|
|
tmpl, err := template.ParseFiles("templates/D.html")
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("Error parsing template: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = tmpl.Execute(w, nil)
|
|
if err != nil {
|
|
http.Error(w, fmt.Sprintf("Error executing template: %v", err), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
|
|
func StaticFileHandler(w http.ResponseWriter, r *http.Request) {
|
|
path := strings.TrimPrefix(r.URL.Path, "/")
|
|
templatesPath := filepath.Join("templates", path)
|
|
|
|
if fileExists(templatesPath) {
|
|
http.ServeFile(w, r, templatesPath)
|
|
return
|
|
}
|
|
|
|
http.NotFound(w, r)
|
|
}
|
|
|
|
func fileExists(path string) bool {
|
|
info, err := os.Stat(path)
|
|
if os.IsNotExist(err) {
|
|
return false
|
|
}
|
|
return !info.IsDir()
|
|
}
|
|
|
|
func main() {
|
|
http.HandleFunc("/", Game)
|
|
|
|
http.HandleFunc("/D.icon.png", StaticFileHandler)
|
|
http.HandleFunc("/D.apple-touch-icon.png", StaticFileHandler)
|
|
http.HandleFunc("/D.png", StaticFileHandler)
|
|
http.HandleFunc("/D.js", StaticFileHandler)
|
|
http.HandleFunc("/D.pck", StaticFileHandler)
|
|
http.HandleFunc("/D.wasm", StaticFileHandler)
|
|
http.HandleFunc("/D.audio.position.worklet.js", StaticFileHandler)
|
|
http.HandleFunc("/D.audio.worklet.js", StaticFileHandler)
|
|
|
|
http.HandleFunc("/templates/", func(w http.ResponseWriter, r *http.Request) {
|
|
path := strings.TrimPrefix(r.URL.Path, "/templates/")
|
|
templatesPath := filepath.Join("templates", path)
|
|
|
|
if fileExists(templatesPath) {
|
|
http.ServeFile(w, r, templatesPath)
|
|
return
|
|
}
|
|
|
|
http.NotFound(w, r)
|
|
})
|
|
|
|
port := ":8181"
|
|
fmt.Printf("Starting Go server on http://localhost%s\n", port)
|
|
fmt.Printf("Open http://localhost%s in your browser\n", port)
|
|
fmt.Printf("Serving files directly from templates directory\n")
|
|
|
|
if err := http.ListenAndServe(port, nil); err != nil {
|
|
fmt.Printf("Server error: %v\n", err)
|
|
}
|
|
}
|