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

[DE-6473] Add feature for trino-client helper rest api #95

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions trino-api/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
DOCKER_DIR := "./deployment/dev"
# Dir where build binaries are generated. The dir should be gitignored
BUILD_OUT_DIR := "bin/"

API_OUT := "bin/api"
API_MAIN_FILE := "cmd/api/main.go"

GOVERSION=$(shell go version)
UNAME_OS=$(shell go env GOOS)
UNAME_ARCH=$(shell go env GOARCH)

GO = go

MODULE = $(shell $(GO) list -m)
SERVICE = $(shell basename $(MODULE))

BIN = $(CURDIR)/bin
PKGS = $(or $(PKG),$(shell $(GO) list ./...))

$(BIN)/%: | $(BIN) ; $(info $(M) building package: $(PACKAGE)…)
tmp=$$(mktemp -d); \
env GOBIN=$(BIN) go install $(PACKAGE) \
|| ret=$$?; \
rm -rf $$tmp ; exit $$ret

$(BIN)/golint: PACKAGE=golang.org/x/lint/golint

GOLINT = $(BIN)/golint

.PHONY: lint
lint: | $(GOLINT) ; $(info $(M) running golint…) @ ## Run golint
$Q $(GOLINT) -set_exit_status $(PKGS)

.PHONY: go-build-api ## Build the binary file for API server
go-build-api:
@CGO_ENABLED=0 GOOS=$(UNAME_OS) GOARCH=$(UNAME_ARCH) go build -v -o $(API_OUT) $(API_MAIN_FILE)

.PHONY: local_dev_rebuild
local_dev_rebuild:
docker-compose -f deployment/dev/docker-compose.yml up -d --build

.PHONY: local_dev_build
# Build the local_dev Docker image
local_dev_build:
docker-compose -f $(DOCKER_DIR)/docker-compose.yml build


.PHONY: local_dev_run
# Run the docker container services
local_dev_run:
docker-compose -f $(DOCKER_DIR)/docker-compose.yml up -d


.PHONY: local_dev_down
# Stop the docker container services
local_dev_down:
docker-compose -f $(DOCKER_DIR)/docker-compose.yml down


.PHONY: local_dev_logs
# View logs for the docker services
local_dev_logs:
docker-compose -f $(DOCKER_DIR)/docker-compose.yml logs -f


.PHONY: local_dev_test
# Run tests inside the API docker container
local_dev_test:
docker-compose -f $(DOCKER_DIR)/docker-compose.yml exec api go test ./...


.PHONY: local_dev_clean
# Clean up Docker container resources
local_dev_clean:
docker-compose -f $(DOCKER_DIR)/docker-compose.yml down --rmi all --volumes --remove-orphans
74 changes: 74 additions & 0 deletions trino-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Trino API Module

## Overview

This module exposes an API to interact with Trino. It retrieves data using the Trino Go client, processes it, and returns it in JSON format to the client.

### Features:
- **Trino Integration:** Connects to a Trino instance and executes SQL queries.
- **Data Processing:** Applies business logic to the data before sending it to the client.
- **JSON API:** Communicates with clients using JSON format.
- **Request/Response Limitation:** Throws an error if response data exceeds 5,000 records.
- **Configurable:** Uses TOML files for different environments (development, staging, production).

## Project Structure

