2

这是 Dart 为使用Shelfpackage 的服务器提供的典型代码:

import 'dart:io';

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';

// Configure routes.
final _router = Router()
  ..get('/', _rootHandler)
  ..get('/echo/<message>', _echoHandler);

Response _rootHandler(Request req) {
  return Response.ok('Hello, World!\n');
}

Response _echoHandler(Request request) {
  final message = request.params['message'];
  return Response.ok('$message\n');
}

void main(List<String> args) async {
  // Use any available host or container IP (usually `0.0.0.0`).
  final ip = InternetAddress.anyIPv4;

  // Configure a pipeline that logs requests.
  final _handler = Pipeline().addMiddleware(logRequests()).addHandler(_router);

  // For running in containers, we respect the PORT environment variable.
  final port = int.parse(Platform.environment['PORT'] ?? '8080');
  final server = await serve(_handler, ip, port);
  print('Server listening on port ${server.port}');
}

需要修改什么/如何支持安全连接(HTTPS) ?

我不太了解证书,所以如果你能详细解释如何生成/购买它们以及如何将它们链接到服务器或任何假人参考,那就太好了。

PS:我的主机是运行以下 Docker 容器的 Linux 发行版(Manjaro):文件服务器(Dart with Shelf)和 GraphQL 服务器(Postgraphile)。我是否需要为主机中运行的每个服务进行配置?

4

2 回答 2

2

首先,感谢@KevinMoore 的回答,但它并不如我所愿,所以这是我的回答:

根据Dart 的帮助,上面的代码修改如下:

import 'dart:io';

import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';

// Configure routes.
final _router = Router()
  ..get('/', _rootHandler)
  ..get('/echo/<message>', _echoHandler);

Response _rootHandler(Request req) {
  return Response.ok('Hello, World!\n');
}

Response _echoHandler(Request request) {
  final message = request.params['message'];
  return Response.ok('$message\n');
}

SecurityContext getSecurityContext() { // Bind with a secure HTTPS connection
  final chain = Platform.script.resolve('certificates/server_chain.pem').toFilePath();
  final key = Platform.script.resolve('certificates/server_key.pem').toFilePath();
  
  return SecurityContext()
  ..useCertificateChain(chain)
  ..usePrivateKey(key, password: 'dartdart');
}

void main(List<String> args) async {
  // Use any available host or container IP (usually `0.0.0.0`).
  final ip = InternetAddress.anyIPv4;

  // Configure a pipeline that logs requests.
  final _handler = Pipeline().addMiddleware(logRequests()).addHandler(_router);

  // For running in containers, we respect the PORT environment variable.
  final port = int.parse(Platform.environment['PORT'] ?? '443');
  final server = await serve(_handler, ip, port, securityContext: getSecurityContext());
  print('Server listening on port ${server.port}');
}

对于该代码,我使用 Dart Team 在其存储库中提供的证书和密码仅用于教育目的。

如果您在生产中需要服务器证书,您可以使用Let's Encrypt免费获取它们。

这些视频对于如何获取和使用这些证书非常有用:

本教程(日文)也是一个很好的参考。

于 2021-11-17T02:05:43.510 回答
1

你见过https://api.dart.dev/dev/2.16.0-7.0.dev/dart-io/HttpServer-class.html吗?

您可以在 pkg:shelf 中将HttpServer实例传递给serveRequestshttps://pub.dev/documentation/shelf/latest/shelf_io/serveRequests.html )

于 2021-11-11T23:07:08.090 回答