initial commit

This commit is contained in:
2025-12-07 11:32:45 +01:00
commit c3daed67ac
35 changed files with 1315 additions and 0 deletions
+106
View File
@@ -0,0 +1,106 @@
package http
import (
"net/http"
"os"
"path/filepath"
"strings"
"teletype_softwares/domain"
"github.com/go-chi/chi/v5"
)
type DownloadController struct {
downloadService domain.DownloadService
}
func (c *DownloadController) serveReleaseFile(w http.ResponseWriter, r *http.Request, release *domain.Release, isSource bool) {
var filePath string
if isSource {
filePath = release.SourcePath
} else {
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"))
if err != nil {
http.Error(w, "Invalid softwares directory path", http.StatusInternalServerError)
return
}
if !strings.HasPrefix(absFilePath, absSoftwaresDir) {
http.Error(w, "Access denied", http.StatusForbidden)
return
}
w.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(filePath))
http.ServeFile(w, r, filePath)
}
func (c *DownloadController) DownloadSource(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
release, err := c.downloadService.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)
return
}
c.serveReleaseFile(w, r, release, true)
}
func (c *DownloadController) DownloadCartridge(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
release, err := c.downloadService.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)
return
}
c.serveReleaseFile(w, r, release, false)
}
func (c *DownloadController) DownloadSourceByVersion(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
version := chi.URLParam(r, "version")
release, err := c.downloadService.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)
return
}
c.serveReleaseFile(w, r, release, true)
}
func (c *DownloadController) DownloadCartridgeByVersion(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
version := chi.URLParam(r, "version")
release, err := c.downloadService.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)
return
}
c.serveReleaseFile(w, r, release, false)
}
+61
View File
@@ -0,0 +1,61 @@
package http
import (
"fmt"
"html/template" // Only import if template parsing is done here
"net/http"
"os"
"path/filepath"
"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) {
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Game name not provided", http.StatusBadRequest)
return
}
// Parse templates on each request
tmpl, err := template.ParseFiles("http/views/layouts/main.html", "http/views/play.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := struct {
Name string
}{
Name: name,
}
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")
name := chi.URLParam(r, "name")
if name == "" {
http.Error(w, "Game name not provided", http.StatusBadRequest)
return
}
htmlBaseDir := filepath.Join(gamePath, "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)
return
} else if err != nil {
http.Error(w, fmt.Sprintf("Error accessing HTML content for game '%s': %v", name, err), http.StatusInternalServerError)
return
}
fs := http.StripPrefix(fmt.Sprintf("/play/%s/content", name), http.FileServer(http.Dir(htmlBaseDir)))
fs.ServeHTTP(w, r)
}
+53
View File
@@ -0,0 +1,53 @@
package http
import (
"html/template"
"net/http"
"teletype_softwares/domain"
"github.com/go-chi/chi/v5"
)
type SoftwareController struct {
softwareService domain.SoftwareService
}
func (c *SoftwareController) index(w http.ResponseWriter, r *http.Request) {
softwares, err := c.softwareService.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")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl.Execute(w, softwares)
}
func (c *SoftwareController) releases(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
software, err := c.softwareService.GetByNameWithReleases(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
}
tmpl, err := template.ParseFiles("http/views/layouts/main.html", "http/views/releases.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl.Execute(w, map[string]interface{}{
"Software": software,
})
}
+29
View File
@@ -0,0 +1,29 @@
package http
import (
"net/http"
"os"
"teletype_softwares/domain"
)
type SoftwareUpdaterController struct {
softwareUpdaterService domain.SoftwareUpdaterService
}
func (c *SoftwareUpdaterController) update(w http.ResponseWriter, r *http.Request) {
secret := r.URL.Query().Get("secret")
if secret != os.Getenv("UPDATE_SECRET") {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
platform := r.URL.Query().Get("platform")
name := r.URL.Query().Get("name")
if err := c.softwareUpdaterService.UpdateSoftware(platform, name); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte("Updated"))
}
Executable
+21
View File
@@ -0,0 +1,21 @@
package http
import (
"teletype_softwares/domain"
"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()
http_utils.StartGenericHTTPServer(http_utils.StartGenericHTTPServerContext{
Router: router,
})
}
Executable
+28
View File
@@ -0,0 +1,28 @@
package http
import (
"github.com/go-chi/chi/v5"
)
type Router struct {
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)
return router
}
+28
View File
@@ -0,0 +1,28 @@
{{define "content"}}
<h1 class="my-4">Softwares</h1>
<div class="row">
{{range .}}
<div class="col-12 mb-4">
<div class="card h-100">
<div class="card-body">
<h5 class="card-title">{{.Title}}</h5>
<h6 class="card-subtitle mb-2 text-muted">{{.Author}}</h6>
<p class="card-text">{{.Desc}}</p>
<p class="card-text"><strong>Version:</strong> {{.LatestRelease.Version}}</p>
<p class="card-text"><strong>License:</strong> {{.License}}</p>
<p class="card-text"><a href="{{.Site}}" target="_blank">Website</a></p>
</div>
<div class="card-footer">
<div class="btn-group d-flex justify-content-end" role="group" aria-label="Operations">
<a href="/download/{{.Name}}/source" class="btn btn-primary btn-sm">Source</a>
<a href="/download/{{.Name}}/cartridge" class="btn btn-primary btn-sm">Cartridge</a>
<a href="/releases/{{.Name}}" class="btn btn-primary btn-sm">Releases</a>
<a href="/play/{{.Name}}" class="btn btn-primary btn-sm">Play</a>
</div>
</div>
</div>
</div>
{{end}}
</div>
{{end}}
+68
View File
@@ -0,0 +1,68 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Teletype Softwares</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<style>
body {
background-color: black;
color: limegreen; /* Light green */
font-family: "Lucida Console", "Courier New", monospace;
}
.card {
background-color: #333; /* Darker background for cards */
color: limegreen;
border: 1px solid limegreen;
}
a {
color: limegreen;
}
a:hover {
color: #00FF00; /* Brighter green on hover */
}
.btn-primary {
background-color: limegreen;
border-color: limegreen;
color: black;
}
.btn-primary:hover {
background-color: #00FF00;
border-color: #00FF00;
color: black;
}
/* Table styling for black background and green borders */
.table {
--bs-table-bg: black; /* Bootstrap 5 variable for table background */
--bs-table-color: limegreen; /* Text color for table */
--bs-table-border-color: limegreen; /* Border color for the table and cells */
}
.table-striped > tbody > tr:nth-of-type(odd) > * {
--bs-table-bg-type: #1a1a1a; /* Slightly lighter black for striped rows */
color: limegreen;
}
.table-hover > tbody > tr:hover > * {
--bs-table-hover-bg: #444; /* Darker grey on hover */
color: limegreen;
}
.table-bordered > :not(caption) > * > * {
border-color: limegreen;
}
.bg-dark { /* Ensure consistency for manually set dark backgrounds */
background-color: black !important;
}
.text-success { /* Ensure consistency for success text */
color: limegreen !important;
}
</style>
</head>
<body>
<div class="container mt-4">
<h1 class="text-center mb-4">Teletype Games</h1>
{{block "content" .}}{{end}}
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</body>
</html>
+10
View File
@@ -0,0 +1,10 @@
{{define "content"}}
<h1 class="my-4">Play {{.Name}}</h1>
<div class="ratio ratio-4x3" style="max-width: 640px; margin: 0 auto;">
<iframe id="gameFrame" src="/play/{{.Name}}/content/index.html" frameborder="0" allowfullscreen></iframe>
</div>
<p style="margin-top: 20px"><a href="/" class="btn btn-primary">Back to Softwares</a></p>
{{end}}
+31
View File
@@ -0,0 +1,31 @@
{{define "content"}}
<h1 class="my-4">Releases for {{.Software.Title}}</h1>
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered">
<thead class="bg-dark text-success">
<tr>
<th>Version</th>
<th>Released At</th>
<th>Downloads</th>
</tr>
</thead>
<tbody>
{{range .Software.Releases}}
<tr>
<td>{{.Version}}</td>
<td>{{.CreatedAt.Format "2006-01-02 15:04:05"}}</td>
<td>
<div class="btn-group" role="group" aria-label="Download options">
<a href="/download/{{$.Software.Name}}/{{.Version}}/source" class="btn btn-primary btn-sm">Source</a>
<a href="/download/{{$.Software.Name}}/{{.Version}}/cartridge" class="btn btn-primary btn-sm">Cartridge</a>
</div>
</td>
</tr>
{{end}}
</tbody>
</table>
</div>
<p><a href="/" class="btn btn-primary">Back to Softwares</a></p>
{{end}}