This commit is contained in:
Smile Rex
2026-01-09 22:40:38 +03:00
commit 6e7faff43c
10 changed files with 1509 additions and 0 deletions

83
main.go Normal file
View File

@@ -0,0 +1,83 @@
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 := ":8080"
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)
}
}