Class: SpecForge::CLI::Serve

Inherits:
Command
  • Object
show all
Includes:
Docs::Generate
Defined in:
lib/spec_forge/cli/serve.rb

Overview

Command for generating and serving API documentation

Combines documentation generation with a local web server to provide an easy way to view and interact with generated API documentation. Supports both Swagger UI and Redoc interfaces.

Examples:

Start documentation server

spec_forge serve

Serve with Redoc UI

spec_forge serve --ui redoc

Defined Under Namespace

Classes: Proxy

Constant Summary collapse

VALID_FORMATS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Valid file formats for documentation output

Supported formats include YAML variants (yml, yaml) and JSON. Used for validation when users specify the --format option.

%w[yml yaml json].freeze

Instance Attribute Summary

Attributes inherited from Command

#arguments, #options

Instance Method Summary collapse

Methods included from Docs::Generate

#generate_documentation

Methods inherited from Command

aliases, example, #initialize, option, register

Methods included from Actions

included

Constructor Details

This class inherits a constructor from SpecForge::CLI::Command

Instance Method Details

#callvoid

This method returns an undefined value.

Generates documentation and starts a local web server

Creates OpenAPI documentation from tests and serves it through a local HTTP server with either Swagger UI or Redoc interface for easy viewing.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/spec_forge/cli/serve.rb', line 80

def call
  server_path = SpecForge.openapi_path.join("server")
  actions.empty_directory(server_path, verbose: false) # spec_forge/openapi/server

  # Generate and copy the OpenAPI spec file
  file_name = generate_and_copy_openapi_spec

  # Determine which template file to use
  template_name =
    if options.ui == "redoc"
      "redoc.html.tt"
    else
      "swagger.html.tt"
    end

  # Remove the index if it exists
  index_path = server_path.join("index.html")
  index_path.delete if index_path.exist?

  # Generate index.html
  actions.template(
    template_name,
    index_path,
    context: Proxy.new(spec_url: file_name).call,
    verbose: false
  )

  # And serve it!
  port = options.port || 8080
  server = WEBrick::HTTPServer.new(
    Port: port,
    DocumentRoot: server_path
  )

  puts <<~STRING
    ========================================
    🚀 SpecForge Documentation Server
    ========================================
    Server running at: http://localhost:#{port}
    Press Ctrl+C to stop
    ========================================
  STRING

  trap("INT") { server.shutdown }
  server.start
end