// Package offline builds a portable Swagger UI bundle with no backend or CDN requirement. package offline import ( "archive/zip" "bytes" _ "embed" "encoding/json" "fmt" "html/template" "os" "path/filepath" "sort" "strings" "Rezina-Backend/internal/apidocs" swaggerFiles "github.com/swaggo/files" "go.yaml.in/yaml/v3" ) //go:embed swagger_ui_license.txt var thirdPartyLicense []byte const pageTemplate = ` {{.Title}} - Offline documentation
Offline reference | {{.Count}} operations | API execution is disabled. No backend, CDN, or internet connection is required to read these docs.
` const indexTemplate = ` Rezina API - Offline documentation

Rezina API documentation

Browse the exported Swagger documentation locally. Everything needed to view these pages is included in this folder.

Admin API

Staff operations under /api/v1/admin.

Customer API

Customer web and mobile operations under /api/v1.

Complete API

All documented operations, including gateway callbacks and integration routes.

Works offline. Open this file directly in a browser after extracting the ZIP. API execution is disabled in this reference bundle; calling the live API requires a running backend and appropriate authorization.
` // Build returns a bundle in memory. Specifications are embedded in the HTML to avoid file:// fetch/CORS restrictions. func Build() (map[string][]byte, error) { files := map[string][]byte{"index.html": []byte(indexTemplate), "THIRD_PARTY_LICENSE.txt": thirdPartyLicense} for _, asset := range []string{"swagger-ui.css", "swagger-ui-bundle.js"} { content, err := swaggerFiles.ReadFile(asset) if err != nil { return nil, fmt.Errorf("load Swagger UI asset %s: %w", asset, err) } files["assets/"+asset] = content } page, err := template.New("swagger").Parse(pageTemplate) if err != nil { return nil, err } for _, audience := range []string{"admin", "client", "all"} { spec, err := apidocs.Specification(audience) if err != nil { return nil, err } raw, err := json.MarshalIndent(spec, "", " ") if err != nil { return nil, err } yamlSpec, err := yaml.Marshal(spec) if err != nil { return nil, err } name := audience if audience == "client" { name = "customer" } files["specs/"+name+".json"] = append(raw, '\n') files["specs/"+name+".yaml"] = yamlSpec title := "Rezina API" if info, ok := spec["info"].(map[string]any); ok { if value, ok := info["title"].(string); ok { title = value } } count := 0 if paths, ok := spec["paths"].(map[string]any); ok { for _, value := range paths { if operations, ok := value.(map[string]any); ok { for method := range operations { switch method { case "get", "post", "put", "patch", "delete", "head", "options": count++ } } } } } var html bytes.Buffer // encoding/json escapes HTML metacharacters, so the JSON cannot terminate the surrounding script element. data := struct { Title, Name string Count int Spec template.JS }{title, name, count, template.JS(raw)} if err := page.Execute(&html, data); err != nil { return nil, err } files[name+".html"] = html.Bytes() } files["README.txt"] = []byte("Rezina offline API documentation\n\nExtract the ZIP completely, then open index.html in a modern browser.\nAdmin and customer pages use the same audience-specific specifications as the running application.\nAll JavaScript, CSS, and API specifications are local. No server, CDN, or internet connection is needed to read the documentation.\nAPI execution is disabled. Use the live Swagger pages to call a running API.\nJSON and YAML files for admin, customer, and all APIs are in specs/.\nRegenerate with: go run ./cmd/swagger-export -out exports/swagger-offline\n") return files, nil } // Export writes the folder and a ZIP beside it, without removing existing unrelated files. func Export(output string) (string, error) { files, err := Build() if err != nil { return "", err } output, err = filepath.Abs(output) if err != nil { return "", err } if err := os.MkdirAll(output, 0755); err != nil { return "", err } names := make([]string, 0, len(files)) for name := range files { names = append(names, name) } sort.Strings(names) for _, name := range names { target := filepath.Join(output, filepath.FromSlash(name)) if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil { return "", err } if err := os.WriteFile(target, files[name], 0644); err != nil { return "", err } } var archive bytes.Buffer writer := zip.NewWriter(&archive) for _, name := range names { header := &zip.FileHeader{Name: filepath.Base(output) + "/" + strings.ReplaceAll(name, "\\", "/"), Method: zip.Deflate} file, err := writer.CreateHeader(header) if err != nil { return "", err } if _, err := file.Write(files[name]); err != nil { return "", err } } if err := writer.Close(); err != nil { return "", err } zipPath := output + ".zip" if err := os.WriteFile(zipPath, archive.Bytes(), 0644); err != nil { return "", err } return zipPath, nil }