Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
madflojo committed Oct 21, 2023
0 parents commit ddb4768
Show file tree
Hide file tree
Showing 20 changed files with 418 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: lint
on:
push:
tags:
- v*
branches:
- actions
- main
pull_request:
jobs:
golangci:
name: golangci
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.21'
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: latest

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
args: -E misspell -E revive -E bodyclose -E errname -E gofmt --timeout=5m

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true then the action will use pre-installed Go.
# skip-go-installation: true

# Optional: if set to true then the action don't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Coverage
coverage/

# No Swapfiles
*.swp

# WASM Module Directory
functions/build

# Ignore .wasm files
*.wasm

# Ignore vendor directories
vendor/
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Contributing to Tarmac

Thank you for considering contributing to Tarmac. The time, skills, and perspectives you contribute to this project are valued.

## How can I contribute?

Bugs, design proposals, feature requests, and questions are all welcome and can be submitted by creating a [Github Issue](https://github.com/tarmac-project/tarmac/issues/new/choose) using one of the templates provided. Please provide as much detail as you can.

Code contributions are welcome as well! In an effort to keep this project tidy, please:

- Use `go mod` to install and lock dependencies
- Use `gofmt` to format code and tests
- Run `go vet -v ./...` to check for any inadvertent suspicious code
- Write and run unit tests when they make sense using `go test`
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Makefile for Tarmac Example Project

build:
## Create build directory
mkdir -p functions/build
## Run TinyGo build via Docker because its easier
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
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

docker-compose:
docker compose up

run: build docker-compose
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Airport Lookup - Example Go Project using Tarmac

This project is a Work in Progress. It is not ready for use and you may find many pushes directly to main, lots of bugs, and very little testing/documentation.

## What is this?

This is an example Go project using [Tarmac](https://github.com/tarmacproject/tarmac) to build an application that provides an Airport lookup API.

Tarmac is a new approach to building applications using Web Assembly (WASM). Tarmac acts as an application framework that lets you focus more on the business logic of your application and less on the infrastructure and boilerplate code.

This project is meant to serve as a practical example of how to use Tarmac to build a real application. Including testing, running locally, leveraging external services, observability, and more.
25 changes: 25 additions & 0 deletions config/tarmac.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"services": {
"airport-lookup": {
"name": "airport-lookup",
"functions": {
"init": {
"filepath": "/functions/init.wasm"
},
"fetch": {
"filepath": "/functions/fetch.wasm"
}
},
"routes": [
{
"type": "init",
"function": "init"
},
{
"type": "function",
"function": "fetch"
}
]
}
}
}
15 changes: 15 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3.8'
services:
airport-lookup-example:
image: tarmac:local
ports:
- 80:8080
environment:
- "APP_ENABLE_TLS=false"
- "APP_LISTEN_ADDR=0.0.0.0:8080"
- "APP_DEBUG=true"
- "APP_TRACE=true"
- "APP_WASM_FUNCTION_CONFIG=/config/tarmac.json"
volumes:
- "./config:/config"
- "./functions/build:/functions"
10 changes: 10 additions & 0 deletions functions/src/data/fetch/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/tarmac-project/example-airport-lookup/functions/src/data/fetch

go 1.21

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
)
8 changes: 8 additions & 0 deletions functions/src/data/fetch/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7 h1:xoIK0ctDddBMnc74udxJYBqlo9Ylnsp1waqjLsnef20=
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7/go.mod h1:YARuvh7BUWHNhzDq2OM5tzR2RiCcN2D7sapiKyCel/M=
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0 h1:QKsEf6SXTYrJM9/B4cNoM4RS3/rzuViJaiutEcdSRZQ=
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0/go.mod h1:UTKYV0QFdkJDgV2sJcnuCujVy49MCd8bgi2JmwviJ6E=
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
github.com/wapc/wapc-guest-tinygo v0.3.3 h1:jLebiwjVSHLGnS+BRabQ6+XOV7oihVWAc05Hf1SbeR0=
github.com/wapc/wapc-guest-tinygo v0.3.3/go.mod h1:mzM3CnsdSYktfPkaBdZ8v88ZlfUDEy5Jh5XBOV3fYcw=
42 changes: 42 additions & 0 deletions functions/src/data/fetch/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"fmt"
"github.com/tarmac-project/tarmac/pkg/sdk"
)

type Function struct {
tarmac *sdk.Tarmac
}

func (f *Function) Handler(_ []byte) ([]byte, error) {
f.tarmac.Logger.Debug("Fetch function initiated, Downloading airports.csv")
rsp, err := f.tarmac.HTTP.Get("https://raw.githubusercontent.com/davidmegginson/ourairports-data/main/airports.csv")
if err != nil {
return []byte(""), fmt.Errorf("failed to get airports.csv: %w", err)
}
f.tarmac.Logger.Trace(fmt.Sprintf("airports.csv downloaded with return code: %d", rsp.StatusCode))

if rsp.StatusCode >= 299 {
f.tarmac.Logger.Error(fmt.Sprintf("airports.csv download failed with return code: %d", rsp.StatusCode))
return []byte(""), fmt.Errorf("failed to get airports.csv: HTTP request returned %d", rsp.StatusCode)
}

return rsp.Body, nil
}

func main() {
var err error

// Initialize Function
f := &Function{}

// Initialize the Tarmac SDK
f.tarmac, err = sdk.New(sdk.Config{
Namespace: "airport-lookup",
Handler: f.Handler,
})
if err != nil {
return
}
}
10 changes: 10 additions & 0 deletions functions/src/handler/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/tarmac-project/example-airport-lookup/functions/src/handler

