Files
herolib/lib/baobab/generator/templates/interface_http.v.template

47 lines
1.7 KiB
Plaintext

import freeflowuniverse.herolib.schemas.openapi { OpenAPI }
import freeflowuniverse.herolib.baobab.stage {Client, ClientConfig}
import freeflowuniverse.herolib.schemas.openrpc { OpenRPC }
import freeflowuniverse.herolib.baobab.stage.interfaces { HTTPServer, Context }
import veb
@@[params]
pub struct ServerParams {
pub:
base_url string
port int = 8080
}
pub fn new_http_server(params ServerParams) !&HTTPServer {
mut s := interfaces.new_http_server()!
@if openrpc
mut openrpc_controller := new_openrpc_http_controller(ServerParams{
...params,
base_url: '@{dollar}{params.base_url}/openrpc'
})!
s.register_controller[openrpc.HTTPController, Context]('/openrpc', mut openrpc_controller)!
@end
@if openapi
mut openapi_controller := new_openapi_http_controller(ServerParams{
...params,
base_url: '@{dollar}{params.base_url}/openapi/v1'
})!
mut openapi_example_controller := new_openapi_http_controller(ServerParams{
...params,
base_url: '@{dollar}{params.base_url}/openapi/example'
})!
mut openapi_playground_controller := openapi.new_playground_controller(
base_url: '@{dollar}{params.base_url}/playground/openapi'
specification_path: openapi_spec_path
)!
s.register_controller[openapi.HTTPController, Context]('/openapi/v1', mut openapi_controller)!
s.register_controller[openapi.HTTPController, Context]('/openapi/example', mut openapi_example_controller)!
s.register_controller[openapi.PlaygroundController, Context]('/playground/openapi', mut openapi_playground_controller)!
@end
return s
}
pub fn run_http_server(params ServerParams) ! {
mut server := new_http_server(params)!
veb.run[HTTPServer, Context](mut server, params.port)
}