Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
softwarespot committed Nov 23, 2024
0 parents commit a8bee5f
Show file tree
Hide file tree
Showing 47 changed files with 1,609 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ENV=production
SERVER_PORT=<SERVER_PORT>
SERVER_MAX_CONCURRENCY=<SERVER_MAX_CONCURRENCY>
23 changes: 23 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go
on: [push, pull_request]

jobs:
test:
strategy:
matrix:
go-version: [1.23.x]

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Test
run: go test -cover -v ./...
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Taken from URL: https://github.com/github/gitignore/blob/main/Go.gitignore
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

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

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

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
go.work.sum

# env file
.env
.env.production

# Misc
bin/
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See URL: https://hub.docker.com/_/golang
# Use the Go image to build the binary only
FROM golang:1.23.0 AS builder
ENV CGO_ENABLED=0
ENV GOOS=linux
WORKDIR /go/src/public-holidays/
COPY . .

# Overwrite the development .env with the .env.production
COPY .env.production .env

RUN make

# See URL: https://hub.docker.com/_/alpine
# Use this image (~50MB) to run the "public-holidays", as the Go image contains too much bloat,
# which isn't needed for running the application in production and the image which can be uploaded
# to a public/private Docker register is then small
FROM alpine:3.20.3

COPY --from=builder /go/src/public-holidays/bin/* ./
COPY --from=builder /go/src/public-holidays/.env ./
EXPOSE 10000
CMD ["./public-holidays"]
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2024 SoftwareSpot Apps

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# If the "VERSION" environment variable is not set, then use this value instead
VERSION?=1.0.0
TIME=$(shell date +%FT%T%z)
GOVERSION=$(shell go version | awk '{print $$3}' | sed s/go//)

LDFLAGS=-ldflags "\
-X github.com/softwarespot/public-holidays/internal/version.Version=${VERSION} \
-X github.com/softwarespot/public-holidays/internal/version.Time=${TIME} \
-X github.com/softwarespot/public-holidays/internal/version.User=${USER} \
-X github.com/softwarespot/public-holidays/internal/version.GoVersion=${GOVERSION} \
-s \
-w \
"

build:
@echo building to bin/public-holidays
@go build $(LDFLAGS) -o ./bin/public-holidays

test:
go test -cover -v ./...

.PHONY: build test
209 changes: 209 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
# Public holidays

![Go Tests](https://github.com/softwarespot/public-holidays/actions/workflows/go.yml/badge.svg)

This project showcases my proficiency in Go by creating a clear and readable
public holidays API. The primary focus is on writing clean, maintainable code
that effectively demonstrates the logic behind retrieving public holidays for a
country's ISO 3166-1 alpha-2 code.

## GET /holidays/v1/{country}/{year}

This endpoint retrieves a list of public holidays for a specified country and
year. By replacing `{country}` with the two-letter ISO 3166-1 alpha-2 code of
the desired country and `{year}` with the target year, users can access detailed
information about each holiday, including dates, local names and English names.
This API is useful for applications that need to display or utilize holiday data
for various purposes, such as scheduling or planning.

## Hosted by [render.com](https://render.com/)

This API is available at https://public-holidays.onrender.com.

**IMPORTANT**
The instance might be down due to inactivity, therefore, wait about 50s for the instance to be started again.

### Example request

```bash
curl -s "https://public-holidays.onrender.com/holidays/v1/FI/2024" | jq

# or locally

curl -s "http://localhost:10000/holidays/v1/FI/2024" | jq
```

### Response format

The response will be in JSON format and includes an array of holiday objects
with the following details:

- **date**: The date of the holiday.
- **name**: The name of the holiday in the local language.
- **englishName**: The name of the holiday in English.

```json
[
{
"date": "2024-01-01",
"name": "Uudenvuodenpäivä",
"englishName": "New Year's Day"
},
{
"date": "2024-01-06",
"name": "Loppiainen",
"englishName": "Epiphany"
},

...
]
```

## Countries

Here is a list of the supported countries.

| Country | ISO 3166-1 alpha-2 | Wikipedia Link |
| ---------- | ------------------ | -------------------------------------------------------------------------------------- |
| 🇩🇰 Denmark | DK | [Public Holidays in Denmark](https://en.wikipedia.org/wiki/Public_holidays_in_Denmark) |
| 🇫🇮 Finland | FI | [Public Holidays in Finland](https://en.wikipedia.org/wiki/Public_holidays_in_Finland) |
| 🇮🇸 Iceland | IS | [Public Holidays in Iceland](https://en.wikipedia.org/wiki/Public_holidays_in_Iceland) |
| 🇳🇴 Norway | NO | [Public Holidays in Norway](https://en.wikipedia.org/wiki/Public_holidays_in_Norway) |
| 🇸🇪 Sweden | SE | [Public Holidays in Sweden](https://en.wikipedia.org/wiki/Public_holidays_in_Sweden) |

## Prerequisites

- Go 1.23.0 or above
- make (if you want to use the `Makefile` provided)
- Docker

## Dependencies

**IMPORTANT:** No 3rd party dependencies are used.

I could easily use [Cobra](https://github.com/spf13/cobra) (and usually I do,
because it allows me to write powerful CLIs), but I felt it was too much for
such a tiny project. I only ever use dependencies when it's say an adapter for
an external service e.g. Redis, MySQL or Prometheus.

## Setup

1. Create and edit the `.env` (used when developing locally) and `.env.production` (used when deploying to production)

```bash
cp .env.example .env
cp .env.example .env.production
```

## Run not using Docker

```bash
go run .
```

or when using `make`

```bash
make

./bin/public-holidays
```

### Version

Display the version of the application and exit.

```bash
# As text
./bin/public-holidays --version

# As JSON
./bin/public-holidays --json --version
```

### Help

Display the help text and exit.

```bash
./bin/public-holidays --help
```

## Run using Docker

1. Build the Docker image with the tag `public-holidays`.

```bash
docker build -t public-holidays .
```

2. Run the Docker image.

```bash
# Port number should the same as defined in ".env.production"
docker run -p "10000:10000" --rm public-holidays
```

### Version

Display the version of the application and exit.

```bash
# As text
docker run --rm public-holidays --version

# As JSON
docker run --rm public-holidays --json --version
```

### Help

Display the help text and exit.

```bash
docker run --rm public-holidays --help
```

## Tests

Tests are written as [Table-Driven Tests](https://go.dev/wiki/TableDrivenTests).

```bash
go test -cover -v ./...
```

or when using `make`

```bash
make test
```

### Linting

Docker

```bash
docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint:latest golangci-lint run -v --tests=false --disable-all -E durationcheck,errorlint,exhaustive,gocritic,gosimple,ineffassign,misspell,predeclared,revive,staticcheck,unparam,unused,whitespace --max-issues-per-linter=10000 --max-same-issues=10000
```

Local

```bash
golangci-lint run --tests=false --disable-all -E durationcheck,errorlint,exhaustive,gocritic,gosimple,ineffassign,misspell,predeclared,revive,staticcheck,unparam,unused,whitespace --max-issues-per-linter=10000 --max-same-issues=10000
```

## Additional information

This section documents any additional information which might be deemed important for the reviewer.

### Decisions made

- Despite using 1.23.0+ and the `slices` pkg being available, I have opted not
to use it, and instead went for how I've been writing Go code before the
`slices` pkg existed. Although for production code, I have started to use it
where applicable.
- Loosely used https://jsonapi.org/.

### License

The code has been licensed under the [MIT](https://opensource.org/license/mit) license.
28 changes: 28 additions & 0 deletions cmd/execute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"flag"

"github.com/softwarespot/public-holidays/internal/logging"
)

func Execute(_ []string, logger logging.Logger) error {
var showHelp bool
flagBoolVarP(&showHelp, "help", "h", false, "Display the help text and exit")

var showVersion bool
flagBoolVarP(&showVersion, "version", "v", false, "Display the version of the application and exit")

var asJSON bool
flagBoolVarP(&asJSON, "json", "j", false, "Output the result as JSON")
flag.Parse()

if showHelp {
cmdHelp()
return nil
}
if showVersion {
return cmdVersion(asJSON)
}
return cmdServer(logger)
}
18 changes: 18 additions & 0 deletions cmd/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import "fmt"

func cmdHelp() {
helpText := `Usage: ./public-holidays [OPTIONS]
Start the public holidays API.
Options:
-h, --help Show this help text and exit.
-v, --version Display the version of the application and exit.
-j, --json Output the version as JSON.
Examples:
./public-holidays`
fmt.Println(helpText)
}
Loading

0 comments on commit a8bee5f

Please sign in to comment.