Skip to content

Commit

Permalink
Merge pull request #3 from wisdom-oss/dev
Browse files Browse the repository at this point in the history
Stable Release
  • Loading branch information
dr4hcu5-jan authored Jul 17, 2024
2 parents 36e335e + bc1922f commit 4541628
Show file tree
Hide file tree
Showing 29 changed files with 1,019 additions and 315 deletions.
33 changes: 3 additions & 30 deletions .github/workflows/build-docker-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: wisdom-oss/microservice-template
IMAGE_NAME: wisdom-oss/service-groundwater-levels


jobs:
Expand All @@ -26,33 +26,6 @@ jobs:
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Docker buildx
uses: docker/setup-buildx-action@v3

- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v5
- uses: wisdom-oss/actions/docker-build@main
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
registry-password: ${{ secrets.GITHUB_TOKEN }}
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
!go.mod
!go.sum

# allow graphql files
!*.graphql

# allow the following folders
!globals/
!routes/
!routes/
!types/
!graphql/
!config/
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ COPY . /tmp/src
WORKDIR /tmp/src
RUN mkdir -p /tmp/build
RUN go mod download
RUN go build -o /tmp/build/app
RUN go build -tags docker -o /tmp/build/app

FROM docker.io/alpine:latest
COPY --from=build-service /tmp/build/app /service
Expand Down
104 changes: 29 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,80 +1,34 @@
<div align="center">
<img height="150px" src="https://raw.githubusercontent.com/wisdom-oss/brand/main/svg/standalone_color.svg">
<h1>Microservice Template/Example</h1>
<h3>service-example</h3>
<p>📐 A minimal working example for microservices in the WISdoM Architecture</p>
<img src="https://img.shields.io/github/go-mod/go-version/wisdom-oss/microservice-template?style=for-the-badge" alt="Go Lang Version"/>
<h1>Water Levels</h1>
<h3>service-water-levels</h3>
<p>🌊 read collected water levels</p>
<img src="https://img.shields.io/github/go-mod/go-version/wisdom-oss/crawler-nlwkn-water-levels?style=for-the-badge" alt="Go Lang Version"/>
<a href="openapi.yaml">
<img src="https://img.shields.io/badge/Schema%20Version-3.0.0-6BA539?style=for-the-badge&logo=OpenAPI%20Initiative" alt="Open
API Schema Version"/></a>
<img src="https://img.shields.io/badge/Schema%20Version-3.0.0-6BA539?style=for-the-badge&logo=OpenAPI%20Initiative" alt="Open API Schema Version"/>
</a>
</div>

## Using the template
1. Download this archive as `.zip` or `.tar.gz` (whatever you prefer)

2. Extract the downloaded archive to a directory of your choice and remove the
parent folders which may have been created during the download

3. Make sure that your folder now contains at least the following file structure:
```
├── globals
│ ├── connections.go (contains globally available connections)
│ ├── variables.go (contains globally available variables)
├── resources
│ ├── authConfig.json (contains auth config)
│ ├── environment.json (contains the environment setup)
│ ├── errors.json (contains http errors)
│ ├── queries.sql (contains sql queries for the service)
├── routes
│ ├── templates.go (contains three template routes)
├── .gitignore
├── init.go (contains code used during startup)
├── template-service.go (contains the bootstrapping code for the service)
├── go.mod (contains the dependencies of the service)
├── go.sum (is the lockfile for the dependencies)
```

4. **Important** Change the service name

To change the service name, you need to edit the file `globals/variables.go`
which should contain the following line

```go
const ServiceName = "template-service"
```

This line needs to be changed to your service name. This constant is
used in logs and error handling to identify the service.

5. Initialize a Git Repository with `main` as default branch

```shell
git init -b main
```
6. Add all files to the repository

```shell
git add -A
```

7. Commit the template to the repository

```shell
git commit -m "loading wisdom-oss/microservice-template"
```

8. Set up a remote origin for the repository

```shell
git remote add origin <your-remote-url>
```

9. Push the repository to the remote origin

```shell
git push origin main
```

10. :tada: You are now able to develop your new microservice

11. Change the README to the contents you desire in here
> [!NOTE]
> The data made available through this service needs to be crawled regularly.
> A working crawler is available here: [wisdom-oss/crawler-nlwkn-water-levels].
> Otherwise, there will be no data available for the service to display.
>
> [wisdom-oss/crawler-nlwkn-water-levels]: https://github.com/wisdom-oss/crawler-nlwkn-water-levels
> [!IMPORTANT]
> The data provided by this application has been licensed under the
> [Datenlizenz Deutschland — Namensnennung — Version 2.0].
>
> ```
> 2024- NLWKN
> www.nlwkn.niedersachsen.de/opendata
> ```
>
> [Datenlizenz Deutschland — Namensnennung — Version 2.0]: https://www.govdata.de/dl-de/by-2-0

This service enables the access to the water levels collected and stored in your
WISdoM platform.
The recommended crawler is listed in the note above which will ensure a
constant collection of the water levels and management of the available stations.
57 changes: 57 additions & 0 deletions config/defaults.docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//go:build docker

package config

import (
"net/http"
"time"

chiMiddleware "github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httplog"
errorMiddleware "github.com/wisdom-oss/microservice-middlewares/v5/error"
securityMiddleware "github.com/wisdom-oss/microservice-middlewares/v5/security"

"microservice/globals"
)

