Skip to content

Commit 7833d7a

Browse files
committed
make docs enabled optional param
1 parent 8895147 commit 7833d7a

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

lightbug.🔥

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn hello(req: HTTPRequest) -> HTTPResponse:
3030
return OK("Hello 🔥!", "text/plain; charset=utf-8")
3131

3232
fn main() raises:
33-
var app = App()
33+
var app = App[docs_enabled=True]()
3434

3535
app.get("/", hello, "hello")
3636
app.post("/printer", printer, "printer")

lightbug_api/app.mojo

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,26 @@ from os.path import exists
33
from pathlib import Path
44
from sys.ffi import external_call
55
from lightbug_http import HTTPRequest, HTTPResponse, Server, NotFound
6-
from external.emberjson import JSON, Array, Object, Value, to_string
6+
from emberjson import JSON, Array, Object, Value, to_string
77
from lightbug_api.openapi.generate import OpenAPIGenerator
88
from lightbug_api.routing import Router
99
from lightbug_api.logger import logger
1010
from lightbug_api.docs import DocsApp
1111

1212
@value
13-
struct App:
13+
struct App[docs_enabled: Bool = False]:
1414
var router: Router
1515
var lightbug_dir: Path
16-
var docs_enabled: Bool
1716

1817
fn __init__(inout self) raises:
1918
self.router = Router()
2019
self.lightbug_dir = Path()
21-
self.docs_enabled = True
22-
23-
fn __init__(inout self, docs_enabled: Bool) raises:
24-
self.router = Router()
25-
self.lightbug_dir = Path()
26-
self.docs_enabled = docs_enabled
2720

2821
fn set_lightbug_dir(mut self, lightbug_dir: Path):
2922
self.lightbug_dir = lightbug_dir
3023

3124
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
32-
if self.docs_enabled and req.uri.path == "/docs" and req.method == "GET":
25+
if docs_enabled and req.uri.path == "/docs" and req.method == "GET":
3326
var openapi_spec = self.generate_openapi_spec()
3427
var docs = DocsApp(to_string(openapi_spec))
3528
return docs.func(req)
@@ -98,7 +91,7 @@ struct App:
9891
return openapi_spec
9992

10093
fn start_server(mut self, address: StringLiteral = "0.0.0.0:8080") raises:
101-
if self.docs_enabled:
94+
if docs_enabled:
10295
logger.info("API Docs ready at: " + "http://" + String(address) + "/docs")
10396
self.update_temporary_files()
10497
var server = Server()

lightbug_api/openapi/generate.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.dict import Dict
2-
from external.emberjson import JSON, Value, Array, Object, ParseOptions, parse, to_string
2+
from emberjson import JSON, Value, Array, Object, ParseOptions, parse, to_string
33

44
struct OpenAPIGenerator:
55
var tags: List[Value]

magic.lock

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ environments:
4949
- conda: https://conda.anaconda.org/conda-forge/noarch/dnspython-2.7.0-pyhff2d567_1.conda
5050
- conda: https://conda.anaconda.org/conda-forge/noarch/email-validator-2.2.0-pyhd8ed1ab_1.conda
5151
- conda: https://conda.anaconda.org/conda-forge/noarch/email_validator-2.2.0-hd8ed1ab_1.conda
52+
- conda: https://repo.prefix.dev/mojo-community/linux-64/emberjson-0.1.1-hb0f4dca_0.conda
5253
- conda: https://conda.anaconda.org/conda-forge/noarch/exceptiongroup-1.2.2-pyhd8ed1ab_1.conda
5354
- conda: https://conda.anaconda.org/conda-forge/noarch/fastapi-0.115.6-pyhd8ed1ab_0.conda
5455
- conda: https://conda.anaconda.org/conda-forge/noarch/fastapi-cli-0.0.7-pyhd8ed1ab_0.conda
@@ -267,6 +268,7 @@ environments:
267268
- conda: https://conda.anaconda.org/conda-forge/noarch/dnspython-2.7.0-pyhff2d567_1.conda
268269
- conda: https://conda.anaconda.org/conda-forge/noarch/email-validator-2.2.0-pyhd8ed1ab_1.conda
269270
- conda: https://conda.anaconda.org/conda-forge/noarch/email_validator-2.2.0-hd8ed1ab_1.conda
271+
- conda: https://repo.prefix.dev/mojo-community/osx-arm64/emberjson-0.1.1-h60d57d3_0.conda
270272
- conda: https://conda.anaconda.org/conda-forge/noarch/exceptiongroup-1.2.2-pyhd8ed1ab_1.conda
271273
- conda: https://conda.anaconda.org/conda-forge/noarch/fastapi-0.115.6-pyhd8ed1ab_0.conda
272274
- conda: https://conda.anaconda.org/conda-forge/noarch/fastapi-cli-0.0.7-pyhd8ed1ab_0.conda
@@ -1638,6 +1640,34 @@ packages:
16381640
license: Unlicense
16391641
size: 6552
16401642
timestamp: 1733300828176
1643+
- kind: conda
1644+
name: emberjson
1645+
version: 0.1.1
1646+
build: h60d57d3_0
1647+
subdir: osx-arm64
1648+
url: https://repo.prefix.dev/mojo-community/osx-arm64/emberjson-0.1.1-h60d57d3_0.conda
1649+
sha256: 2f684a57551b43254573eb4ef0be49d8de45df046d4693a1e467f8b0cf820a88
1650+
depends:
1651+
- max ==24.6.0
1652+
arch: arm64
1653+
platform: osx
1654+
license: MIT
1655+
size: 681608
1656+
timestamp: 1736671803271
1657+
- kind: conda
1658+
name: emberjson
1659+
version: 0.1.1
1660+
build: hb0f4dca_0
1661+
subdir: linux-64
1662+
url: https://repo.prefix.dev/mojo-community/linux-64/emberjson-0.1.1-hb0f4dca_0.conda
1663+
sha256: 31b60dbaad341c90c47f555e01ce2e6fe4290a7ff8bbfe2994266b88210b46a0
1664+
depends:
1665+
- max ==24.6.0
1666+
arch: x86_64
1667+
platform: linux
1668+
license: MIT
1669+
size: 681619
1670+
timestamp: 1736671846359
16411671
- kind: conda
16421672
name: exceptiongroup
16431673
version: 1.2.2

tests/lightbug_api/test_generate.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from testing import *
2-
from external.emberjson import JSON, Object, Array
2+
from emberjson import JSON, Object, Array
33
from lightbug_api.openapi.generate import OpenAPIGenerator
44

55
def test_create_endpoint():

0 commit comments

Comments
 (0)