Skip to content

Commit

Permalink
Refactoring project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
madflojo committed Nov 23, 2023
1 parent 346ce17 commit 58e9632
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 23 deletions.
18 changes: 10 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
## 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:
## Run 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
Expand Down
8 changes: 4 additions & 4 deletions config/tarmac.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
4 changes: 4 additions & 0 deletions functions/src/data/fetch/main.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
7 changes: 7 additions & 0 deletions functions/src/data/load/main.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
3 changes: 3 additions & 0 deletions functions/src/handlers/lookup/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.PHONY: tests
tests:
go test -v -race ./...
10 changes: 10 additions & 0 deletions functions/src/handlers/lookup/go.mod
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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")
Expand Down
File renamed without changes.
10 changes: 0 additions & 10 deletions functions/src/init/go.mod

This file was deleted.

0 comments on commit 58e9632

Please sign in to comment.