168 lines
7.5 KiB
Go
168 lines
7.5 KiB
Go
// 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 = `<!doctype html>
|
|
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title>{{.Title}} - Offline documentation</title>
|
|
<link rel="stylesheet" href="assets/swagger-ui.css">
|
|
<style>body{margin:0;background:#fafafa}nav{padding:16px 24px;background:#172033;color:white;font:14px system-ui;display:flex;gap:20px;align-items:center;flex-wrap:wrap}nav a{color:#dbeafe}.notice{padding:12px 24px;background:#eff6ff;color:#1e3a5f;font:14px system-ui}.swagger-ui .wrapper{max-width:1400px}</style>
|
|
</head><body>
|
|
<nav><strong>{{.Title}}</strong><a href="index.html">All documentation</a><a href="admin.html">Admin API</a><a href="customer.html">Customer API</a><a href="specs/{{.Name}}.json" download>Download JSON</a><a href="specs/{{.Name}}.yaml" download>Download YAML</a></nav>
|
|
<div class="notice">Offline reference | {{.Count}} operations | API execution is disabled. No backend, CDN, or internet connection is required to read these docs.</div>
|
|
<div id="swagger-ui"></div>
|
|
<script src="assets/swagger-ui-bundle.js"></script>
|
|
<script>
|
|
window.ui=SwaggerUIBundle({
|
|
spec:{{.Spec}},dom_id:'#swagger-ui',deepLinking:true,
|
|
presets:[SwaggerUIBundle.presets.apis],layout:'BaseLayout',
|
|
validatorUrl:null,supportedSubmitMethods:[],tryItOutEnabled:false,persistAuthorization:false,
|
|
docExpansion:'list',defaultModelsExpandDepth:0,filter:true
|
|
});
|
|
</script></body></html>`
|
|
|
|
const indexTemplate = `<!doctype html>
|
|
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Rezina API - Offline documentation</title>
|
|
<style>body{margin:0;background:#f6f8fc;color:#172033;font:16px/1.6 system-ui,sans-serif}main{max-width:960px;margin:64px auto;padding:0 24px}h1{font-size:36px;line-height:1.2}.cards{display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:20px;margin:32px 0}.card{display:block;border:1px solid #dce3ee;border-radius:12px;background:white;padding:24px;text-decoration:none;color:inherit}.card:hover{border-color:#2563eb}.card h2{margin-top:0;color:#1d4ed8}code{background:#e9eef7;padding:2px 6px;border-radius:4px}.note{background:#eaf2ff;border-radius:10px;padding:20px}footer{margin-top:32px;color:#526174;font-size:14px}</style>
|
|
</head><body><main><h1>Rezina API documentation</h1><p>Browse the exported Swagger documentation locally. Everything needed to view these pages is included in this folder.</p>
|
|
<div class="cards"><a class="card" href="admin.html"><h2>Admin API</h2><p>Staff operations under <code>/api/v1/admin</code>.</p></a><a class="card" href="customer.html"><h2>Customer API</h2><p>Customer web and mobile operations under <code>/api/v1</code>.</p></a><a class="card" href="all.html"><h2>Complete API</h2><p>All documented operations, including gateway callbacks and integration routes.</p></a></div>
|
|
<div class="note"><strong>Works offline.</strong> 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.</div>
|
|
<footer>JSON and YAML specifications are included in <code>specs/</code> for import into API tools. Third-party licenses: <a href="THIRD_PARTY_LICENSE.txt">Swagger UI and packaging</a>.</footer>
|
|
</main></body></html>`
|
|
|
|
// 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
|
|
}
|