serverHandler function

Future<Response> serverHandler(
  1. Request request
)

Implementation

Future<shelf.Response> serverHandler(shelf.Request request) async {
  final session = request.headers['x-session'];
  final studyId = request.headers['x-study-id'];
  if (session == null) {
    return shelf.Response(400, body: 'Bad Request. x-session header is missing.');
  }
  if (studyId == null) {
    return shelf.Response(400, body: 'Bad Request. x-study-id header is missing.');
  }

  print('Recovering Supabase session...');
  final res = await env.client.auth.recoverSession(session);
  print('Recovered Supabase session.');
  if (res.error != null) {
    print(res.error?.message);
    return shelf.Response.forbidden('Could not authenticate with X-Session. Error: ${res.error!.message}');
  }

  if (env.client.auth.session() == null) {
    return shelf.Response(500, body: 'Server Error. Session could not be established.');
  }

  if (env.client.auth.session()!.providerToken == null) {
    return shelf.Response(500, body: 'Server Error. GitLab token is not set.');
  }

  final gl = GitlabClient(env.client.auth.session()!.providerToken!);

  switch (request.url.pathSegments[0]) {
    case 'generate':
      await generateRepo(gl, studyId);
      return shelf.Response.ok('Generated repo');
    case 'update':
      final projectId = request.headers['x-project-id'];
      if (projectId == null) {
        return shelf.Response(400, body: 'Bad Request. x-project-id header is missing.');
      }
      await updateRepo(gl, projectId, studyId);
      return shelf.Response.ok('Updated repo');
    default:
      return shelf.Response.ok('Request for "${request.url}" did not match any know routes.');
  }
}