diff --git a/domain/model.release.go b/domain/model.release.go index 0501fe2..3ca4a59 100644 --- a/domain/model.release.go +++ b/domain/model.release.go @@ -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:"-"` } diff --git a/domain/service.software_updater_tic80.go b/domain/service.software_updater_tic80.go index 87b6316..79b3c8c 100644 --- a/domain/service.software_updater_tic80.go +++ b/domain/service.software_updater_tic80.go @@ -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) diff --git a/http/controller.docs.go b/http/controller.docs.go new file mode 100644 index 0000000..516af4c --- /dev/null +++ b/http/controller.docs.go @@ -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) +} diff --git a/http/http.go b/http/http.go index d6dc8b0..e9affb4 100755 --- a/http/http.go +++ b/http/http.go @@ -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{ diff --git a/http/router.go b/http/router.go index 576dd48..99d5f82 100755 --- a/http/router.go +++ b/http/router.go @@ -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)) diff --git a/http/views/play/play.html b/http/views/play/play.html index 67c529a..22e57cb 100644 --- a/http/views/play/play.html +++ b/http/views/play/play.html @@ -8,6 +8,9 @@