Skip to content

Commit

Permalink
backend api setup works, auto generate api configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
lbernhard95 committed Oct 31, 2024
1 parent accc605 commit 052fcbd
Show file tree
Hide file tree
Showing 13 changed files with 627 additions and 115 deletions.
113 changes: 27 additions & 86 deletions infrastructure/api/apigw.tf
Original file line number Diff line number Diff line change
@@ -1,96 +1,37 @@
resource "aws_api_gateway_rest_api" "api" {
name = "schafkopf-api"

endpoint_configuration {
types = ["REGIONAL"]
}
resource "aws_apigatewayv2_api" "schafkopf_api" {
name = "schafkopf_api"
protocol_type = "HTTP"
}


resource "aws_api_gateway_domain_name" "api" {
domain_name = local.api_sub_domain
regional_certificate_arn = aws_acm_certificate.certs.arn
endpoint_configuration {
types = ["REGIONAL"]
}
resource "aws_apigatewayv2_integration" "schafkopf_api" {
api_id = aws_apigatewayv2_api.schafkopf_api.id
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.api.invoke_arn
}
/*
resource "aws_api_gateway_base_path_mapping" "api" {
domain_name = aws_api_gateway_domain_name.api.domain_name
stage_name = aws_api_gateway_stage.api.stage_name
api_id = aws_api_gateway_rest_api.api.id
}*/


resource "aws_api_gateway_rest_api_policy" "api_policy" {
rest_api_id = aws_api_gateway_rest_api.api.id

policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Principal = "*", # Change this to specific principals in production
Action = "execute-api:Invoke",
Resource = "${aws_api_gateway_rest_api.api.execution_arn}/*" # Allows access to all resources and methods
}
]
})
resource "aws_apigatewayv2_route" "schafkopf_api" {
api_id = aws_apigatewayv2_api.schafkopf_api.id
route_key = "$default" # Catch-all route
target = "integrations/${aws_apigatewayv2_integration.schafkopf_api.id}"
}


/*resource "aws_api_gateway_deployment" "api" {
rest_api_id = aws_api_gateway_rest_api.api.id
triggers = {
# NOTE: The configuration below will satisfy ordering considerations,
# but not pick up all future REST API changes. More advanced patterns
# are possible, such as using the filesha1() function against the
# Terraform configuration file(s) or removing the .id references to
# calculate a hash against whole resources. Be aware that using whole
# resources will show a difference after the initial implementation.
# It will stabilize to only change when resources change afterwards.
redeployment = sha1(jsonencode([
aws_api_gateway_resource.api.id,
aws_api_gateway_method.api.id,
aws_api_gateway_integration.api.id,
aws_api_gateway_rest_api.api.id
]))
}
depends_on = [
aws_api_gateway_integration.api,
]
}
resource "aws_api_gateway_stage" "api" {
deployment_id = aws_api_gateway_deployment.api.id
rest_api_id = aws_api_gateway_rest_api.api.id
stage_name = "v1"
}*/

resource "aws_api_gateway_resource" "api" {
rest_api_id = aws_api_gateway_rest_api.api.id
parent_id = aws_api_gateway_rest_api.api.root_resource_id
path_part = "{proxy+}"
resource "aws_apigatewayv2_stage" "default_stage" {
api_id = aws_apigatewayv2_api.schafkopf_api.id
name = "$default"
auto_deploy = true
}

resource "aws_api_gateway_method" "api" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.api.id
http_method = "ANY"
authorization = "NONE"

request_parameters = {
"method.request.path.proxy" = true
resource "aws_apigatewayv2_domain_name" "custom_domain" {
domain_name = local.api_sub_domain
domain_name_configuration {
certificate_arn = aws_acm_certificate_validation.cert-validation.certificate_arn
endpoint_type = "REGIONAL"
security_policy = "TLS_1_2"
}
}
/*
resource "aws_api_gateway_integration" "api" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.api.id
http_method = aws_api_gateway_method.api.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.api.invoke_arn
}
*/

resource "aws_apigatewayv2_api_mapping" "custom_domain_mapping" {
api_id = aws_apigatewayv2_api.schafkopf_api.id
domain_name = aws_apigatewayv2_domain_name.custom_domain.domain_name
stage = aws_apigatewayv2_stage.default_stage.name
}
4 changes: 2 additions & 2 deletions infrastructure/api/ecr.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ resource "aws_ecr_lifecycle_policy" "api" {
})
}