// This file contains default paths that are used inside the service to load
// resource files.
// The go toolchain automatically uses this file if no build tags have been
// set.
// This should be the case in local development and testing.
// To achieve the behavior in docker containers, supply the build tag "docker"
// and the Docker defaults are used

// Middlewares contains the middlewares used per default in this service.
// To disable single middlewares, please remove the line in which this array
// is used and add the middlewares that shall be used manually to the router
var Middlewares = []func(next http.Handler) http.Handler{
httpLogger(),
chiMiddleware.RequestID,
chiMiddleware.RealIP,
errorMiddleware.Handler,
securityMiddleware.ValidateServiceJWT,
}

// EnvironmentFilePath contains the default file path under which the
// environment configuration file is stored
const EnvironmentFilePath = "./environment.json"

// QueryFilePath contains the default file path under which the
// sql queries are stored
const QueryFilePath = "./queries.sql"

// ListenAddress sets the host on which the microservice listens to incoming
// requests
const ListenAddress = "0.0.0.0"

func httpLogger() func(next http.Handler) http.Handler {
l := httplog.NewLogger(globals.ServiceName)
httplog.Configure(httplog.Options{
JSON: true,
Concise: true,
TimeFieldFormat: time.RFC3339,
})
return httplog.Handler(l)

}
55 changes: 55 additions & 0 deletions config/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build !docker

package config

import (
"net/http"
"time"

chiMiddleware "github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httplog"
errorMiddleware "github.com/wisdom-oss/microservice-middlewares/v5/error"

"microservice/globals"
)

// This file contains default paths that are used inside the service to load
// resource files.
// The go toolchain automatically uses this file if no build tags have been
// set.
// This should be the case in local development and testing.
// To achieve the behavior in docker containers, supply the build tag "docker"
// and the Docker defaults are used

// Middlewares contains the middlewares used per default in this service.
// To disable single middlewares, please remove the line in which this array
// is used and add the middlewares that shall be used manually to the router
var Middlewares = []func(next http.Handler) http.Handler{
httpLogger(),
chiMiddleware.RequestID,
chiMiddleware.RealIP,
errorMiddleware.Handler,
}

// EnvironmentFilePath contains the default file path under which the
// environment configuration file is stored
const EnvironmentFilePath = "./resources/environment.json"

// QueryFilePath contains the default file path under which the
// sql queries are stored
const QueryFilePath = "./resources/queries.sql"

// ListenAddress sets the host on which the microservice listens to incoming
// requests
const ListenAddress = "127.0.0.1"

func httpLogger() func(next http.Handler) http.Handler {
l := httplog.NewLogger(globals.ServiceName)
httplog.Configure(httplog.Options{
JSON: false,
Concise: true,
TimeFieldFormat: time.RFC3339,
})
return httplog.Handler(l)

}
6 changes: 6 additions & 0 deletions embeds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

import _ "embed"

//go:embed schema.graphql
var graphQlSchema string
10 changes: 1 addition & 9 deletions globals/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@ package globals

import (
"github.com/qustavo/dotsql"
wisdomType "github.com/wisdom-oss/commonTypes"
)

// This file contains globally shared variables (e.g., service name, sql queries)

// ServiceName contains the global identifier for the service
const ServiceName = "template-service"
const ServiceName = "groundwater-levels"

// SqlQueries contains the prepared sql queries from the resources folder
var SqlQueries *dotsql.DotSql

// AuthorizationConfiguration contains the configuration of the Authorization
// middleware for this microservice
var AuthorizationConfiguration wisdomType.AuthorizationConfiguration

// Environment contains a mapping between the environment variables and the values
// they were set to. However, this variable only contains the configured environment
// variables
var Environment map[string]string = make(map[string]string)

// Errors contains all errors that have been predefined in the "errors.json" file.
var Errors map[string]wisdomType.WISdoMError = make(map[string]wisdomType.WISdoMError)
26 changes: 9 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,43 @@ module microservice
go 1.22.0

require (
github.com/getkin/kin-openapi v0.124.0
github.com/go-chi/chi/v5 v5.0.12
github.com/georgysavva/scany/v2 v2.1.3
github.com/go-chi/chi/v5 v5.1.0
github.com/go-chi/httplog v0.3.2
github.com/jackc/pgx/v5 v5.5.5
github.com/graph-gophers/graphql-go v1.5.0
github.com/jackc/pgx/v5 v5.6.0
github.com/joho/godotenv v1.5.1
github.com/qustavo/dotsql v1.2.0
github.com/rs/zerolog v1.32.0
github.com/wisdom-oss/commonTypes v1.0.0
github.com/rs/zerolog v1.33.0
github.com/twpayne/go-geom v1.5.4
github.com/twpayne/pgx-geom v0.0.2
github.com/wisdom-oss/commonTypes/v2 v2.0.1
github.com/wisdom-oss/go-healthcheck v1.0.2
github.com/wisdom-oss/microservice-middlewares/v4 v4.0.1
openapi.tanna.dev/go/validator v0.4.0
github.com/wisdom-oss/microservice-middlewares/v5 v5.1.2

)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
github.com/lestrrat-go/httprc v1.0.5 // indirect
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/jwx/v2 v2.0.21 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.20.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 4541628

Please sign in to comment.