Class: FunApi::App
- Inherits:
-
Object
- Object
- FunApi::App
- Defined in:
- lib/funapi/application.rb
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#openapi_config ⇒ Object
readonly
Returns the value of attribute openapi_config.
-
#shutdown_hooks ⇒ Object
readonly
Returns the value of attribute shutdown_hooks.
-
#startup_hooks ⇒ Object
readonly
Returns the value of attribute startup_hooks.
Instance Method Summary collapse
- #add_cors(allow_origins: ["*"], allow_methods: ["*"], allow_headers: ["*"], expose_headers: [], max_age: 600, allow_credentials: false) ⇒ Object
- #add_gzip ⇒ Object
- #add_request_logger(logger: nil, level: :info) ⇒ Object
- #add_trusted_host(allowed_hosts:) ⇒ Object
- #call(env) ⇒ Object
- #delete(path, query: nil, response_schema: nil, depends: nil, &blk) ⇒ Object
- #get(path, query: nil, response_schema: nil, depends: nil, &blk) ⇒ Object
-
#initialize(title: "FunApi Application", version: "1.0.0", description: "") {|_self| ... } ⇒ App
constructor
A new instance of App.
- #on_shutdown(&block) ⇒ Object
- #on_startup(&block) ⇒ Object
- #patch(path, body: nil, query: nil, response_schema: nil, depends: nil, &blk) ⇒ Object
- #post(path, body: nil, query: nil, response_schema: nil, depends: nil, &blk) ⇒ Object
- #put(path, body: nil, query: nil, response_schema: nil, depends: nil, &blk) ⇒ Object
- #register(key, &block) ⇒ Object
- #resolve(key) ⇒ Object
- #run_shutdown_hooks ⇒ Object
- #run_startup_hooks ⇒ Object
- #use(middleware, *args, &block) ⇒ Object
Constructor Details
#initialize(title: "FunApi Application", version: "1.0.0", description: "") {|_self| ... } ⇒ App
Returns a new instance of App.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/funapi/application.rb', line 22 def initialize(title: "FunApi Application", version: "1.0.0", description: "") @router = Router.new @middleware_stack = [] @container = Dry::Container.new @startup_hooks = [] @shutdown_hooks = [] @openapi_config = { title: title, version: version, description: description } yield self if block_given? register_openapi_routes end |
Instance Attribute Details
#container ⇒ Object (readonly)
Returns the value of attribute container.
20 21 22 |
# File 'lib/funapi/application.rb', line 20 def container @container end |
#openapi_config ⇒ Object (readonly)
Returns the value of attribute openapi_config.
20 21 22 |
# File 'lib/funapi/application.rb', line 20 def openapi_config @openapi_config end |
#shutdown_hooks ⇒ Object (readonly)
Returns the value of attribute shutdown_hooks.
20 21 22 |
# File 'lib/funapi/application.rb', line 20 def shutdown_hooks @shutdown_hooks end |
#startup_hooks ⇒ Object (readonly)
Returns the value of attribute startup_hooks.
20 21 22 |
# File 'lib/funapi/application.rb', line 20 def startup_hooks @startup_hooks end |
Instance Method Details
#add_cors(allow_origins: ["*"], allow_methods: ["*"], allow_headers: ["*"], expose_headers: [], max_age: 600, allow_credentials: false) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/funapi/application.rb', line 109 def add_cors(allow_origins: ["*"], allow_methods: ["*"], allow_headers: ["*"], expose_headers: [], max_age: 600, allow_credentials: false) require_relative "middleware/cors" use FunApi::Middleware::Cors, allow_origins: allow_origins, allow_methods: allow_methods, allow_headers: allow_headers, expose_headers: expose_headers, max_age: max_age, allow_credentials: allow_credentials end |
#add_gzip ⇒ Object
131 132 133 134 135 |
# File 'lib/funapi/application.rb', line 131 def add_gzip use Rack::Deflater, if: lambda { |_env, _status, headers, _body| headers["content-type"]&.start_with?("application/json") } end |
#add_request_logger(logger: nil, level: :info) ⇒ Object
126 127 128 129 |
# File 'lib/funapi/application.rb', line 126 def add_request_logger(logger: nil, level: :info) require_relative "middleware/request_logger" use FunApi::Middleware::RequestLogger, logger: logger, level: level end |
#add_trusted_host(allowed_hosts:) ⇒ Object
121 122 123 124 |
# File 'lib/funapi/application.rb', line 121 def add_trusted_host(allowed_hosts:) require_relative "middleware/trusted_host" use FunApi::Middleware::TrustedHost, allowed_hosts: allowed_hosts end |
#call(env) ⇒ Object
137 138 139 140 |
# File 'lib/funapi/application.rb', line 137 def call(env) app = build_middleware_chain app.call(env) end |
#delete(path, query: nil, response_schema: nil, depends: nil, &blk) ⇒ Object
100 101 102 |
# File 'lib/funapi/application.rb', line 100 def delete(path, query: nil, response_schema: nil, depends: nil, &blk) add_route("DELETE", path, query: query, response_schema: response_schema, depends: depends, &blk) end |
#get(path, query: nil, response_schema: nil, depends: nil, &blk) ⇒ Object
84 85 86 |
# File 'lib/funapi/application.rb', line 84 def get(path, query: nil, response_schema: nil, depends: nil, &blk) add_route("GET", path, query: query, response_schema: response_schema, depends: depends, &blk) end |
#on_shutdown(&block) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/funapi/application.rb', line 46 def on_shutdown(&block) raise ArgumentError, "on_shutdown requires a block" unless block_given? @shutdown_hooks << block self end |
#on_startup(&block) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/funapi/application.rb', line 39 def on_startup(&block) raise ArgumentError, "on_startup requires a block" unless block_given? @startup_hooks << block self end |
#patch(path, body: nil, query: nil, response_schema: nil, depends: nil, &blk) ⇒ Object
96 97 98 |
# File 'lib/funapi/application.rb', line 96 def patch(path, body: nil, query: nil, response_schema: nil, depends: nil, &blk) add_route("PATCH", path, body: body, query: query, response_schema: response_schema, depends: depends, &blk) end |
#post(path, body: nil, query: nil, response_schema: nil, depends: nil, &blk) ⇒ Object
88 89 90 |
# File 'lib/funapi/application.rb', line 88 def post(path, body: nil, query: nil, response_schema: nil, depends: nil, &blk) add_route("POST", path, body: body, query: query, response_schema: response_schema, depends: depends, &blk) end |
#put(path, body: nil, query: nil, response_schema: nil, depends: nil, &blk) ⇒ Object
92 93 94 |
# File 'lib/funapi/application.rb', line 92 def put(path, body: nil, query: nil, response_schema: nil, depends: nil, &blk) add_route("PUT", path, body: body, query: query, response_schema: response_schema, depends: depends, &blk) end |
#register(key, &block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/funapi/application.rb', line 65 def register(key, &block) @container.register(key) do if block.arity == 0 result = block.call if result.is_a?(Array) && result.length == 2 && result[1].respond_to?(:call) ManagedDependency.new(result[0], result[1]) else SimpleDependency.new(result) end else BlockDependency.new(block) end end end |
#resolve(key) ⇒ Object
80 81 82 |
# File 'lib/funapi/application.rb', line 80 def resolve(key) @container.resolve(key) end |
#run_shutdown_hooks ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/funapi/application.rb', line 57 def run_shutdown_hooks @shutdown_hooks.each do |hook| hook.call rescue => e warn "Shutdown hook failed: #{e.}" end end |
#run_startup_hooks ⇒ Object
53 54 55 |
# File 'lib/funapi/application.rb', line 53 def run_startup_hooks @startup_hooks.each(&:call) end |
#use(middleware, *args, &block) ⇒ Object
104 105 106 107 |
# File 'lib/funapi/application.rb', line 104 def use(middleware, *args, &block) @middleware_stack << [middleware, args, block] self end |