/*

data "aws_ecr_image" "api" {
repository_name = aws_ecr_repository.api.name
image_tag = "latest"
}*/
}
14 changes: 6 additions & 8 deletions infrastructure/api/lambda.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*resource "aws_lambda_function" "api" {
resource "aws_lambda_function" "api" {
function_name = "schafkopf_api"
package_type = "Image"
image_uri = "${aws_ecr_repository.api.repository_url}:latest"
Expand All @@ -8,7 +8,7 @@

role = aws_iam_role.api.arn
}
*/

resource "aws_iam_role" "api" {
name = "schafkopf_api"
assume_role_policy = jsonencode({
Expand Down Expand Up @@ -57,12 +57,10 @@ resource "aws_iam_role_policy_attachment" "api" {
policy_arn = aws_iam_policy.api.arn
}

/*
resource "aws_lambda_permission" "wheatley-api" {
statement_id = "AllowExecutionFromAPIGateway"
resource "aws_lambda_permission" "api_gateway_invoke" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.api.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.api.execution_arn}/*"
}
*/
source_arn = "${aws_apigatewayv2_api.schafkopf_api.execution_arn}/*"
}
10 changes: 5 additions & 5 deletions infrastructure/api/route53.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
resource "aws_route53_record" "api" {
zone_id = data.aws_route53_zone.domain.id
name = aws_api_gateway_domain_name.api.domain_name
resource "aws_route53_record" "custom_domain_record" {
zone_id = data.aws_route53_zone.domain.zone_id
name = local.api_sub_domain
type = "A"

alias {
name = aws_api_gateway_domain_name.api.domain_name
zone_id = aws_api_gateway_domain_name.api.regional_zone_id
name = aws_apigatewayv2_domain_name.custom_domain.domain_name_configuration[0].target_domain_name
zone_id = aws_apigatewayv2_domain_name.custom_domain.domain_name_configuration[0].hosted_zone_id
evaluate_target_health = false
}
}
56 changes: 55 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ requests = "^2.32.3"
boto3 = "^1.35.49"
fastapi = "^0.115.4"
mangum = "^0.19.0"
uvicorn = "^0.32.0"


[build-system]
Expand Down
8 changes: 6 additions & 2 deletions schafkopf/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ def hello() -> str:
app.add_middleware(
CORSMiddleware,
allow_origins=[
"*"
"https://schafkopf.lukas-bernhard.de", # your frontend domain
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
)

if __name__ == '__main__':
import uvicorn
uvicorn.run("schafkopf.api.api:app", port=8000, log_level="info", reload=True)
1 change: 1 addition & 0 deletions web/.swagger-codegen-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const baseURL = process.env.REACT_APP_API_URL || 'http://localhost:8000';
6 changes: 4 additions & 2 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"typescript": "^4.4.2",
"swagger-typescript-api": "^13.0.22",
"typescript": "^5.6.3",
"web-vitals": "^2.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
"eject": "react-scripts eject",
"generate-api": "swagger-typescript-api -p http://localhost:8000/openapi.json -o src/api/"
},
"eslintConfig": {
"extends": [
Expand Down
6 changes: 5 additions & 1 deletion web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React from 'react';
import { Button, Typography } from '@mui/material';
import {Api} from "./api/Api";

const App: React.FC = () => {

return (
<div style={{ padding: 20 }}>
<Typography variant="h4" component="h1" gutterBottom>
Welcome to My Material App
</Typography>
<Button variant="contained" color="primary">
<Button variant="contained" color="primary" onClick={() => {
new Api().helloGet().then(msg => console.log(msg))
}}>
Hello Material-UI
</Button>
</div>
Expand Down
Loading

0 comments on commit 052fcbd

Please sign in to comment.