Skip to content

Commit

Permalink
chore: reusable workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
junghao committed Jun 26, 2024
1 parent df8b85f commit 99219bf
Show file tree
Hide file tree
Showing 29 changed files with 843 additions and 97 deletions.
79 changes: 29 additions & 50 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,35 @@ jobs:
- name: check output
run: |
jq . <<< '${{ steps.set.outputs.matrix }}'
build-app:
uses: GeoNet/Actions/.github/workflows/reusable-go-apps.yml@main
with:
buildSetup: |
sudo apt-get -yq update
sudo apt-get install -y xsltproc
testSetup: |
sudo apt-get -yq update
sudo apt-get install -y xsltproc
docker \
run -d \
-p 5432:5432 \
-e POSTGRES_PASSWORD=test \
-e POSTGRES_USER=fdsn_w \
-e POSTGRES_DB=fdsn \
--name postgres \
docker.io/postgis/postgis:15-3.3-alpine
echo "Waiting until Postgres is ready..."
until nc -zv -w 1 127.0.0.1 5432; do
sleep 1s
done
sleep 5s
docker logs postgres
echo "Postgres is ready"
psql postgresql://fdsn_w:[email protected]/fdsn --file=./etc/ddl/drop-create.ddl
psql postgresql://fdsn_w:[email protected]/fdsn --file=./etc/ddl/create-users.ddl
goTestExtraArgs: -p 1
build:
needs: prepare
needs: [prepare, build-app]
strategy:
matrix: ${{ fromJSON(needs.prepare.outputs.matrix) }}
uses: GeoNet/Actions/.github/workflows/reusable-docker-build.yml@main
Expand Down Expand Up @@ -66,52 +93,4 @@ jobs:
aws-region: ap-southeast-2
aws-role-arn-to-assume: arn:aws:iam::862640294325:role/github-actions-geonet-ecr-push
aws-role-duration-seconds: "3600"
go-build:
if: ${{ contains(fromJSON('["workflow_call", "push", "pull_request"]'), github.event_name) && startsWith(github.repository, 'GeoNet/') != false }}
uses: GeoNet/Actions/.github/workflows/reusable-go-build-smoke-test.yml@main
with:
paths: ${{ inputs.paths }}
gofmt:
if: ${{ contains(fromJSON('["workflow_call", "push", "pull_request"]'), github.event_name) && startsWith(github.repository, 'GeoNet/') != false }}
uses: GeoNet/Actions/.github/workflows/reusable-gofmt.yml@main
golangci-lint:
if: ${{ contains(fromJSON('["workflow_call", "push", "pull_request"]'), github.event_name) && startsWith(github.repository, 'GeoNet/') != false }}
uses: GeoNet/Actions/.github/workflows/reusable-golangci-lint.yml@main
go-vet:
if: ${{ contains(fromJSON('["workflow_call", "push", "pull_request"]'), github.event_name) && startsWith(github.repository, 'GeoNet/') != false }}
uses: GeoNet/Actions/.github/workflows/reusable-go-vet.yml@main
go-test:
runs-on: ubuntu-latest
env:
AWS_REGION: ap-southeast-2
steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- uses: actions/setup-go@fac708d6674e30b6ba41289acaab6d4b75aa0753 # v4.0.1
with:
go-version-file: go.mod
cache-dependency-path: go.sum
check-latest: true
- name: setup
run: |
sudo apt-get -yq update
sudo apt-get install -y xsltproc
docker \
run -d \
-p 5432:5432 \
-e POSTGRES_PASSWORD=test \
-e POSTGRES_USER=fdsn_w \
-e POSTGRES_DB=fdsn \
--name postgres \
docker.io/postgis/postgis:15-3.3-alpine
echo "Waiting until Postgres is ready..."
until nc -zv -w 1 127.0.0.1 5432; do
sleep 1s
done
sleep 5s
docker logs postgres
echo "Postgres is ready"
psql postgresql://fdsn_w:[email protected]/fdsn --file=./etc/ddl/drop-create.ddl
psql postgresql://fdsn_w:[email protected]/fdsn --file=./etc/ddl/create-users.ddl
- name: test
run: |
./all.sh

3 changes: 2 additions & 1 deletion cmd/fdsn-holdings-consumer/data_holdings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"database/sql"
"github.com/GeoNet/fdsn/internal/holdings"
"testing"
"time"

