Skip to content

Commit 6d472ab

Browse files
author
Jochum van der Ploeg
authored
docs: add documentation about the init method (#590)
1 parent 8b3f625 commit 6d472ab

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

β€Ždocs/docs/advanced/custom_dockerfile.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 2
2+
sidebar_position: 3
33
title: 🐳 Custom Dockerfile
44
---
55

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
sidebar_position: 2
3+
title: πŸ›« Custom Init Method
4+
---
5+
6+
# Custom Init Method πŸ›«
7+
8+
Dart Frog supports creating a custom entrypoint as shown in the [Custom Entrypoint docs](docs/advanced/custom_entrypoint) but that will run every time the server hot reloads. In cases where you want to initialize something only on server start, like setting up a database connection, you can use the `init` method.
9+
10+
## Creating a Custom Init Method ✨
11+
12+
To create a custom init method, simply create a `main.dart` file at the root of your Dart Frog project.
13+
14+
:::warning
15+
Keep in mind that the `main.dart` file must expose a top-level `run` as mentioned in the [Custom Entrypoint docs](docs/advanced/custom_entrypoint).
16+
:::
17+
18+
Add the following top-level `init` method to the `main.dart` file:
19+
20+
```dart
21+
import 'dart:io';
22+
23+
import 'package:dart_frog/dart_frog.dart';
24+
25+
Future<void> init(InternetAddress ip, int port) async {
26+
// Any code initialized within this method will only run on server start, any hot reloads
27+
// afterwards will not trigger this method until a hot restart.
28+
}
29+
30+
Future<HttpServer> run(Handler handler, InternetAddress ip, int port) {
31+
...
32+
}
33+
```
34+
35+
The Dart Frog CLI will detect the `init` method and execute it on server start.

β€Ždocs/docs/advanced/powered_by_header.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 4
2+
sidebar_position: 5
33
title: πŸ”‹ Powered By Header
44
---
55

β€Ždocs/docs/advanced/security_context.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 5
2+
sidebar_position: 6
33
title: πŸ”‘ Security Context
44
---
55

β€Ždocs/docs/advanced/web_socket.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 3
2+
sidebar_position: 4
33
title: πŸ”Œ WebSockets
44
---
55

0 commit comments

Comments
Β (0)