-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
590 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.dockerignore | ||
Dockerfile | ||
build/ | ||
.dart_tool/ | ||
.git/ | ||
.github/ | ||
.gitignore | ||
.idea/ | ||
.packages |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Files and directories created by pub. | ||
.dart_tool/ | ||
.packages | ||
|
||
# Conventional directory for build output. | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Use latest stable channel SDK. | ||
FROM dart:stable AS build | ||
|
||
# Resolve app dependencies. | ||
WORKDIR /app | ||
COPY pubspec.* ./ | ||
RUN dart pub get | ||
|
||
# Copy app source code (except anything in .dockerignore) and AOT compile app. | ||
COPY . . | ||
RUN dart compile exe bin/server.dart -o bin/server | ||
|
||
# Build minimal serving image from AOT-compiled `/server` | ||
# and the pre-built AOT-runtime in the `/runtime/` directory of the base image. | ||
FROM scratch | ||
COPY --from=build /runtime/ / | ||
COPY --from=build /app/bin/server /app/bin/ | ||
|
||
# Start server. | ||
EXPOSE 8080 | ||
CMD ["/app/bin/server"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# How to run? | ||
|
||
Download dependencies | ||
|
||
```sh | ||
dart pub get | ||
``` | ||
|
||
Run server | ||
|
||
```sh | ||
dart bin\server.dart | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This file configures the static analysis results for your project (errors, | ||
# warnings, and lints). | ||
# | ||
# This enables the 'recommended' set of lints from `package:lints`. | ||
# This set helps identify many issues that may lead to problems when running | ||
# or consuming Dart code, and enforces writing Dart using a single, idiomatic | ||
# style and format. | ||
# | ||
# If you want a smaller set of lints you can change this to specify | ||
# 'package:lints/core.yaml'. These are just the most critical lints | ||
# (the recommended set includes the core lints). | ||
# The core lints are also what is used by pub.dev for scoring packages. | ||
|
||
include: package:lints/recommended.yaml | ||
|
||
# Uncomment the following section to specify additional rules. | ||
|
||
# linter: | ||
# rules: | ||
# - camel_case_types | ||
|
||
# analyzer: | ||
# exclude: | ||
# - path/to/excluded/files/** | ||
|
||
# For more information about the core and recommended set of lints, see | ||
# https://dart.dev/go/core-lints | ||
|
||
# For additional information about configuring this file, see | ||
# https://dart.dev/guides/language/analysis-options |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:dia/dia.dart'; | ||
import 'package:dia_body/dia_body.dart'; | ||
import 'package:dia_router/dia_router.dart'; | ||
|
||
class ServerContext extends Context with Routing, ParsedBody { | ||
ServerContext(super.request); | ||
|
||
@override | ||
String toString() { | ||
var res = <String, String>{}; | ||
res['query'] = query.toString(); | ||
res['params'] = params.toString(); | ||
res['files'] = files.toString(); | ||
res['parsed'] = parsed.toString(); | ||
|
||
return res.toString(); | ||
} | ||
} | ||
|
||
void main(List<String> args) async { | ||
// Use any available host or container IP (usually `0.0.0.0`). | ||
final ip = "127.0.0.1"; | ||
|
||
final app = App<ServerContext>((req) => ServerContext(req)); | ||
|
||
final router = Router<ServerContext>('/'); | ||
|
||
router.get('/', (ctx, next) async { | ||
ctx.body = json.encode({"response": 'Hello, World!'}); | ||
}); | ||
router.post('/echo', (ctx, next) async { | ||
final map = ctx.parsed; | ||
ctx.body = json.encode({"response": "Hello, ${map["name"]}!"}); | ||
}); | ||
router.post('/file_upload', (ctx, next) async { | ||
final files = ctx.files; | ||
ctx.body = files.length.toString(); | ||
}); | ||
router.post('/json_obj', (ctx, next) async { | ||
final body = ctx.parsed; | ||
ctx.body = body.length.toString(); | ||
}); | ||
|
||
app.use(body()); | ||
app.use(router.middleware); | ||
|
||
// For running in containers, we respect the PORT environment variable. | ||
final port = int.parse(Platform.environment['PORT'] ?? '8080'); | ||
app | ||
.listen(ip, port) | ||
.then((info) => print('Server listening on port $ip:$port')); | ||
} |
Oops, something went wrong.