diff --git a/bin/running_on_dart.dart b/bin/running_on_dart.dart index 2b6206f..575e311 100644 --- a/bin/running_on_dart.dart +++ b/bin/running_on_dart.dart @@ -47,4 +47,6 @@ void main() async { )); await setupContainer(client); + + WebServer().startServer(); } diff --git a/docker-compose.yml b/docker-compose.yml index e831fe4..8db9f0a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,8 +6,8 @@ services: container_name: running_on_dart env_file: - .env - links: - - db + ports: + - "8088:8088" depends_on: - db diff --git a/lib/running_on_dart.dart b/lib/running_on_dart.dart index 2dc8d04..080f796 100644 --- a/lib/running_on_dart.dart +++ b/lib/running_on_dart.dart @@ -15,3 +15,5 @@ export 'src/settings.dart'; export 'src/converter.dart'; export 'src/error_handler.dart'; export 'src/init.dart'; + +export 'src/api/api_server.dart' show WebServer; diff --git a/lib/src/api/api_server.dart b/lib/src/api/api_server.dart new file mode 100644 index 0000000..6dfc1e1 --- /dev/null +++ b/lib/src/api/api_server.dart @@ -0,0 +1,35 @@ +import 'dart:convert'; + +import 'package:injector/injector.dart'; +import 'package:running_on_dart/src/api/jwt_middleware.dart'; +import 'package:running_on_dart/src/modules/bot_start_duration.dart'; + +import 'package:shelf_router/shelf_router.dart' as shelf_router; +import 'package:shelf/shelf.dart' as shelf; +import 'package:shelf/shelf_io.dart' as shelf_io; + +class WebServer { + Future _handleBotInfo(shelf.Request request) async { + final botStartDuration = Injector.appInstance.get(); + + return shelf.Response.ok(jsonEncode({"ok": true, 'uptime': botStartDuration.startDate.toIso8601String()}), headers: {'Content-Type': 'application/json'}); + } + + Future _setupRouter() async { + return shelf_router.Router() + ..get("/api/info", _handleBotInfo) + ..get("/api/test", _authorized(_handleBotInfo)); + } + + shelf.Handler _authorized(shelf.Handler inner) => const shelf.Pipeline().addMiddleware(jwtMiddleware()).addHandler(inner); + + Future startServer() async { + final router = await _setupRouter(); + + final app = const shelf.Pipeline() + .addMiddleware(shelf.logRequests()) + .addHandler(router.call); + + await shelf_io.serve(app, "0.0.0.0", 8088); + } +} diff --git a/lib/src/api/jwt_middleware.dart b/lib/src/api/jwt_middleware.dart new file mode 100644 index 0000000..0bc366b --- /dev/null +++ b/lib/src/api/jwt_middleware.dart @@ -0,0 +1,13 @@ +import 'package:shelf/shelf.dart' as shelf; + +shelf.Middleware jwtMiddleware() => (shelf.Handler handler) { + return (shelf.Request request) { + final authHeader = request.headers['Authorization']; + + if (authHeader == null) { + return shelf.Response.unauthorized(null); + } + + return handler(request); + }; +}; diff --git a/pubspec.lock b/pubspec.lock index a14364b..5f977dc 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -85,10 +85,10 @@ packages: dependency: transitive description: name: clock - sha256: fddb70d9b5277016c77a80201021d40a2247104d9f4aa7bab7157b7e3f05b84b + sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf url: "https://pub.dev" source: hosted - version: "1.1.2" + version: "1.1.1" collection: dependency: "direct main" description: @@ -105,14 +105,6 @@ packages: url: "https://pub.dev" source: hosted version: "3.1.2" - coverage: - dependency: transitive - description: - name: coverage - sha256: "4b03e11f6d5b8f6e5bb5e9f7889a56fe6c5cbe942da5378ea4d4d7f73ef9dfe5" - url: "https://pub.dev" - source: hosted - version: "1.11.0" crypto: dependency: transitive description: @@ -169,14 +161,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.1.1" - frontend_server_client: - dependency: transitive - description: - name: frontend_server_client - sha256: f64a0333a82f30b0cca061bc3d143813a486dc086b574bfb233b7c1372427694 - url: "https://pub.dev" - source: hosted - version: "4.0.0" fuzzy: dependency: "direct main" description: @@ -209,14 +193,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.2.2" - http_multi_server: + http_methods: dependency: transitive description: - name: http_multi_server - sha256: "97486f20f9c2f7be8f514851703d0119c3596d14ea63227af6f7a481ef2b2f8b" + name: http_methods + sha256: "6bccce8f1ec7b5d701e7921dca35e202d425b57e317ba1a37f2638590e29e566" url: "https://pub.dev" source: hosted - version: "3.2.1" + version: "1.1.1" http_parser: dependency: transitive description: @@ -245,26 +229,10 @@ packages: dependency: "direct main" description: name: intl - sha256: "99f282cb0e02edcbbf8c6b3bbc7c90b65635156c412e58f3975a7e55284ce685" - url: "https://pub.dev" - source: hosted - version: "0.20.0" - io: - dependency: transitive - description: - name: io - sha256: "2ec25704aba361659e10e3e5f5d672068d332fc8ac516421d483a11e5cbd061e" + sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf url: "https://pub.dev" source: hosted - version: "1.0.4" - js: - dependency: transitive - description: - name: js - sha256: c1b2e9b5ea78c45e1a0788d29606ba27dc5f71f019f32ca5140f61ef071838cf - url: "https://pub.dev" - source: hosted - version: "0.7.1" + version: "0.19.0" lints: dependency: "direct dev" description: @@ -285,10 +253,10 @@ packages: dependency: transitive description: name: matcher - sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb + sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2 url: "https://pub.dev" source: hosted - version: "0.12.16+1" + version: "0.12.17" meta: dependency: transitive description: @@ -302,34 +270,18 @@ packages: description: path: "." ref: HEAD - resolved-ref: "82104b2934f7fa13a88e2cd596d4b4df932e6cd6" + resolved-ref: a31aaa35c975e8a6d17554d0241c11c297b7afb6 url: "https://github.com/nyxx-discord/migent.git" source: git - version: "1.2.0" - mime: - dependency: transitive - description: - name: mime - sha256: "41a20518f0cb1256669420fdba0cd90d21561e560ac240f26ef8322e45bb7ed6" - url: "https://pub.dev" - source: hosted - version: "2.0.0" - node_preamble: - dependency: transitive - description: - name: node_preamble - sha256: "6e7eac89047ab8a8d26cf16127b5ed26de65209847630400f9aefd7cd5c730db" - url: "https://pub.dev" - source: hosted - version: "2.0.2" + version: "1.1.0" nyxx: dependency: "direct main" description: name: nyxx - sha256: "3ee891f4098c0dd7b992dafc2b4e961b82bd076b3149a15408efda1e11c7c5aa" + sha256: "073c4f3a49269b4580c54aef32a472b3eae0e6f0e125eb1be2eeb5976210e886" url: "https://pub.dev" source: hosted - version: "6.5.2" + version: "6.5.0" nyxx_commands: dependency: "direct main" description: @@ -342,10 +294,10 @@ packages: dependency: "direct main" description: name: nyxx_extensions - sha256: f0b6dfc5ed2350c10f3018c7fc5fbaa7c01fbce6fee8b8fb14724f57cc0d67fe + sha256: "8016605fc131320bfb58362e5124a0fb1c23e51c26f1638098611fa8f93760aa" url: "https://pub.dev" source: hosted - version: "4.2.1" + version: "4.2.0" oauth2: dependency: transitive description: @@ -398,10 +350,10 @@ packages: dependency: "direct main" description: name: postgres - sha256: e9802d4c9d78e432c4d4e57be9a91d99cf1552d45f17b17792f8e595060376a2 + sha256: bc3a36f9960d822af1ac4c2e0a32c4e7a3e426d2ce4500c11afca40f53c34612 url: "https://pub.dev" source: hosted - version: "3.4.3" + version: "3.4.0" pub_semver: dependency: transitive description: @@ -451,53 +403,21 @@ packages: source: hosted version: "1.0.3" shelf: - dependency: transitive + dependency: "direct main" description: name: shelf sha256: e7dd780a7ffb623c57850b33f43309312fc863fb6aa3d276a754bb299839ef12 url: "https://pub.dev" source: hosted version: "1.4.2" - shelf_packages_handler: - dependency: transitive - description: - name: shelf_packages_handler - sha256: "89f967eca29607c933ba9571d838be31d67f53f6e4ee15147d5dc2934fee1b1e" - url: "https://pub.dev" - source: hosted - version: "3.0.2" - shelf_static: - dependency: transitive - description: - name: shelf_static - sha256: c87c3875f91262785dade62d135760c2c69cb217ac759485334c5857ad89f6e3 - url: "https://pub.dev" - source: hosted - version: "1.1.3" - shelf_web_socket: - dependency: transitive - description: - name: shelf_web_socket - sha256: "073c147238594ecd0d193f3456a5fe91c4b0abbcc68bf5cd95b36c4e194ac611" - url: "https://pub.dev" - source: hosted - version: "2.0.0" - source_map_stack_trace: - dependency: transitive - description: - name: source_map_stack_trace - sha256: c0713a43e323c3302c2abe2a1cc89aa057a387101ebd280371d6a6c9fa68516b - url: "https://pub.dev" - source: hosted - version: "2.1.2" - source_maps: - dependency: transitive + shelf_router: + dependency: "direct main" description: - name: source_maps - sha256: "708b3f6b97248e5781f493b765c3337db11c5d2c81c3094f10904bfa8004c703" + name: shelf_router + sha256: f5e5d492440a7fb165fe1e2e1a623f31f734d3370900070b2b1e0d0428d59864 url: "https://pub.dev" source: hosted - version: "0.10.12" + version: "1.1.4" source_span: dependency: transitive description: @@ -555,14 +475,6 @@ packages: url: "https://pub.dev" source: hosted version: "1.2.1" - test: - dependency: "direct dev" - description: - name: test - sha256: "713a8789d62f3233c46b4a90b174737b2c04cb6ae4500f2aa8b1be8f03f5e67f" - url: "https://pub.dev" - source: hosted - version: "1.25.8" test_api: dependency: transitive description: @@ -571,14 +483,6 @@ packages: url: "https://pub.dev" source: hosted version: "0.7.3" - test_core: - dependency: transitive - description: - name: test_core - sha256: "12391302411737c176b0b5d6491f466b0dd56d4763e347b6714efbaa74d7953d" - url: "https://pub.dev" - source: hosted - version: "0.6.5" typed_data: dependency: transitive description: @@ -595,14 +499,6 @@ packages: url: "https://pub.dev" source: hosted version: "0.3.0" - vm_service: - dependency: transitive - description: - name: vm_service - sha256: "0968250880a6c5fe7edc067ed0a13d4bae1577fe2771dcf3010d52c4a9d3ca14" - url: "https://pub.dev" - source: hosted - version: "14.3.1" watcher: dependency: transitive description: @@ -615,34 +511,10 @@ packages: dependency: transitive description: name: web - sha256: "97da13628db363c635202ad97068d47c5b8aa555808e7a9411963c533b449b27" - url: "https://pub.dev" - source: hosted - version: "0.5.1" - web_socket: - dependency: transitive - description: - name: web_socket - sha256: "3c12d96c0c9a4eec095246debcea7b86c0324f22df69893d538fcc6f1b8cce83" - url: "https://pub.dev" - source: hosted - version: "0.1.6" - web_socket_channel: - dependency: transitive - description: - name: web_socket_channel - sha256: "9f187088ed104edd8662ca07af4b124465893caf063ba29758f97af57e61da8f" - url: "https://pub.dev" - source: hosted - version: "3.0.1" - webkit_inspection_protocol: - dependency: transitive - description: - name: webkit_inspection_protocol - sha256: "87d3f2333bb240704cd3f1c6b5b7acd8a10e7f0bc28c28dcf14e782014f4a572" + sha256: cd3543bd5798f6ad290ea73d210f423502e71900302dde696f8bff84bf89a1cb url: "https://pub.dev" source: hosted - version: "1.2.1" + version: "1.1.0" yaml: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 973d3bf..b7bc43e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,9 +27,15 @@ dependencies: git: https://github.com/TentacleOrg/Tentacle.git injector: ^4.0.0 + # directly used transitive dependencies dio: any built_collection: any + # api server + shelf: ^1.4.2 + shelf_router: ^1.1.4 + jaguar_jwt: ^3.0.0 + dev_dependencies: lints: ^5.0.0 test: ^1.25.8