"github.com/GeoNet/fdsn/internal/holdings"
)

func TestSaveHoldings(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/fdsn-holdings-consumer/log.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"github.com/GeoNet/kit/metrics"
"log"
"os"

"github.com/GeoNet/kit/metrics"
)

var Prefix string
Expand Down
3 changes: 2 additions & 1 deletion cmd/fdsn-quake-consumer/log.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"github.com/GeoNet/kit/metrics"
"log"
"os"

"github.com/GeoNet/kit/metrics"
)

var Prefix string
Expand Down
3 changes: 2 additions & 1 deletion cmd/fdsn-ws/data_holdings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package main

import (
"database/sql"
"github.com/GeoNet/fdsn/internal/fdsn"
"time"

"github.com/GeoNet/fdsn/internal/fdsn"
)

type metric struct {
Expand Down
7 changes: 4 additions & 3 deletions cmd/fdsn-ws/data_holdings_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"github.com/GeoNet/fdsn/internal/fdsn"
"github.com/GeoNet/fdsn/internal/holdings"
"github.com/lib/pq"
"log"
"testing"
"time"

"github.com/GeoNet/fdsn/internal/fdsn"
"github.com/GeoNet/fdsn/internal/holdings"
"github.com/lib/pq"
)

// http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
Expand Down
2 changes: 1 addition & 1 deletion cmd/fdsn-ws/fdsn_dataselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type dataSelect struct {
keys []string
}

