Files
teletypegames/http/controller.play.go
T
2026-01-29 20:47:18 +01:00

88 lines
2.4 KiB
Go

package http
import (
"fmt"
"net/http"
"os"
"teletype_softwares/domain"
"teletype_softwares/lib/template_utils"
"github.com/go-chi/chi/v5"
)
type PlayController struct {
softwareService domain.SoftwareServiceInterface
fileService domain.FileServiceInterface
}
func NewPlayController(software_service domain.SoftwareServiceInterface, file_service domain.FileServiceInterface) *PlayController {
return &PlayController{
softwareService: software_service,
fileService: file_service,
}
}
func (c *PlayController) Play(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
version := chi.URLParam(r, "version")
software, err := c.softwareService.GetByName(name)
if err != nil {
if err == domain.ErrSoftwareNotFound {
http.Error(w, "Software not found", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var web_playable_release *domain.Release
for _, release := range software.Releases {
if release.Version == version && release.HTMLFolderPath != "" {
web_playable_release = &release
break
}
}
if web_playable_release == nil {
http.Error(w, "No web-playable version found for this software", http.StatusNotFound)
return
}
softwares, err := c.softwareService.List()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl, err := template_utils.GetTemplate("play_controller_play", "http/views/shared/layout.html", "http/views/play/play.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl.Execute(w, map[string]interface{}{
"Software": software,
"WebPlayableRelease": web_playable_release,
"Softwares": softwares,
})
}
func (c *PlayController) ServeContent(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
version := chi.URLParam(r, "version")
html_base_dir := c.fileService.GetPath(name + "-" + version)
if _, err := os.Stat(html_base_dir); os.IsNotExist(err) {
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 content for '%s': %v", name, err), http.StatusInternalServerError)
return
}
fs := http.StripPrefix(fmt.Sprintf("/play/%s/%s/content", name, version), http.FileServer(http.Dir(html_base_dir)))
fs.ServeHTTP(w, r)
}