This commit is contained in:
2025-12-07 14:54:35 +01:00
parent ac6a4fd6d3
commit d18a72d492
18 changed files with 725 additions and 307 deletions
+39 -38
View File
@@ -12,10 +12,14 @@ import (
)
type DownloadController struct {
downloadService domain.DownloadService
service domain.DownloadServiceInterface
}
func (c *DownloadController) serveReleaseFile(w http.ResponseWriter, r *http.Request, release *domain.Release, isSource bool) {
func NewDownloadController(service domain.DownloadServiceInterface) *DownloadController {
return &DownloadController{service: service}
}
func (c *DownloadController) serve(w http.ResponseWriter, r *http.Request, release *domain.Release, isSource bool) {
var filePath string
if isSource {
filePath = release.SourcePath
@@ -23,18 +27,19 @@ func (c *DownloadController) serveReleaseFile(w http.ResponseWriter, r *http.Req
filePath = release.CartridgePath
}
// Security check: ensure the file is within the softwares directory
absFilePath, err := filepath.Abs(filePath)
if err != nil {
http.Error(w, "Invalid file path", http.StatusInternalServerError)
return
}
absSoftwaresDir, err := filepath.Abs(os.Getenv("GAMES_DIR"))
absContentsDir, err := filepath.Abs(os.Getenv("CONTENTS_DIR"))
if err != nil {
http.Error(w, "Invalid softwares directory path", http.StatusInternalServerError)
http.Error(w, "Invalid contents directory path", http.StatusInternalServerError)
return
}
if !strings.HasPrefix(absFilePath, absSoftwaresDir) {
if !strings.HasPrefix(absFilePath, absContentsDir) {
http.Error(w, "Access denied", http.StatusForbidden)
return
}
@@ -43,64 +48,60 @@ func (c *DownloadController) serveReleaseFile(w http.ResponseWriter, r *http.Req
http.ServeFile(w, r, filePath)
}
func (c *DownloadController) DownloadSource(w http.ResponseWriter, r *http.Request) {
func (c *DownloadController) handleError(w http.ResponseWriter, err error) {
if err == domain.ErrSoftwareNotFound || err == domain.ErrNoReleasesFound {
http.Error(w, "Software not found", http.StatusNotFound)
return
}
if err == domain.ErrReleaseNotFound {
http.Error(w, "Software or release not found", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
}
func (c *DownloadController) GetLatestSource(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
release, err := c.downloadService.GetLatestRelease(name)
release, err := c.service.GetLatestRelease(name)
if err != nil {
if err == domain.ErrSoftwareNotFound || err == domain.ErrNoReleasesFound {
http.Error(w, "Software not found", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
c.handleError(w, err)
return
}
c.serveReleaseFile(w, r, release, true)
c.serve(w, r, release, true)
}
func (c *DownloadController) DownloadCartridge(w http.ResponseWriter, r *http.Request) {
func (c *DownloadController) GetLatestCartridge(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
release, err := c.downloadService.GetLatestRelease(name)
release, err := c.service.GetLatestRelease(name)
if err != nil {
if err == domain.ErrSoftwareNotFound || err == domain.ErrNoReleasesFound {
http.Error(w, "Software not found", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
c.handleError(w, err)
return
}
c.serveReleaseFile(w, r, release, false)
c.serve(w, r, release, false)
}
func (c *DownloadController) DownloadSourceByVersion(w http.ResponseWriter, r *http.Request) {
func (c *DownloadController) GetSource(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
version := chi.URLParam(r, "version")
release, err := c.downloadService.GetSpecificRelease(name, version)
release, err := c.service.GetSpecificRelease(name, version)
if err != nil {
if err == domain.ErrSoftwareNotFound || err == domain.ErrReleaseNotFound {
http.Error(w, "Software or release not found", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
c.handleError(w, err)
return
}
c.serveReleaseFile(w, r, release, true)
c.serve(w, r, release, true)
}
func (c *DownloadController) DownloadCartridgeByVersion(w http.ResponseWriter, r *http.Request) {
func (c *DownloadController) GetCartridge(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
version := chi.URLParam(r, "version")
release, err := c.downloadService.GetSpecificRelease(name, version)
release, err := c.service.GetSpecificRelease(name, version)
if err != nil {
if err == domain.ErrSoftwareNotFound || err == domain.ErrReleaseNotFound {
http.Error(w, "Software or release not found", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
c.handleError(w, err)
return
}
c.serveReleaseFile(w, r, release, false)
c.serve(w, r, release, false)
}
+16 -15
View File
@@ -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
}
+10 -6
View File
@@ -1,26 +1,30 @@
package http
import (
"html/template"
"net/http"
"teletype_softwares/domain"
"teletype_softwares/lib/template_utils"
"github.com/go-chi/chi/v5"
)
type SoftwareController struct {
softwareService domain.SoftwareService
service domain.SoftwareServiceInterface
}
func NewSoftwareController(service domain.SoftwareServiceInterface) *SoftwareController {
return &SoftwareController{service: service}
}
func (c *SoftwareController) index(w http.ResponseWriter, r *http.Request) {
softwares, err := c.softwareService.List()
softwares, err := c.service.List()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl, err := template.ParseFiles("http/views/layouts/main.html", "http/views/index.html")
tmpl, err := template_utils.GetTemplate("index", "http/views/layouts/main.html", "http/views/index.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@@ -31,7 +35,7 @@ func (c *SoftwareController) index(w http.ResponseWriter, r *http.Request) {
func (c *SoftwareController) releases(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
software, err := c.softwareService.GetByNameWithReleases(name)
software, err := c.service.GetByNameWithReleases(name)
if err != nil {
if err == domain.ErrSoftwareNotFound {
http.Error(w, "Software not found", http.StatusNotFound)
@@ -41,7 +45,7 @@ func (c *SoftwareController) releases(w http.ResponseWriter, r *http.Request) {
return
}
tmpl, err := template.ParseFiles("http/views/layouts/main.html", "http/views/releases.html")
tmpl, err := template_utils.GetTemplate("releases", "http/views/layouts/main.html", "http/views/releases.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
+6 -2
View File
@@ -8,7 +8,11 @@ import (
)
type SoftwareUpdaterController struct {
softwareUpdaterService domain.SoftwareUpdaterService
service domain.SoftwareUpdaterServiceInterface
}
func NewSoftwareUpdaterController(service domain.SoftwareUpdaterServiceInterface) *SoftwareUpdaterController {
return &SoftwareUpdaterController{service: service}
}
func (c *SoftwareUpdaterController) update(w http.ResponseWriter, r *http.Request) {
@@ -21,7 +25,7 @@ func (c *SoftwareUpdaterController) update(w http.ResponseWriter, r *http.Reques
platform := r.URL.Query().Get("platform")
name := r.URL.Query().Get("name")
if err := c.softwareUpdaterService.UpdateSoftware(platform, name); err != nil {
if err := c.service.Update(platform, name); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
+7 -9
View File
@@ -5,15 +5,13 @@ import (
"teletype_softwares/lib/http_utils"
)
func StartHttpServer(domain domain.Domain) {
router := Router{
SoftwareController: &SoftwareController{softwareService: domain.SoftwareService},
SoftwareUpdaterController: &SoftwareUpdaterController{
softwareUpdaterService: domain.SoftwareUpdaterService,
},
DownloadController: &DownloadController{downloadService: domain.DownloadService},
PlayController: &PlayController{},
}.Init()
func StartHttpServer(domainInstance domain.Domain) {
router := NewRouter(
NewSoftwareController(domainInstance.SoftwareService),
NewSoftwareUpdaterController(domainInstance.SoftwareUpdaterService),
NewDownloadController(domainInstance.DownloadService),
NewPlayController(),
).Init()
http_utils.StartGenericHTTPServer(http_utils.StartGenericHTTPServerContext{
Router: router,
+28 -14
View File
@@ -5,24 +5,38 @@ import (
)
type Router struct {
SoftwareController *SoftwareController
SoftwareUpdaterController *SoftwareUpdaterController
DownloadController *DownloadController
PlayController *PlayController
softwareController *SoftwareController
softwareUpdaterController *SoftwareUpdaterController
downloadController *DownloadController
playController *PlayController
}
func (r Router) Init() *chi.Mux {
func NewRouter(
softwareController *SoftwareController,
softwareUpdaterController *SoftwareUpdaterController,
downloadController *DownloadController,
playController *PlayController,
) *Router {
return &Router{
softwareController: softwareController,
softwareUpdaterController: softwareUpdaterController,
downloadController: downloadController,
playController: playController,
}
}
func (r *Router) Init() *chi.Mux {
router := chi.NewRouter()
router.Get("/", r.SoftwareController.index)
router.Get("/update", r.SoftwareUpdaterController.update)
router.Get("/releases/{name}", r.SoftwareController.releases)
router.Get("/download/{name}/source", r.DownloadController.DownloadSource)
router.Get("/download/{name}/cartridge", r.DownloadController.DownloadCartridge)
router.Get("/download/{name}/{version}/source", r.DownloadController.DownloadSourceByVersion)
router.Get("/download/{name}/{version}/cartridge", r.DownloadController.DownloadCartridgeByVersion)
router.Get("/play/{name}", r.PlayController.PlayGame)
router.Get("/play/{name}/content*", r.PlayController.ServeGameContent)
router.Get("/", r.softwareController.index)
router.Get("/update", r.softwareUpdaterController.update)
router.Get("/releases/{name}", r.softwareController.releases)
router.Get("/download/{name}/source", r.downloadController.GetLatestSource)
router.Get("/download/{name}/cartridge", r.downloadController.GetLatestCartridge)
router.Get("/download/{name}/{version}/source", r.downloadController.GetSource)
router.Get("/download/{name}/{version}/cartridge", r.downloadController.GetCartridge)
router.Get("/play/{name}", r.playController.Play)
router.Get("/play/{name}/content*", r.playController.ServeContent)
return router
}