func init() {
func initDataselectTemplate() {
// Handle comma separated parameters (eg: net, sta, loc, cha, etc)
decoder.RegisterConverter([]string{}, func(input string) reflect.Value {
return reflect.ValueOf(strings.Split(input, ","))
Expand Down
2 changes: 1 addition & 1 deletion cmd/fdsn-ws/fdsn_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ var validEventTypes = strings.Split(
", ", ","),
",") // remove spaces after comma

func init() {
func initEventTemplate() {
var err error
var b bytes.Buffer

Expand Down
9 changes: 5 additions & 4 deletions cmd/fdsn-ws/fdsn_station.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ var (
fdsnStationWadlFile []byte
fdsnStationIndex []byte
fdsnStations fdsnStationObj
emptyDateTime time.Time
emptyDateTime = time.Date(9999, 1, 1, 0, 0, 0, 0, time.UTC)
errNotModified = fmt.Errorf("Not modified.")
s3Bucket string
s3Meta string
)

func init() {
func initStationTemplate() {
var err error
var b bytes.Buffer

Expand All @@ -130,9 +130,10 @@ func init() {
if err != nil {
log.Printf("error reading assets/fdsn-ws-station.html: %s", err.Error())
}
}

emptyDateTime = time.Date(9999, 1, 1, 0, 0, 0, 0, time.UTC)

func initStationXML() {
var err error
s3Bucket = os.Getenv("STATION_XML_BUCKET")
s3Meta = os.Getenv("STATION_XML_META_KEY")

Expand Down
32 changes: 19 additions & 13 deletions cmd/fdsn-ws/fdsn_station_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ import (
// NOTE: To run the test, please export :
// STATION_XML_META_KEY=fdsn-station-test.xml

func init() {
}

func TestStationFilter(t *testing.T) {
var e fdsnStationV1Search
var err error

setup(t)
defer teardown()
// Filter test
var v url.Values = make(map[string][]string)

Expand Down Expand Up @@ -207,13 +206,16 @@ Testdata timeline
Station 1:
2007-05-20T23 2011-03-06T22 2011-06-20T04
|------3 cha----|
|-----3 cha----|
|-----3 cha----------->
|------3 cha----|
|-----3 cha----|
|-----3 cha----------->
Station 2:
2010-03-11T21 2012-01-19T22
|-----3 cha----|
|-----3 cha------->
2010-03-11T21 2012-01-19T22
|-----3 cha----|
|-----3 cha------->
*/
func TestStartEnd(t *testing.T) {
var e fdsnStationV1Search
Expand Down Expand Up @@ -408,10 +410,11 @@ NZ|ARAZ|-38.627690|176.120060|420.000000|Aratiatia Landcorp Farm|2007-05-20T23:0
}

// To profiling, you'll have to use full fdsn-station xml as data source:
// 1. Put full fdsn-station.xml in etc/.
// 2. export FDSN_STATION_XML_META_KEY=fdsn-station.xml
// 3. Run `go test -bench=StationQuery -benchmem -run=^$`.
// Note: You must specify -run=^$ to skip test functions since you're not using test fdsn-station xml.
// 1. Put full fdsn-station.xml in etc/.
// 2. export FDSN_STATION_XML_META_KEY=fdsn-station.xml
// 3. Run `go test -bench=StationQuery -benchmem -run=^$`.
// Note: You must specify -run=^$ to skip test functions since you're not using test fdsn-station xml.
//
// Currently the benchmark result for my MacBookPro 2017 is:
// BenchmarkStationQuery/post-4 20000 74472 ns/op 78376 B/op 706 allocs/op
func BenchmarkStationQuery(b *testing.B) {
Expand Down Expand Up @@ -585,6 +588,9 @@ func TestDoFilter(t *testing.T) {
var query url.Values
var hasValue bool

setup(t)
defer teardown()

//
// basic case
//
Expand Down
2 changes: 2 additions & 0 deletions cmd/fdsn-ws/fdsn_station_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ type NetworkType struct {
}

// May be one of NOMINAL, CALCULATED
//
//nolint:deadcode,unused // Struct based on FDSN spec so keep it
type NominalType string

Expand Down Expand Up @@ -364,6 +365,7 @@ type SampleRateType struct {
}

// A time value in seconds.
//
//nolint:deadcode,unused // Struct based on FDSN spec so keep it
type SecondType struct {
Value float64 `xml:",chardata"`
Expand Down
5 changes: 3 additions & 2 deletions cmd/fdsn-ws/log.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
"github.com/GeoNet/kit/metrics"
"github.com/GeoNet/kit/weft"
"log"
"os"

"github.com/GeoNet/kit/metrics"
"github.com/GeoNet/kit/weft"
)

var Prefix string
Expand Down
2 changes: 1 addition & 1 deletion cmd/fdsn-ws/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

var mux *http.ServeMux

func init() {
func initRoutes() {
mux = http.NewServeMux()

mux.HandleFunc("/", weft.MakeHandler(weft.NoMatch, weft.TextError))
Expand Down
5 changes: 3 additions & 2 deletions cmd/fdsn-ws/routes_integration_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//go:build integration
// +build integration

package main

import (
wt "github.com/GeoNet/kit/weft/wefttest"
"testing"

wt "github.com/GeoNet/kit/weft/wefttest"
)

var routesIntegration = wt.Requests{
Expand All @@ -19,7 +21,6 @@ var routesIntegration = wt.Requests{
// Run using:
// go test -tags integration -v -run TestRoutesIntegration
func TestRoutesIntegration(t *testing.T) {
setup(t)
setup(t)
defer teardown()

Expand Down
3 changes: 2 additions & 1 deletion cmd/fdsn-ws/sc3ml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"bytes"
"net/http"

"github.com/GeoNet/fdsn/internal/valid"
"github.com/GeoNet/kit/weft"
"net/http"
)

func s3ml(r *http.Request, h http.Header, b *bytes.Buffer) error {
Expand Down
20 changes: 11 additions & 9 deletions cmd/fdsn-ws/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ import (
)

var (
db *sql.DB
decoder = schema.NewDecoder() // decoder for URL queries.
S3_BUCKET string // the S3 bucket storing the miniseed files used by dataselect
LOG_EXTRA bool // Whether POST body is logged.
zeroDateTime time.Time
db *sql.DB
decoder = schema.NewDecoder() // decoder for URL queries.
S3_BUCKET string // the S3 bucket storing the miniseed files used by dataselect
LOG_EXTRA bool // Whether POST body is logged.
)

var stationVersion = "1.1"
var eventVersion = "1.2"
var dataselectVersion = "1.1"

func init() {
zeroDateTime = time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
}
var zeroDateTime = time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)

func main() {
var err error
Expand Down Expand Up @@ -60,6 +56,12 @@ func main() {
log.Println("ERROR: problem pinging DB - is it up and contactable? 500s will be served")
}

initDataselectTemplate()
initEventTemplate()
initStationTemplate()
initStationXML()
initRoutes()

setupStationXMLUpdater()

log.Println("starting server")
Expand Down
Loading

0 comments on commit 99219bf

Please sign in to comment.