```plaintext
trino-api/
├── build/
│ ├── docker/
│ ├── dev/
│ │ ├── Dockerfile.api
│ ├── entrypoint.sh
├── cmd/
│ ├── api/
│ ├── main.go
├── config/
│ ├── default.toml
│ ├── dev_docker.toml
│ ├── stage.toml
│ ├── prod.toml
├── deployment/
│ ├── dev/
│ ├── docker-compose.yml
├── internal/
│ ├── app/
│ │ ├── handler/
│ │ │ ├── handler.go
│ │ │ ├── handler_test.go
│ │ ├── process/
│ │ │ ├── processor.go
│ │ ├── routes/
│ │ │ ├── routes.go
│ │ ├── app.go
│ │
│ ├── config/
│ │ ├── config.go
│ ├── model/
│ │ ├── model.go
│ ├── utils/
│ │ ├── response.go
│ ├── services/
│ │ ├── trino/
│ │ ├── client.go
│ └── boot/
│ ├── boot.go
├── pkg/
│ ├── config/
│ ├── config.go
│ ├── config_test.go
├── go.mod
├── go.sum
├── Makefile
├── README.md
Empty file added trino-api/bin/.gitkeep
Empty file.
Binary file added trino-api/bin/golint
Binary file not shown.
41 changes: 41 additions & 0 deletions trino-api/build/docker/dev/Dockerfile.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Use a lightweight Go image
FROM golang:1.21.5-alpine AS builder

ENV CGO_ENABLED 0

RUN mkdir /src

WORKDIR /src

RUN apk add --update --no-cache --repository https://dl-4.alpinelinux.org/alpine/latest-stable/community/ make

ADD Makefile /src

COPY go.mod .
COPY go.sum .

RUN go mod download

COPY . /src/

RUN make go-build-api

# stage 2

FROM alpine:latest

COPY --from=builder /src/bin/api /app/
COPY --from=builder /src/config/ /app/config/
COPY build/docker/entrypoint.sh /app/

ENV WORKDIR=/app
ENV DUMB_INIT_SETSID=0
WORKDIR /app

RUN apk add --update --no-cache dumb-init su-exec ca-certificates curl

EXPOSE 8000

RUN chmod +x entrypoint.sh

ENTRYPOINT ["./api"]
28 changes: 28 additions & 0 deletions trino-api/build/docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/dumb-init /bin/sh
set -euo pipefail

start_application()
{
su-exec appuser $WORKDIR/"$appName" &

# Get pid for app
APP_PID=$!

# wait returns immediately after signal is caught,
# hence double wait is required in shutdown_application
# refer : http://veithen.io/2014/11/16/sigterm-propagation.html
wait "$APP_PID"
}

shutdown_application()
{
kill -s SIGTERM "$APP_PID"
trap - SIGTERM SIGINT
wait "$APP_PID"
EXIT_STATUS=$?
return ${EXIT_STATUS}
}

appName="$1"
trap shutdown_application SIGTERM SIGINT
start_application
39 changes: 39 additions & 0 deletions trino-api/cmd/api/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"context"
"fmt"
"net/http"
"strconv"
"trino-api/internal/app"
"trino-api/internal/boot"

log "github.com/sirupsen/logrus"
)

func main() {
//Initialize context
ctx, cancel := context.WithCancel(boot.NewContext(context.Background()))
defer cancel()

// env := boot.GetEnv()
// err := boot.InitApi(ctx, env)
// if err != nil {
// log.Fatalf("failed to inti api: %v", err)
// }
// boot.InitTracing()
boot.Init()
// logger := boot.InitLogger(ctx)
// logger := log
log.Debug(ctx)

app, err := app.NewApp(boot.Config.Db.DSN)
if err != nil {
// logger.Fatal(fmt.Sprintf("Failed to initialize app: %v", err))
log.Fatalf(fmt.Sprintf("Failed to initialize app: %v", err))
}
defer app.Close()

log.Info(fmt.Sprintf("Starting server on: %d", boot.Config.App.Port))
log.Fatal(fmt.Sprint(http.ListenAndServe(":"+strconv.Itoa(boot.Config.App.Port), app.Router)))
}
24 changes: 24 additions & 0 deletions trino-api/config/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[app]
appEnv = "default"
serviceName = "trino-client"
hostname = "trino.client.razorpay.com"
port = 8000
metricsPort = 8080
shutdownTimeout = 2
shutdownDelay = 2

[db]
dialect = "trino"
protocol = "tcp"
url = "localhost"
port = 8080
username = "trino-client"
password = "trino-client"
catalog = "trino-client-catalog"
schema = "trino-client-schema"
DSN = "http://user@localhost:8080?catalog=default&schema=public"

[auth]
[auth.token]
api = "1234"
debug = "4567"
24 changes: 24 additions & 0 deletions trino-api/config/dev_docker.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[app]
appEnv = "default"
serviceName = "trino-client"
hostname = "trino.client.com"
port = 8000
metricsPort = 8080
shutdownTimeout = 2
shutdownDelay = 2

[db]
dialect = "trino"
protocol = "tcp"
url = "127.0.0.1"
port = 8080
username = "trino-client"
password = "trino-client"
catalog = "trino-client-catalog"
schema = "trino-client-schema"
DSN = "http://user@trino_local:8080?catalog=default&schema=public"

[auth]
[auth.token]
api = "1234"
debug = "4567"
Empty file added trino-api/config/prod.toml
Empty file.
Empty file added trino-api/config/stage.toml
Empty file.
33 changes: 33 additions & 0 deletions trino-api/deployment/dev/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
services:
trino:
image: trinodb/trino:latest
container_name: trino_local
ports:
- "8080:8080"
environment:
JAVA_TOOL_OPTIONS: "-Xmx4g"
networks:
- app-network

api:
build:
context: ../../
dockerfile: build/docker/dev/Dockerfile.api
ports:
- "8000:8000"
container_name: trino_api
networks:
- app-network
environment:
- APP_ENV=dev_docker
depends_on:
- trino
healthcheck:
test: ["CMD", "wget", "--spider", "-q", "http://localhost:8000/api/v1/health"]
interval: 30s
timeout: 10s
retries: 3

networks:
app-network:
driver: bridge
Loading
Loading