Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter web doesnt recognizes cors #10

Open
Gtomazini opened this issue Aug 19, 2024 · 0 comments
Open

Flutter web doesnt recognizes cors #10

Gtomazini opened this issue Aug 19, 2024 · 0 comments

Comments

@Gtomazini
Copy link

I'm not able to consume the api through Flutter Web (just mobile) because it points out that it doesn't have cors, I tried to implement custom ones but it didn't work, any ideas?

( i even tried to force https but do not work too in the Web version)

My API:


import 'dart:async';
import 'dart:io';

import 'package:backend/api/login_api.dart';
import 'package:backend/api/pendency_api.dart';
import 'package:backend/services/login_service.dart';
import 'package:backend/services/pendency_service.dart';
import 'package:mysql_client/mysql_client.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_cors_headers/shelf_cors_headers.dart';
import 'infra/database/mysql_config.dart';
import 'infra/middleware_interception.dart';

void main(List<String> args) async {
  

  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');
}
  final ip = 'localhost'; //InternetAddress.anyIPv4;

  print("Connecting to mysql server...");

final mysqlConnection = await createMySQLConnection();



var pendencyService = PendencyService(mysqlConnection);
var loginService = LoginService(mysqlConnection);

var cascadeHandler = Cascade()
    .add(PendencyApi(pendencyService).handler)
    .add(LoginApi(loginService).handler)
    .handler;

  final handler = Pipeline()
  .addMiddleware(corsHeaders())
  .addMiddleware(MiddlewareInterception.contenTypeJson)
  .addMiddleware(MiddlewareInterception.cors)
  .addHandler(cascadeHandler);

 
  final port = int.parse(Platform.environment['PORT'] ?? '8080');
  final server = await serve(handler, ip, port, securityContext: getSecurityContext());
  print('Server listening on port ${server.port}');

  setupExitHandlers(server, mysqlConnection);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant