Skip to content

Commit

Permalink
add dia
Browse files Browse the repository at this point in the history
  • Loading branch information
unger1984 committed Mar 15, 2023
1 parent ad03c14 commit 9377fc5
Show file tree
Hide file tree
Showing 10 changed files with 590 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
9 changes: 9 additions & 0 deletions backends/dart_dia/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.dockerignore
Dockerfile
build/
.dart_tool/
.git/
.github/
.gitignore
.idea/
.packages
6 changes: 6 additions & 0 deletions backends/dart_dia/.gitignore
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/
21 changes: 21 additions & 0 deletions backends/dart_dia/Dockerfile
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"]
13 changes: 13 additions & 0 deletions backends/dart_dia/README.md
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
```
30 changes: 30 additions & 0 deletions backends/dart_dia/analysis_options.yaml
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
55 changes: 55 additions & 0 deletions backends/dart_dia/bin/server.dart
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'));
}
Loading

0 comments on commit 9377fc5

Please sign in to comment.