这是 Dart 为使用Shelf
package 的服务器提供的典型代码:
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)。我是否需要为主机中运行的每个服务进行配置?