docs handling

This commit is contained in:
2026-02-27 14:47:28 +01:00
parent a795426700
commit f9cb923639
7 changed files with 93 additions and 1 deletions
+1
View File
@@ -9,4 +9,5 @@ type Release struct {
CartridgePath string `gorm:"size:255" json:"-"`
SourcePath string `gorm:"size:255" json:"-"`
HTMLFolderPath string `gorm:"size:255" json:"-"`
DocsFolderPath string `gorm:"size:255" json:"-"`
}
+15 -1
View File
@@ -35,27 +35,41 @@ func (s *SoftwareUpdaterTIC80Service) Update(name, version string) error {
versioned_name := name + "-" + version
zip_filename := versioned_name + ".html.zip"
docs_zip_filename := versioned_name + "-docs.zip"
cartridge_filename := versioned_name + ".tic"
source_filename := versioned_name + ".lua"
html_dirname := versioned_name
docs_dirname := versioned_name + "-docs"
fmt.Printf("TIC80 Updater: Processing zip file: %s\n", zip_filename)
fmt.Printf("TIC80 Updater: Processing docs zip file: %s\n", docs_zip_filename)
fmt.Printf("TIC80 Updater: Processing cartridge file: %s\n", cartridge_filename)
fmt.Printf("TIC80 Updater: Processing source file: %s\n", source_filename)
fmt.Printf("TIC80 Updater: Processing HTML directory: %s\n", html_dirname)
fmt.Printf("TIC80 Updater: Processing docs directory: %s\n", docs_dirname)
if s.fileRepository.FileExists(html_dirname) {
fmt.Printf("TIC80 Updater: Removing existing HTML directory: %s\n", html_dirname)
s.fileRepository.DeleteDir(html_dirname)
}
if s.fileRepository.FileExists(docs_dirname) {
fmt.Printf("TIC80 Updater: Removing existing docs directory: %s\n", docs_dirname)
s.fileRepository.DeleteDir(docs_dirname)
}
s.fileRepository.CreateDir(html_dirname)
fmt.Printf("TIC80 Updater: Unzipping file: %s to directory: %s\n", zip_filename, html_dirname)
s.fileRepository.UnzipFile(zip_filename, html_dirname)
s.fileRepository.CreateDir(docs_dirname)
fmt.Printf("TIC80 Updater: Unzipping docs file: %s to directory: %s\n", docs_zip_filename, docs_dirname)
s.fileRepository.UnzipFile(docs_zip_filename, docs_dirname)
cartridge_path := s.fileRepository.GetPath((cartridge_filename))
source_path := s.fileRepository.GetPath(source_filename)
html_dir_path := s.fileRepository.GetPath(html_dirname)
docs_dir_path := s.fileRepository.GetPath(docs_dirname)
var err error
fmt.Printf("TIC80 Updater: Extracting metadata from source file: %s\n", source_path)
@@ -66,7 +80,6 @@ func (s *SoftwareUpdaterTIC80Service) Update(name, version string) error {
}
software := s.BuildSoftware(meta_data)
err = s.softwareRepository.UpdateOrCreate(software)
if err != nil {
fmt.Printf("TIC80 Updater: Error updating or creating software: %s\n", err.Error())
@@ -80,6 +93,7 @@ func (s *SoftwareUpdaterTIC80Service) Update(name, version string) error {
CartridgePath: cartridge_path,
SourcePath: source_path,
HTMLFolderPath: html_dir_path,
DocsFolderPath: docs_dir_path,
}
s.releaseRepository.CreateIfNotExist(&release)
+63
View File
@@ -0,0 +1,63 @@
package http
import (
"fmt"
"net/http"
"os"
"teletype_softwares/domain"
"github.com/go-chi/chi/v5"
)
type DocsController struct {
softwareService domain.SoftwareServiceInterface
fileService domain.FileServiceInterface
}
func NewDocsController(software_service domain.SoftwareServiceInterface, file_service domain.FileServiceInterface) *DocsController {
return &DocsController{
softwareService: software_service,
fileService: file_service,
}
}
func (c *DocsController) ServeDocs(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 targetRelease *domain.Release
for _, release := range software.Releases {
if release.Version == version && release.DocsFolderPath != "" {
targetRelease = &release
break
}
}
if targetRelease == nil {
http.Error(w, "No documentation found for this release", http.StatusNotFound)
return
}
docs_base_dir := c.fileService.GetPath(targetRelease.DocsFolderPath)
if _, err := os.Stat(docs_base_dir); os.IsNotExist(err) {
http.Error(w, fmt.Sprintf("Documentation for '%s' version '%s' not found.", name, version), http.StatusNotFound)
return
} else if err != nil {
http.Error(w, fmt.Sprintf("Error accessing documentation for '%s' version '%s': %v", name, version, err), http.StatusInternalServerError)
return
}
fs := http.StripPrefix(fmt.Sprintf("/docs/%s/%s", name, version), http.FileServer(http.Dir(docs_base_dir)))
fs.ServeHTTP(w, r)
}
+1
View File
@@ -12,6 +12,7 @@ func StartHttpServer(domain_instance domain.Domain) {
NewDownloadController(domain_instance.DownloadService, domain_instance.SoftwareService),
NewPlayController(domain_instance.SoftwareService, domain_instance.FileService),
NewRootController(domain_instance.SoftwareService),
NewDocsController(domain_instance.SoftwareService, domain_instance.FileService),
).Init()
http_utils.StartGenericHTTPServer(http_utils.StartGenericHTTPServerContext{
+4
View File
@@ -12,6 +12,7 @@ type Router struct {
downloadController *DownloadController
playController *PlayController
rootController *RootController
docsController *DocsController
}
func NewRouter(
@@ -20,6 +21,7 @@ func NewRouter(
download_controller *DownloadController,
play_controller *PlayController,
root_controller *RootController,
docs_controller *DocsController,
) *Router {
return &Router{
softwareController: software_controller,
@@ -27,6 +29,7 @@ func NewRouter(
downloadController: download_controller,
playController: play_controller,
rootController: root_controller,
docsController: docs_controller,
}
}
@@ -44,6 +47,7 @@ func (r *Router) Init() *chi.Mux {
router.Get("/download/{name}/{version}/cartridge", r.downloadController.GetCartridge)
router.Get("/play/{name}/{version}", r.playController.Play)
router.Get("/play/{name}/{version}/content*", r.playController.ServeContent)
router.Get("/docs/{name}/{version}/*", r.docsController.ServeDocs)
fs := http.FileServer(http.Dir("assets"))
router.Handle("/assets/*", http.StripPrefix("/assets/", fs))
+3
View File
@@ -8,6 +8,9 @@
<div style="max-width: 960px; margin: 10px auto; text-align: center;">
<div class="btn-group" role="group" aria-label="Game controls">
<a href="/releases/{{.Software.Name}}" class="btn btn-primary">Back to Releases</a>
{{if .WebPlayableRelease.DocsFolderPath}}
<a href="/docs/{{.Software.Name}}/{{.WebPlayableRelease.Version}}" class="btn btn-info">Docs</a>
{{end}}
<button id="fullscreenButton" class="btn btn-secondary">Fullscreen</button>
</div>
</div>
+6
View File
@@ -17,6 +17,9 @@
<div class="card-footer">
<div class="btn-group d-flex justify-content-center" role="group" aria-label="Game controls">
<button id="fullscreenButton" class="btn btn-secondary btn-sm">Fullscreen</button>
{{if .WebPlayableRelease.DocsFolderPath}}
<a href="/docs/{{.Software.Name}}/{{.WebPlayableRelease.Version}}" class="btn btn-info btn-sm">Docs</a>
{{end}}
</div>
</div>
</div>
@@ -85,6 +88,9 @@
class="btn btn-primary btn-sm">Source</a>
<a href="/download/{{$.Software.Name}}/{{.Version}}/cartridge"
class="btn btn-primary btn-sm">Cartridge</a>
{{if .DocsFolderPath}}
<a href="/docs/{{$.Software.Name}}/{{.Version}}" class="btn btn-info btn-sm">Docs</a>
{{end}}
</div>
</div>
</div>