new tma
This commit is contained in:
80
main.go
80
main.go
@@ -1,83 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"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)
|
||||
fs := http.FileServer(http.Dir("./dist"))
|
||||
http.Handle("/", fs)
|
||||
|
||||
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)
|
||||
}
|
||||
addr := ":8181"
|
||||
log.Println("Server started on", addr)
|
||||
log.Fatal(http.ListenAndServe(addr, nil))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user