Skip to content

Commit

Permalink
Merge pull request #1 from NithinKumaraNT/bq_dockerize
Browse files Browse the repository at this point in the history
Issues noticed while dockerizing the bq connection to flapi
  • Loading branch information
jrosskopf authored Nov 6, 2024
2 parents c4e6d2a + 3dec8cd commit e6fb7e0
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 28 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ jobs:
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile
file: ./docker/development/Dockerfile
push: true
tags: |
ghcr.io/${{ env.REPOSITORY_OWNER }}/flapi:latest
${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && format('ghcr.io/{0}/flapi:latest', env.REPOSITORY_OWNER) || '' }}
${{ github.event_name == 'push' && format('ghcr.io/{0}/flapi:branch-{1}', env.REPOSITORY_OWNER, github.ref_name) || '' }}
${{ github.event_name == 'pull_request' && format('ghcr.io/{0}/flapi:pr-{1}', env.REPOSITORY_OWNER, github.event.number) || '' }}
ghcr.io/${{ env.REPOSITORY_OWNER }}/flapi:${{ github.sha }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ test/python/__pycache__/
.Rhistory
vcpkg_installed/
*.db
*.db.wal
*.db.wal
service-account.json
16 changes: 10 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -O0 -g")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT MINGW)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
endif()
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined -fsanitize-undefined-trap-on-error -Os -DNDEBUG")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined -fsanitize-undefined-trap-on-error")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
endif()

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--as-needed -Wl,--no-undefined")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--as-needed -Wl,--no-undefined")

# Disable link-time optimization
#set(CMAKE_INTERPROCEDURAL_OPTIMIZATION FALSE)
if(WIN32)
set(CMAKE_GENERATOR "Visual Studio 16 2019")
endif()

# Find other dependencies
find_package(Catch2 CONFIG REQUIRED)
Expand Down
6 changes: 0 additions & 6 deletions Dockerfile

This file was deleted.

5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ DEBUG_DIR := $(BUILD_DIR)/Debug
RELEASE_DIR := $(BUILD_DIR)/Release

# Docker image name
DOCKER_IMAGE_NAME := flapi
DOCKER_FILE := docker/development/Dockerfile
DOCKER_IMAGE_NAME := ghcr.io/datazoode/flapi

# Default target
all: debug release
Expand Down Expand Up @@ -61,7 +62,7 @@ run-integration-tests: debug
# Build Docker image
docker: release
@echo "Building Docker image..."
docker build -t $(DOCKER_IMAGE_NAME):latest .
docker build -t $(DOCKER_IMAGE_NAME):latest -f $(DOCKER_FILE) .

# Phony targets
.PHONY: all debug release clean run-debug run-release run-integration-tests docker-build
12 changes: 12 additions & 0 deletions docker/development/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM ubuntu:oracular-20241009

RUN apt-get update && apt-get install -y --no-install-recommends \
libc6 \
libstdc++6 \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY ./build/Release/flapi /app/flapi

ENTRYPOINT ["/app/flapi"]
12 changes: 12 additions & 0 deletions docker/docker-compose-bigquery/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.8'

services:
flapi:
image: ghcr.io/datazoode/flapi:latest
environment:
- GOOGLE_APPLICATION_CREDENTIALS=/app/examples/service-account.json
ports:
- 8080:8080
command: --config /app/examples/flapi.yaml --log-level debug
volumes:
- ../../examples:/app/examples
2 changes: 1 addition & 1 deletion examples/flapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ connections:
bigquery-lakehouse:
# SQL commands to initialize the connection (e.g., loading extensions)
init: |
INSTALL 'bigquery' FROM 'http://storage.googleapis.com/hafenkran';
FORCE INSTALL 'bigquery' FROM 'http://storage.googleapis.com/hafenkran';
LOAD 'bigquery';
# Connection-specific properties (accessible in templates via {{ context.conn.property_name }})
properties:
Expand Down
2 changes: 1 addition & 1 deletion examples/sqls/customers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ auth:
roles: [read]