go 1.21

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
)
8 changes: 8 additions & 0 deletions functions/src/handler/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7 h1:xoIK0ctDddBMnc74udxJYBqlo9Ylnsp1waqjLsnef20=
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7/go.mod h1:YARuvh7BUWHNhzDq2OM5tzR2RiCcN2D7sapiKyCel/M=
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0 h1:QKsEf6SXTYrJM9/B4cNoM4RS3/rzuViJaiutEcdSRZQ=
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0/go.mod h1:UTKYV0QFdkJDgV2sJcnuCujVy49MCd8bgi2JmwviJ6E=
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
github.com/wapc/wapc-guest-tinygo v0.3.3 h1:jLebiwjVSHLGnS+BRabQ6+XOV7oihVWAc05Hf1SbeR0=
github.com/wapc/wapc-guest-tinygo v0.3.3/go.mod h1:mzM3CnsdSYktfPkaBdZ8v88ZlfUDEy5Jh5XBOV3fYcw=
42 changes: 42 additions & 0 deletions functions/src/handler/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"fmt"
"github.com/tarmac-project/tarmac/pkg/sdk"
)

type Function struct {
tarmac *sdk.Tarmac
}

func (f *Function) Handler(payload []byte) ([]byte, error) {
return []byte("Hello World"), fmt.Errorf("not implemented http handler")
// Parse the incoming request

// Lookup the airport from cache

// If not in cache, lookup the airport from the database

// If not previously in cache, add to cache

// If nothing found, return an error

// Return the airport
return nil, nil
}

func main() {
var err error

// Initialize Function
f := &Function{}

// Initialize the Tarmac SDK
f.tarmac, err = sdk.New(sdk.Config{
Namespace: "airport-lookup",
Handler: f.Handler,
})
if err != nil {
return
}
}
10 changes: 10 additions & 0 deletions functions/src/init/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/tarmac-project/example-airport-lookup/functions/src/init

go 1.21

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
)
8 changes: 8 additions & 0 deletions functions/src/init/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7 h1:xoIK0ctDddBMnc74udxJYBqlo9Ylnsp1waqjLsnef20=
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7/go.mod h1:YARuvh7BUWHNhzDq2OM5tzR2RiCcN2D7sapiKyCel/M=
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0 h1:QKsEf6SXTYrJM9/B4cNoM4RS3/rzuViJaiutEcdSRZQ=
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0/go.mod h1:UTKYV0QFdkJDgV2sJcnuCujVy49MCd8bgi2JmwviJ6E=
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
github.com/wapc/wapc-guest-tinygo v0.3.3 h1:jLebiwjVSHLGnS+BRabQ6+XOV7oihVWAc05Hf1SbeR0=
github.com/wapc/wapc-guest-tinygo v0.3.3/go.mod h1:mzM3CnsdSYktfPkaBdZ8v88ZlfUDEy5Jh5XBOV3fYcw=
43 changes: 43 additions & 0 deletions functions/src/init/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"github.com/tarmac-project/tarmac/pkg/sdk"
)

type Function struct {
tarmac *sdk.Tarmac
}

func (f *Function) Handler(_ []byte) ([]byte, error) {
f.tarmac.Logger.Info("Airport raw data download starting")

// Fetch the airport data
_, err := f.tarmac.Function.Call("fetch", []byte(""))
if err != nil {
f.tarmac.Logger.Error(fmt.Sprintf("Failed to fetch airport data", err))
return []byte(""), fmt.Errorf("Failed to fetch airport data: %s", err)
}

// Parse the data

// Update the database

return []byte(""), nil
}

func main() {
var err error

// Initialize Function
f := &Function{}

// Initialize the Tarmac SDK
f.tarmac, err = sdk.New(sdk.Config{
Namespace: "airport-lookup",
Handler: f.Handler,
})
if err != nil {
return
}
}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/tarmac-project/example-airport-lookup

go 1.21

require (
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0 // indirect
github.com/valyala/fastjson v1.6.4 // indirect
github.com/wapc/wapc-guest-tinygo v0.3.3 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0 h1:QKsEf6SXTYrJM9/B4cNoM4RS3/rzuViJaiutEcdSRZQ=
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0/go.mod h1:UTKYV0QFdkJDgV2sJcnuCujVy49MCd8bgi2JmwviJ6E=
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
github.com/wapc/wapc-guest-tinygo v0.3.3 h1:jLebiwjVSHLGnS+BRabQ6+XOV7oihVWAc05Hf1SbeR0=
github.com/wapc/wapc-guest-tinygo v0.3.3/go.mod h1:mzM3CnsdSYktfPkaBdZ8v88ZlfUDEy5Jh5XBOV3fYcw=
33 changes: 33 additions & 0 deletions pkg/data/airport/airport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package airport

type Airport struct {
// Continent is the continent the airport is located in.
Continent string

// Emoji is the emoji of the airport location. (Example: 🇺🇸, 🇨🇦, 🇬🇧, etc.)
Emoji string

// GPSCode is the GPS code of the airport.
GPSCode int

// IATACode is the IATA code of the airport. This is a three letter code and is unique to each airport.
IATACode string

// ISOCountry is the ISO code of the country the airport is located in.
ISOCountry string

// ISORegion is the ISO code of the region the airport is located in.
ISORegion string

// LocalCode is the local code of the airport. This is a three letter code and is unique to each airport.
LocalCode string

// Municipality is the municipality the airport is located in.
Municipality string

// Name is the name of the airport.
Name string

// Status is the type and status of airport (examples: small_airport, heliport, closed, etc.).
Status string
}
Loading

0 comments on commit ddb4768

Please sign in to comment.