refakt
This commit is contained in:
+16
-15
@@ -2,31 +2,34 @@ package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template" // Only import if template parsing is done here
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"teletype_softwares/lib/template_utils"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type PlayController struct{}
|
||||
|
||||
// PlayGame renders the play.html template, which embeds the game content in an iframe.
|
||||
// Templates are parsed on each request, mirroring the original SoftwareController's approach.
|
||||
func (c *PlayController) PlayGame(w http.ResponseWriter, r *http.Request) {
|
||||
func NewPlayController() *PlayController {
|
||||
return &PlayController{}
|
||||
}
|
||||
|
||||
func (c *PlayController) Play(w http.ResponseWriter, r *http.Request) {
|
||||
name := chi.URLParam(r, "name")
|
||||
if name == "" {
|
||||
http.Error(w, "Game name not provided", http.StatusBadRequest)
|
||||
http.Error(w, "Name not provided", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse templates on each request
|
||||
tmpl, err := template.ParseFiles("http/views/layouts/main.html", "http/views/play.html")
|
||||
tmpl, err := template_utils.GetTemplate("play", "http/views/layouts/main.html", "http/views/play.html")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
data := struct {
|
||||
Name string
|
||||
}{
|
||||
@@ -36,23 +39,21 @@ func (c *PlayController) PlayGame(w http.ResponseWriter, r *http.Request) {
|
||||
tmpl.Execute(w, data)
|
||||
}
|
||||
|
||||
// ServeGameContent serves the static files for a game from the html/$NAME/ directory.
|
||||
func (c *PlayController) ServeGameContent(w http.ResponseWriter, r *http.Request) {
|
||||
gamePath := os.Getenv("GAMES_DIR")
|
||||
func (c *PlayController) ServeContent(w http.ResponseWriter, r *http.Request) {
|
||||
contentsPath := os.Getenv("CONTENTS_DIR")
|
||||
name := chi.URLParam(r, "name")
|
||||
if name == "" {
|
||||
http.Error(w, "Game name not provided", http.StatusBadRequest)
|
||||
http.Error(w, "Name not provided", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
htmlBaseDir := filepath.Join(gamePath, "html", name)
|
||||
htmlBaseDir := filepath.Join(contentsPath, "html", name)
|
||||
|
||||
// Check if the directory exists and is accessible
|
||||
if _, err := os.Stat(htmlBaseDir); os.IsNotExist(err) {
|
||||
http.Error(w, fmt.Sprintf("HTML content for game '%s' not found.", name), http.StatusNotFound)
|
||||
http.Error(w, fmt.Sprintf("Content for '%s' not found.", name), http.StatusNotFound)
|
||||
return
|
||||
} else if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error accessing HTML content for game '%s': %v", name, err), http.StatusInternalServerError)
|
||||
http.Error(w, fmt.Sprintf("Error accessing content for '%s': %v", name, err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user