pf-demo

module
v0.0.0-...-63bdbfa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2021 License: Apache-2.0

README

Blacklister

License Go Report Card

This application does the following:

  • It responds to the URL like 'http://host/?n=x' and returns n*n.
  • It responds to the URL 'http://host/blacklisted' with conditions:
    • return error code 444 to the visitor
    • block the IP of the visitor
    • send an email with IP address to "test@domain.com"
    • insert into PostgreSQL table information: path, IP address of the visitor and datetime when he got blocked

Dependencies

  • Docker
  • Go-migrate
  • Golang
  • Helm
  • Kind
  • Kubectl
  • Make

You can check dependency versions in .tool-versions file.

Install dependencies

Suggested way to install dependencies is to use 'asdf' tool (except 'docker' and 'make'). If you have the tool installed, run:

# make sure all relevant plugins are installed
cat .tool-versions |\
  while read p; do
    asdf plugin add ${p%% *}
  done

# install dependencies
asdf install

Check docker manual for docker installation instructions: get docker.

Running

IMPORTANT: Make sure you have all dependencies available (see instructions above).

Use the following command to run the application:

make all

This will perform the following actions:

  • Start a local Kubernetes cluster using Kind
  • Start a local Docker registry
  • Build Docker images and push them to local registry
  • Deploy Helm chart
Clean-up

You can use this helper to remove Helm release and Kind cluster:

make delete-all

Using application

  1. Calculate the square of a number:
curl -XPOST http://localhost:8080/?n=2
  1. Blacklist your IP:
curl -XGET http://localhost:8080/blacklisted
  1. Get logs:
kubectl logs --context kind-dev -n dev-blacklister -l 'app.kubernetes.io/name=blacklister' -f

Development

You can run the application locally and use the database from the local Kind cluster. To do that you will need to set DB_HOST to localhost and get the DB credentials from secret:

export DB_HOST=localhost
export DB_USER="$(kubectl get secret --context kind-dev -n dev-blacklister "blacklister-blacklister-writer-user.ops-dev-blacklister-db.credentials.postgresql.acid.zalan.do" -o go-template='{{.data.username|base64decode}}')"
export DB_PASSWORD="$(kubectl get secret  --context kind-dev -n dev-blacklister "blacklister-blacklister-writer-user.ops-dev-blacklister-db.credentials.postgresql.acid.zalan.do" -o go-template='{{.data.password|base64decode}}')"

# Run the application
./build/blacklister-darwin-amd64 -log-level debug -dev -listen-address "0.0.0.0:8081"

Here is a list of helpers to aid development process:

  • make migrate-up - Run DB migrations
  • make migrate-down - Rollback last DB migration
  • make migrate-drop - Drop all migrations
  • make psql - Login into the database with psql (must have psql installed)
  • make build-all - Build code locally
  • make test - Run tests
  • make docker - Build docker images and push to local registry
  • make deploy - Deploy Helm chart
  • make help - Display help message

You can also access code reference documentation with: make godoc - this will start a local godoc webserver (see link in the command output).

Versioning

Please maintain application version in VERSION file. Use semantic versioning.

Directories

Path Synopsis
pkg
api
Package api sets up the HTTP server, configures middleware and routes
Package api sets up the HTTP server, configures middleware and routes
api/handler
Package handler implements handlers for HTTP routes
Package handler implements handlers for HTTP routes
context
Package context contains custom context value ids and context-related functions
Package context contains custom context value ids and context-related functions
db
Package db contains db connection logic, data accessor functions and model
Package db contains db connection logic, data accessor functions and model
mailer
Package mailer contains mail operations
Package mailer contains mail operations
util
Package util contains generic helper functions
Package util contains generic helper functions

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL