From 58e963204d39efb3f71191940eb9cf0e8a1de98d Mon Sep 17 00:00:00 2001 From: Benjamin Cane Date: Thu, 23 Nov 2023 08:23:27 -0700 Subject: [PATCH] Refactoring project structure --- Makefile | 18 ++++++++++-------- config/tarmac.json | 8 ++++---- functions/src/data/fetch/main.go | 4 ++++ functions/src/{handler => data/init}/go.mod | 2 +- functions/src/{handler => data/init}/go.sum | 0 functions/src/{ => data}/init/main.go | 5 +++++ functions/src/data/load/main.go | 7 +++++++ functions/src/handlers/lookup/Makefile | 3 +++ functions/src/handlers/lookup/go.mod | 10 ++++++++++ functions/src/{init => handlers/lookup}/go.sum | 0 .../src/{handler => handlers/lookup}/main.go | 9 +++++++++ .../{handler => handlers/lookup}/main_test.go | 0 functions/src/init/go.mod | 10 ---------- 13 files changed, 53 insertions(+), 23 deletions(-) rename functions/src/{handler => data/init}/go.mod (92%) rename functions/src/{handler => data/init}/go.sum (100%) rename functions/src/{ => data}/init/main.go (88%) create mode 100644 functions/src/handlers/lookup/Makefile create mode 100644 functions/src/handlers/lookup/go.mod rename functions/src/{init => handlers/lookup}/go.sum (100%) rename functions/src/{handler => handlers/lookup}/main.go (82%) rename functions/src/{handler => handlers/lookup}/main_test.go (100%) delete mode 100644 functions/src/init/go.mod diff --git a/Makefile b/Makefile index 2b092a8..c5ff72f 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,16 @@ -## Makefile for Tarmac Example Project - build: - ## Create build directory - mkdir -p functions/build ## Build Init Function - docker run --rm -v `pwd`:/build -w /build/functions/build/init tinygo/tinygo:0.25.0 tinygo build -o /build/functions/build/init.wasm -target wasi /build/functions/src/init/main.go + mkdir -p functions/build/data + docker run --rm -v `pwd`:/build -w /build/functions/build/data/init tinygo/tinygo:0.25.0 tinygo build -o /build/functions/build/data/init.wasm -target wasi /build/functions/src/data/init/main.go ## Build CSV Fetch Function - docker run --rm -v `pwd`:/build -w /build/functions/build/data/fetch tinygo/tinygo:0.25.0 tinygo build -o /build/functions/build/fetch.wasm -target wasi /build/functions/src/data/fetch/main.go + mkdir -p functions/build/data + docker run --rm -v `pwd`:/build -w /build/functions/build/data/fetch tinygo/tinygo:0.25.0 tinygo build -o /build/functions/build/data/fetch.wasm -target wasi /build/functions/src/data/fetch/main.go ## Build CSV Load Function - docker run --rm -v `pwd`:/build -w /build/functions/build/data/load tinygo/tinygo:0.25.0 tinygo build -o /build/functions/build/load.wasm -target wasi /build/functions/src/data/load/main.go + mkdir -p functions/build/data + docker run --rm -v `pwd`:/build -w /build/functions/build/data/load tinygo/tinygo:0.25.0 tinygo build -o /build/functions/build/data/load.wasm -target wasi /build/functions/src/data/load/main.go ## Build HTTP Request Handler Function - docker run --rm -v `pwd`:/build -w /build/functions/build/handler tinygo/tinygo:0.25.0 tinygo build -o /build/functions/build/handler.wasm -target wasi /build/functions/src/handler/main.go + mkdir -p functions/build/handlers + docker run --rm -v `pwd`:/build -w /build/functions/build/handlers/lookup/ tinygo/tinygo:0.25.0 tinygo build -o /build/functions/build/handlers/lookup.wasm -target wasi /build/functions/src/handlers/lookup/main.go .PHONY: tests tests: @@ -18,6 +18,8 @@ tests: mkdir -p coverage go test -v -race -covermode=atomic -coverprofile=coverage/coverage.out ./... go tool cover -html=coverage/coverage.out -o coverage/coverage.html + ## Run tests for the lookup function + $(MAKE) -C functions/src/handlers/lookup tests docker-compose: docker compose up -d mysql diff --git a/config/tarmac.json b/config/tarmac.json index 199a4b0..95115d7 100644 --- a/config/tarmac.json +++ b/config/tarmac.json @@ -4,16 +4,16 @@ "name": "airport-lookup", "functions": { "init": { - "filepath": "/functions/init.wasm" + "filepath": "/functions/data/init.wasm" }, "load": { - "filepath": "/functions/load.wasm" + "filepath": "/functions/data/load.wasm" }, "fetch": { - "filepath": "/functions/fetch.wasm" + "filepath": "/functions/data/fetch.wasm" }, "handler": { - "filepath": "/functions/handler.wasm" + "filepath": "/functions/handlers/lookup.wasm" } }, "routes": [ diff --git a/functions/src/data/fetch/main.go b/functions/src/data/fetch/main.go index cb7d401..be1bc40 100644 --- a/functions/src/data/fetch/main.go +++ b/functions/src/data/fetch/main.go @@ -1,3 +1,7 @@ +/* +This function fetches airport data from a remote CSV file via HTTP and returns it to the application. It is designed +for reuse throughout the application. +*/ package main import ( diff --git a/functions/src/handler/go.mod b/functions/src/data/init/go.mod similarity index 92% rename from functions/src/handler/go.mod rename to functions/src/data/init/go.mod index aac7b5a..d1361ca 100644 --- a/functions/src/handler/go.mod +++ b/functions/src/data/init/go.mod @@ -1,4 +1,4 @@ -module github.com/tarmac-project/example-airport-lookup-go/functions/src/handler +module github.com/tarmac-project/example-airport-lookup-go/functions/src/data/init go 1.21 diff --git a/functions/src/handler/go.sum b/functions/src/data/init/go.sum similarity index 100% rename from functions/src/handler/go.sum rename to functions/src/data/init/go.sum diff --git a/functions/src/init/main.go b/functions/src/data/init/main.go similarity index 88% rename from functions/src/init/main.go rename to functions/src/data/init/main.go index 72918c9..cd3626a 100644 --- a/functions/src/init/main.go +++ b/functions/src/data/init/main.go @@ -1,3 +1,8 @@ +/* +This function executes on startup to verify the database setup and load initial airport data. + +The service will not start until the database is setup and the initial data is loaded. +*/ package main import ( diff --git a/functions/src/data/load/main.go b/functions/src/data/load/main.go index e391176..5291eac 100644 --- a/functions/src/data/load/main.go +++ b/functions/src/data/load/main.go @@ -1,3 +1,10 @@ +/* +The purpose of this function is to download the CSV data (by calling another function), parse it, enrich the data, and +load the contents within the SQL database. + +This function is called multiple times throughout the application. It's called by an "init" function and also set +as a scheduled task by itself. +*/ package main import ( diff --git a/functions/src/handlers/lookup/Makefile b/functions/src/handlers/lookup/Makefile new file mode 100644 index 0000000..2befcf8 --- /dev/null +++ b/functions/src/handlers/lookup/Makefile @@ -0,0 +1,3 @@ +.PHONY: tests +tests: + go test -v -race ./... diff --git a/functions/src/handlers/lookup/go.mod b/functions/src/handlers/lookup/go.mod new file mode 100644 index 0000000..7021dc6 --- /dev/null +++ b/functions/src/handlers/lookup/go.mod @@ -0,0 +1,10 @@ +module github.com/tarmac-project/example-airport-lookup-go/functions/src/handlers/lookup + +go 1.21 + +require ( + github.com/tarmac-project/tarmac/pkg/sdk v0.5.0 + github.com/valyala/fastjson v1.6.4 +) + +require github.com/wapc/wapc-guest-tinygo v0.3.3 // indirect diff --git a/functions/src/init/go.sum b/functions/src/handlers/lookup/go.sum similarity index 100% rename from functions/src/init/go.sum rename to functions/src/handlers/lookup/go.sum diff --git a/functions/src/handler/main.go b/functions/src/handlers/lookup/main.go similarity index 82% rename from functions/src/handler/main.go rename to functions/src/handlers/lookup/main.go index bab37a5..f301b50 100644 --- a/functions/src/handler/main.go +++ b/functions/src/handlers/lookup/main.go @@ -1,3 +1,8 @@ +/* +This function is a lookup request handler function. When a request is received, this function is called and will parse +the incoming request, look up the requested airport from the cache, on cache miss, find the requested data in the +database, and provide it back as a response to the request. +*/ package main import ( @@ -8,10 +13,14 @@ import ( "html" ) +// Function is the main function object that will be initialized +// and called by the Tarmac SDK. type Function struct { tarmac *sdk.Tarmac } +// Handler is the entry point for the function and will be called +// when a request is received. func (f *Function) Handler(payload []byte) ([]byte, error) { // Parse the incoming request lc := fastjson.GetString(payload, "local_code") diff --git a/functions/src/handler/main_test.go b/functions/src/handlers/lookup/main_test.go similarity index 100% rename from functions/src/handler/main_test.go rename to functions/src/handlers/lookup/main_test.go diff --git a/functions/src/init/go.mod b/functions/src/init/go.mod deleted file mode 100644 index 745e685..0000000 --- a/functions/src/init/go.mod +++ /dev/null @@ -1,10 +0,0 @@ -module github.com/tarmac-project/example-airport-lookup-go/functions/src/init - -go 1.21.1 - -require github.com/tarmac-project/tarmac/pkg/sdk v0.5.0 - -require ( - github.com/valyala/fastjson v1.6.4 // indirect - github.com/wapc/wapc-guest-tinygo v0.3.3 // indirect -)