Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exported/
schemas/*.json
.idea/*
.idea/*
node_js/node_modules
24 changes: 20 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
# Stage 1: Go builder
FROM golang:1.25.3-bookworm AS builder
COPY . /server
WORKDIR /server
COPY . /server
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /bin/autodoc main.go

# Stage 2: Node.js builder (optional, to install deps)
FROM node:20-bookworm AS node-builder
WORKDIR /app
COPY node_js/package*.json ./
RUN npm ci --production
COPY node_js/ ./

# Stage 3: Final image
FROM ubuntu:22.04
RUN apt-get update && \
apt-get install -y jq curl ca-certificates && \
rm -rf /var/lib/apt/lists/*
COPY ./html-swagger.sh /html-swagger.sh
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
apt-get install -y nodejs

# Copy Go binary
COPY --from=builder /bin/autodoc /bin/autodoc
# Copy Node.js app + deps
COPY --from=node-builder /app /node_js
COPY ./html-swagger.sh /html-swagger.sh
WORKDIR /
RUN chmod +x /node_js/deref.js

# Default command: Go binary
CMD ["/bin/autodoc"]

41 changes: 41 additions & 0 deletions api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
Expand All @@ -10,6 +11,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
)

// BaseCDNUrl is the base CDN URL used to construct export links.
Expand Down Expand Up @@ -48,6 +50,7 @@ type ExportSuccess struct {

// returnError sends an error response to the client.
func returnError(w http.ResponseWriter, err error) {
slog.Error("Error handling request: %v", err)
w.WriteHeader(http.StatusBadRequest)
newError := ErrorResponse{Error: err.Error()}
_ = json.NewEncoder(w).Encode(newError)
Expand Down Expand Up @@ -115,6 +118,43 @@ func openapiExport(w http.ResponseWriter, r *http.Request) {
slog.Info("URL: %s", success.Url)
}

// expandedOpenapi handles OpenAPI export requests.
//
// @Summary Export OpenAPI schema to Redoc
// @Description Returns the expanded openapi schema
// @Tags openapi
// @Param name path string true "JSON name"
// @Accept application/json
// @Produce application/json
// @Success 200 {object} FullOpenAPI
// @Failure 400 {object} ErrorResponse
// @Router /expand/{name} [get]
func expandedOpenapi(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
schemaName := vars["name"]
slog.Info("Received request for schema: %s", schemaName)
schemaPath, err := filepath.Abs(
fmt.Sprintf("./schemas/%s.json", strings.TrimSuffix(schemaName, ".json")),
)
if err != nil {
returnError(w, err)
return
}
cmd := exec.CommandContext(context.Background(), "node", "node_js/deref.js", schemaPath)
cmd.Dir = ""
output, err := cmd.CombinedOutput()
if err != nil {
returnError(w, fmt.Errorf("error running command: %w; output: %s", err, output))
return
}
if !json.Valid(output) {
slog.Error("Failed to parse JSON: %s", output)
returnError(w, fmt.Errorf("failed to parse JSON: %s", output))
return
}
_, _ = w.Write(output)
}

type AllResponse struct {
AllFiles []string `json:"allFiles"`
}
Expand Down Expand Up @@ -151,5 +191,6 @@ func Router() *mux.Router {
r := mux.NewRouter().StrictSlash(true)
r.HandleFunc("/openapi-export", openapiExport).Methods(http.MethodPost)
r.HandleFunc("/all", listAll).Methods(http.MethodGet)
r.HandleFunc("/expand/{name}", expandedOpenapi).Methods(http.MethodGet)
return r
}
42 changes: 40 additions & 2 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const docTemplate = `{
"paths": {
"/all": {
"get": {
"description": "Recursively scans the ./scalar directory and returns a list of CDN URLs pointing to each ` + "`" + `index.",
"description": "Recursively scans the ./exported directory and returns a list of CDN URLs pointing to each ` + "`" + `index.",
"produces": [
"application/json"
],
"tags": [
"Files"
],
"summary": "Get all index.html files in the \"scalar\" directory",
"summary": "Get all index.html files in the \"exported\" directory",
"responses": {
"200": {
"description": "OK",
Expand All @@ -41,6 +41,44 @@ const docTemplate = `{
}
}
},
"/expand/{name}": {
"get": {
"description": "Returns the expanded openapi schema",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"openapi"
],
"summary": "Export OpenAPI schema to Redoc",
"parameters": [
{
"type": "string",
"description": "JSON name",
"name": "name",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/api.FullOpenAPI"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.ErrorResponse"
}
}
}
}
},
"/openapi-export": {
"post": {
"description": "Accepts a full OpenAPI JSON schema, builds static Redoc documentation, and returns a CDN URL.",
Expand Down
42 changes: 40 additions & 2 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"paths": {
"/all": {
"get": {
"description": "Recursively scans the ./scalar directory and returns a list of CDN URLs pointing to each `index.",
"description": "Recursively scans the ./exported directory and returns a list of CDN URLs pointing to each `index.",
"produces": [
"application/json"
],
"tags": [
"Files"
],
"summary": "Get all index.html files in the \"scalar\" directory",
"summary": "Get all index.html files in the \"exported\" directory",
"responses": {
"200": {
"description": "OK",
Expand All @@ -34,6 +34,44 @@
}
}
},
"/expand/{name}": {
"get": {
"description": "Returns the expanded openapi schema",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"openapi"
],
"summary": "Export OpenAPI schema to Redoc",
"parameters": [
{
"type": "string",
"description": "JSON name",
"name": "name",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/api.FullOpenAPI"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.ErrorResponse"
}
}
}
}
},
"/openapi-export": {
"post": {
"description": "Accepts a full OpenAPI JSON schema, builds static Redoc documentation, and returns a CDN URL.",
Expand Down
29 changes: 27 additions & 2 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ info:
paths:
/all:
get:
description: Recursively scans the ./scalar directory and returns a list of
description: Recursively scans the ./exported directory and returns a list of
CDN URLs pointing to each `index.
produces:
- application/json
Expand All @@ -83,9 +83,34 @@ paths:
description: Internal Server Error
schema:
$ref: '#/definitions/api.ErrorResponse'
summary: Get all index.html files in the "scalar" directory
summary: Get all index.html files in the "exported" directory
tags:
- Files
/expand/{name}:
get:
consumes:
- application/json
description: Returns the expanded openapi schema
parameters:
- description: JSON name
in: path
name: name
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/api.FullOpenAPI'
"400":
description: Bad Request
schema:
$ref: '#/definitions/api.ErrorResponse'
summary: Export OpenAPI schema to Redoc
tags:
- openapi
/openapi-export:
post:
consumes:
Expand Down
24 changes: 7 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,29 @@ module autodoc

go 1.25.3

require (
github.com/gorilla/mux v1.8.1
github.com/swaggo/http-swagger v1.3.4
github.com/swaggo/swag v1.16.6
)

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
github.com/go-openapi/jsonpointer v0.22.1 // indirect
github.com/go-openapi/jsonreference v0.21.2 // indirect
github.com/go-openapi/spec v0.22.0 // indirect
github.com/go-openapi/swag v0.25.1 // indirect
github.com/go-openapi/swag/conv v0.25.1 // indirect
github.com/go-openapi/swag/jsonname v0.25.1 // indirect
github.com/go-openapi/swag/jsonutils v0.25.1 // indirect
github.com/go-openapi/swag/loading v0.25.1 // indirect
github.com/go-openapi/swag/stringutils v0.25.1 // indirect
github.com/go-openapi/swag/typeutils v0.25.1 // indirect
github.com/go-openapi/swag/yamlutils v0.25.1 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/swaggo/files v1.0.1 // indirect
github.com/swaggo/http-swagger v1.3.4 // indirect
github.com/swaggo/swag v1.16.6 // indirect
github.com/urfave/cli/v2 v2.27.7 // indirect
github.com/xrash/smetrics v0.0.0-20250705151800-55b8f293f342 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/mod v0.29.0 // indirect
golang.org/x/net v0.46.0 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/text v0.30.0 // indirect
golang.org/x/tools v0.38.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
sigs.k8s.io/yaml v1.6.0 // indirect
)
Loading