You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- CLI and Terminal - [@toasty/prism](https://github.com/thatstoasty/prism), [@toasty/mog](https://github.com/thatstoasty/mog)
43
42
- Date/Time - [@mojoto/morrow](https://github.com/mojoto/morrow.mojo) and [@toasty/small-time](https://github.com/thatstoasty/small-time)
44
43
45
44
<palign="right">(<ahref="#readme-top">back to top</a>)</p>
46
45
47
46
<!-- GETTING STARTED -->
48
47
## Getting Started
49
48
50
-
The only hard dependencies for `lightbug_http` are Mojo and [Git](https://docs.github.com/en/get-started/getting-started-with-git).
51
-
Learn how to get up and running with Mojo on the [Modular website](https://www.modular.com/max/mojo). The Docker installation was removed with the changes in Modular CLI. It will be available once Modular provides needed functionality for Docker setups.
49
+
The only hard dependency for `lightbug_http` is Mojo.
50
+
Learn how to get up and running with Mojo on the [Modular website](https://www.modular.com/max/mojo).
Open `localhost:8080` in your browser. You should see a welcome screen.
69
-
70
-
Congrats 🥳 You're using Lightbug!
71
-
2. Add your handler in `lightbug.🔥` by passing a struct that satisfies the following trait:
63
+
3. Run `magic install` at the root of your project, where `mojoproject.toml` is located
64
+
4. Lightbug should now be installed as a dependency. You can import all the default imports at once, e.g:
65
+
```mojo
66
+
from lightbug_http import *
67
+
```
68
+
or import individual structs and functions, e.g.
69
+
```mojo
70
+
from lightbug_http.http import HTTPService, HTTPRequest, HTTPResponse, OK, NotFound
71
+
```
72
+
there are some default handlers you can play with:
73
+
```mojo
74
+
from lightbug_http.service import Printer # prints request details to console
75
+
from lightbug_http.service import Welcome # serves an HTML file with an image (currently requires manually adding files to static folder, details below)
76
+
from lightbug_http.service import ExampleRouter # serves /, /first, /second, and /echo routes
77
+
```
78
+
5. Add your handler in `lightbug.🔥` by passing a struct that satisfies the following trait:
6. Start a server listening on a port with your service like so.
105
+
```mojo
106
+
fn main() raises:
107
+
var server = SysServer()
108
+
var handler = Printer()
109
+
server.listen_and_serve("0.0.0.0:8080", handler)
110
+
```
111
+
Feel free to change the settings in `listen_and_serve()` to serve on a particular host and port.
112
+
113
+
Now send a request `0.0.0.0:8080`. You should see some details about the request printed out to the console.
112
114
113
-
We plan to add more advanced routing functionality in a future library called `lightbug_api`, see [Roadmap](#roadmap) for more details.
114
-
3. Run `magic run mojo lightbug.🔥`. This will start up a server listening on `localhost:8080`. Or, if you prefer to import the server into your own app:
115
-
```mojo
116
-
from lightbug_http import *
115
+
Congrats 🥳 You're using Lightbug!
116
+
117
+
118
+
Routing is not in scope for this library, but you can easily set up routes yourself:
We plan to add more advanced routing functionality in a future library called `lightbug_api`, see [Roadmap](#roadmap) for more details.
117
141
118
-
fn main() raises:
119
-
var server = SysServer()
120
-
var handler = Welcome()
121
-
server.listen_and_serve("0.0.0.0:8080", handler)
122
-
```
123
-
Feel free to change the settings in `listen_and_serve()` to serve on a particular host and port.
124
142
125
143
<palign="right">(<ahref="#readme-top">back to top</a>)</p>
126
144
127
145
### Serving static files
128
146
129
-
The default welcome screen shows an example of how to serve files like images or HTML using Lightbug. Mojo has built-in `open`, `read` and `read_bytes` methods that you can use to read files from e.g. a `static` directory and serve them on a route:
147
+
The default welcome screen shows an example of how to serve files like images or HTML using Lightbug. Mojo has built-in `open`, `read` and `read_bytes` methods that you can use to read files and serve them on a route. Assuming you copy an html file and image from the Lightbug repo into a `static` directory at the root of your repo:
130
148
131
149
```mojo
132
-
from lightbug_http.http import HTTPService, HTTPRequest, HTTPResponse, OK, NotFound
133
-
from lightbug_http.io.bytes import Bytes
150
+
from lightbug_http import *
134
151
135
152
@value
136
153
struct Welcome(HTTPService):
@@ -157,10 +174,8 @@ struct Welcome(HTTPService):
157
174
Create a file, e.g `client.mojo` with the following code. Run `magic run mojo client.mojo` to execute the request to a given URL.
Pure Mojo-based client is available by default. This client is also used internally for testing the server.
@@ -195,6 +215,7 @@ By default, Lightbug uses the pure Mojo implementation for networking. To use Py
195
215
from lightbug_http.python.server import PythonServer
196
216
```
197
217
You can then use all the regular server commands in the same way as with the default server.
218
+
Note: as of September, 2024, `PythonServer` and `PythonClient` throw a compilation error when starting. There's an open [issue](https://github.com/saviorand/lightbug_http/issues/41) to fix this - contributions welcome!
0 commit comments