Skip to content

Commit 6efed03

Browse files
committed
fix: change response helpers to String
1 parent 76bb36d commit 6efed03

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

lightbug.🔥

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ from lightbug_http import *
22

33
struct HelloWorld(HTTPHandler):
44
fn handle(self, context: Context) -> HTTPResponse:
5-
return OK(String("Hello, World!").as_bytes())
5+
return Success("Hello, World!")
66

77
fn main() raises:
88
var router = RouterMiddleware()

lightbug_http/middleware.mojo

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct ErrorMiddleware(Middleware):
5858
try:
5959
return self.next.call(context)
6060
except e:
61-
return InternalServerError(e.as_bytes())
61+
return InternalServerError(e)
6262

6363

6464
## Compression middleware compresses the response body.
@@ -164,7 +164,7 @@ struct StaticMiddleware(Middleware):
164164
with open(file, "r") as f:
165165
html = f.read()
166166

167-
return OK(html.as_bytes(), "text/html")
167+
return Success(html, "text/html")
168168
except e:
169169
return self.next.call(context)
170170

@@ -208,46 +208,46 @@ struct RouterMiddleware(Middleware):
208208
@value
209209
struct NotFoundMiddleware(Middleware):
210210
fn call(self, context: Context) -> HTTPResponse:
211-
return NotFound(String("Not Found").as_bytes())
211+
return NotFound("Not Found")
212212

213213

214214

215215
### Helper functions to create HTTP responses
216-
fn OK(body: Bytes) -> HTTPResponse:
217-
return OK(body, String("text/plain"))
216+
fn Success(body: String) -> HTTPResponse:
217+
return Success(body, String("text/plain"))
218218

219-
fn OK(body: Bytes, content_type: String) -> HTTPResponse:
219+
fn Success(body: String, content_type: String) -> HTTPResponse:
220220
return HTTPResponse(
221-
ResponseHeader(True, 200, String("OK").as_bytes(), content_type.as_bytes()),
222-
body,
221+
ResponseHeader(True, 200, String("Success").as_bytes(), content_type.as_bytes()),
222+
body.as_bytes(),
223223
)
224224

225-
fn NotFound(body: Bytes) -> HTTPResponse:
225+
fn NotFound(body: String) -> HTTPResponse:
226226
return NotFound(body, String("text/plain"))
227227

228-
fn NotFound(body: Bytes, content_type: String) -> HTTPResponse:
228+
fn NotFound(body: String, content_type: String) -> HTTPResponse:
229229
return HTTPResponse(
230230
ResponseHeader(True, 404, String("Not Found").as_bytes(), content_type.as_bytes()),
231-
body,
231+
body.as_bytes(),
232232
)
233233

234-
fn InternalServerError(body: Bytes) -> HTTPResponse:
234+
fn InternalServerError(body: String) -> HTTPResponse:
235235
return InternalServerErrorResponse(body, String("text/plain"))
236236

237-
fn InternalServerError(body: Bytes, content_type: String) -> HTTPResponse:
237+
fn InternalServerError(body: String, content_type: String) -> HTTPResponse:
238238
return HTTPResponse(
239239
ResponseHeader(True, 500, String("Internal Server Error").as_bytes(), content_type.as_bytes()),
240-
body,
240+
body.as_bytes(),
241241
)
242242

243-
fn Unauthorized(body: Bytes) -> HTTPResponse:
243+
fn Unauthorized(body: String) -> HTTPResponse:
244244
return UnauthorizedResponse(body, String("text/plain"))
245245

246-
fn Unauthorized(body: Bytes, content_type: String) -> HTTPResponse:
246+
fn Unauthorized(body: String, content_type: String) -> HTTPResponse:
247247
var header = ResponseHeader(True, 401, String("Unauthorized").as_bytes(), content_type.as_bytes())
248248
header.headers["WWW-Authenticate"] = "Basic realm=\"Login Required\""
249249

250250
return HTTPResponse(
251251
header,
252-
body,
252+
body.as_bytes(),
253253
)

0 commit comments

Comments
 (0)