heartbeat:
enabled: true
enabled: false
params:
id: 123
File renamed without changes.
5 changes: 4 additions & 1 deletion examples/sqls/recommendations/bydevice_cache.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
CREATE TABLE {{cache.schema}}.{{cache.table}} AS
SELECT *
FROM bigquery_scan('d-kaercher-lakehouse.application__sales.krass__recommendations_by_device')
FROM bigquery_query(
'd-kaercher-lakehouse',
'SELECT * FROM `d-kaercher-lakehouse.application__sales.krass__recommendations_by_device`'
)
10 changes: 6 additions & 4 deletions src/config_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ void ConfigManager::loadEndpointConfig(const std::string& config_file) {
YAML::Node endpoint_config = YAML::LoadFile(config_file);
EndpointConfig endpoint;

auto endpoint_dir = std::filesystem::path(config_file).parent_path();

endpoint.urlPath = safeGet<std::string>(endpoint_config, "url-path", "url-path");
endpoint.method = safeGet<std::string>(endpoint_config, "method", "method", "GET");
endpoint.templateSource = safeGet<std::string>(endpoint_config, "template-source", "template-source");
endpoint.templateSource = (endpoint_dir / safeGet<std::string>(endpoint_config, "template-source", "template-source")).string();

CROW_LOG_DEBUG << "\t\tEndpoint: " << endpoint.method << " " << endpoint.urlPath;
CROW_LOG_DEBUG << "\t\tTemplate Source: " << endpoint.templateSource;
Expand All @@ -193,7 +195,7 @@ void ConfigManager::loadEndpointConfig(const std::string& config_file) {
parseEndpointConnection(endpoint_config, endpoint);
parseEndpointRateLimit(endpoint_config, endpoint);
parseEndpointAuth(endpoint_config, endpoint);
parseEndpointCache(endpoint_config, endpoint);
parseEndpointCache(endpoint_config, endpoint_dir, endpoint);
parseEndpointHeartbeat(endpoint_config, endpoint);

endpoints.push_back(endpoint);
Expand Down Expand Up @@ -278,12 +280,12 @@ void ConfigManager::parseEndpointAuth(const YAML::Node& endpoint_config, Endpoin
}
}

void ConfigManager::parseEndpointCache(const YAML::Node& endpoint_config, EndpointConfig& endpoint) {
void ConfigManager::parseEndpointCache(const YAML::Node& endpoint_config, const std::filesystem::path& endpoint_dir, EndpointConfig& endpoint) {
CROW_LOG_DEBUG << "\tParsing endpoint cache configuration";
if (endpoint_config["cache"]) {
auto cache_node = endpoint_config["cache"];
endpoint.cache.cacheTableName = safeGet<std::string>(cache_node, "cache-table-name", "cache.cache-table-name", "");
endpoint.cache.cacheSource = safeGet<std::string>(cache_node, "cache-source", "cache.cache-source", "");
endpoint.cache.cacheSource = (endpoint_dir / safeGet<std::string>(cache_node, "cache-source", "cache.cache-source")).string();
endpoint.cache.refreshTime = safeGet<std::string>(cache_node, "refresh-time", "cache.refresh-time", "");
endpoint.cache.refreshEndpoint = safeGet<bool>(cache_node, "refresh-endpoint", "cache.refresh-endpoint", false);
endpoint.cache.maxPreviousTables = safeGet<std::size_t>(cache_node, "max-previous-tables", "cache.max-previous-tables", 5);
Expand Down
2 changes: 1 addition & 1 deletion src/include/config_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class ConfigManager {
void parseEndpointConnection(const YAML::Node& endpoint_config, EndpointConfig& endpoint);
void parseEndpointRateLimit(const YAML::Node& endpoint_config, EndpointConfig& endpoint);
void parseEndpointAuth(const YAML::Node& endpoint_config, EndpointConfig& endpoint);
void parseEndpointCache(const YAML::Node& endpoint_config, EndpointConfig& endpoint);
void parseEndpointCache(const YAML::Node& endpoint_config, const std::filesystem::path& endpoint_dir, EndpointConfig& endpoint);
void parseGlobalHeartbeatConfig();
void parseEndpointHeartbeat(const YAML::Node& endpoint_config, EndpointConfig& endpoint);

Expand Down
2 changes: 1 addition & 1 deletion src/include/open_api_doc_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <crow.h>
#include "crow/middlewares/cors.h"

#include <algorithm>
#include <yaml-cpp/yaml.h>
#include <memory>

Expand Down
23 changes: 21 additions & 2 deletions src/open_api_doc_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ YAML::Node OpenAPIDocGenerator::generateDoc(crow::App<crow::CORSHandler, RateLim
// Servers
doc["servers"].push_back(YAML::Node());
std::string serverUrl = app.ssl_used() ? "https://" : "http://";
//serverUrl += app.bindaddr().empty() ? "localhost" : app.bindaddr();
serverUrl += "localhost";
serverUrl += ":" + std::to_string(app.port());
doc["servers"][0]["url"] = serverUrl;

// Security Schemes - define both types
doc["components"]["securitySchemes"]["bearerAuth"]["type"] = "http";
doc["components"]["securitySchemes"]["bearerAuth"]["scheme"] = "bearer";
doc["components"]["securitySchemes"]["bearerAuth"]["bearerFormat"] = "JWT";
doc["components"]["securitySchemes"]["bearerAuth"]["description"] = "JWT Authorization header using the Bearer scheme.";

doc["components"]["securitySchemes"]["basicAuth"]["type"] = "http";
doc["components"]["securitySchemes"]["basicAuth"]["scheme"] = "basic";
doc["components"]["securitySchemes"]["basicAuth"]["description"] = "Basic HTTP Authentication";

// Paths
for (const auto& endpoint : configManager->getEndpoints()) {
doc["paths"][endpoint.urlPath] = generatePathItem(endpoint);
Expand All @@ -38,7 +47,10 @@ YAML::Node OpenAPIDocGenerator::generatePathItem(const EndpointConfig& endpoint)
{
YAML::Node pathItem;

// Convert method to lowercase
std::string method = endpoint.method.empty() ? "get" : endpoint.method;
std::transform(method.begin(), method.end(), method.begin(), ::tolower);

YAML::Node operation;

operation["summary"] = "Endpoint for " + endpoint.urlPath;
Expand All @@ -54,8 +66,15 @@ YAML::Node OpenAPIDocGenerator::generatePathItem(const EndpointConfig& endpoint)
operation["x-rate-limit"]["interval"] = endpoint.rate_limit.interval;
}

// Add security requirement based on auth type
if (endpoint.auth.enabled) {
operation["security"][0]["bearerAuth"] = YAML::Node(YAML::NodeType::Sequence);
operation["security"].push_back(YAML::Node());
if (endpoint.auth.type == "basic") {
operation["security"][0]["basicAuth"] = YAML::Node(YAML::NodeType::Sequence);
} else {
// Default to bearer auth for other cases
operation["security"][0]["bearerAuth"] = YAML::Node(YAML::NodeType::Sequence);
}
}

pathItem[method] = operation;
Expand Down

0 comments on commit e6fb7e0

Please sign in to comment.