docs handling
This commit is contained in:
@@ -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)
|
||||
}
|
||||
@@ -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{
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user