Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
castus committed Oct 20, 2023
1 parent b5b85ae commit 36bdca3
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/distribute.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
on:
workflow_dispatch:
push:
branches:
- main

name: Build and deploy to Docker Hub

jobs:
deploy:
name: Build
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: setup-docker
uses: docker-practice/[email protected]

- name: Set up QEMU
uses: docker/setup-qemu-action@v2
with:
platforms: linux/amd64

- name: Docker Setup Buildx
uses: docker/[email protected]

- name: Docker Login
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}

- name: Build and push Docker image
uses: docker/[email protected]
with:
platforms: |
linux/amd64
push: true
tags: c4stus/lights-api:${{ env.GITHUB_SHA }}
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM --platform=$BUILDPLATFORM golang:1.20 AS builder
ARG TARGETPLATFORM
ARG BUILDPLATFORM
ARG TARGETARCH
RUN echo "I am running on $BUILDPLATFORM, building for $TARGETPLATFORM"

WORKDIR /data
COPY . /data
RUN GOOS=linux GOARCH=$TARGETARCH go build -o lights-api

FROM scratch
COPY --from=builder /data/lights-api ./
CMD ["./lights-api"]
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
This repo is an API to serve values based on time. These values are then used by LED Controllers to dim lights and set a specific color.

# How it works?

Brightness and color are values from 0 to 1000. This repo uses simple linear function that increase that number from midnight to noon and then decrease it until midnight.

```
1000 │ ┌─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
│ ┌─┘ └─┐
0 │┌─┘ └─┐
└┴─────────────────────────────────────────────────────────────────┴─▶
midnight noon midnight
```
32 changes: 32 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package api

import (
"fmt"
"math"
"strconv"
"time"
)

func Get(unixTimestamp time.Time) string {
now := time.Now()
var secondsDay float64 = 86400
startOfDay := unixTimestamp.Truncate(24 * time.Hour)
noon := startOfDay.Add(12 * time.Hour)
endOfDay := startOfDay.Add(24 * time.Hour)

fmt.Println("Start of day:", startOfDay)
fmt.Println("End of day:", endOfDay)
fmt.Println(now, secondsDay)

var value float64
if unixTimestamp.Before(noon) {
subTime := unixTimestamp.Sub(startOfDay).Seconds()
value = math.Ceil(subTime * 1000 / (secondsDay / 2))
} else {
subTime := endOfDay.Sub(unixTimestamp).Seconds()
secs := (secondsDay / 2) - subTime
value = 1000 - math.Ceil(secs*1000/(secondsDay/2))
}

return strconv.FormatFloat(value, 'f', 0, 64)
}
42 changes: 42 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package api_test

import (
"fmt"
"testing"
"time"

"github.com/castus/lights-api/api"
)

func TestAfterNoon(t *testing.T) {
ti := GetUnixTimestampForTime(21, 00)
if api.Get(ti) != "250" {
t.Fail()
}
}

func TestBeforeNoon(t *testing.T) {
ti := GetUnixTimestampForTime(11, 00)
if api.Get(ti) != "917" {
t.Fail()
}
}

func TestNoon(t *testing.T) {
ti := GetUnixTimestampForTime(12, 00)
if api.Get(ti) != "1000" {
t.Fail()
}
}

func TestMidnight(t *testing.T) {
ti := GetUnixTimestampForTime(00, 00)
fmt.Println(api.Get(ti))
if api.Get(ti) != "0" {
t.Fail()
}
}

func GetUnixTimestampForTime(hour int, minute int) time.Time {
return time.Date(2023, time.October, 19, hour, minute, 0, 0, time.UTC)
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/castus/lights-api

go 1.21.3

require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
46 changes: 46 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"fmt"
"net/http"
"strconv"
"time"

"go.uber.org/zap"

"github.com/castus/lights-api/api"
)

func main() {
logger, _ := zap.NewProduction()
log := logger.Sugar()
defer logger.Sync()

http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
queryParameters := r.URL.Query()
timestamp := queryParameters.Get("timestamp")
parsedTime, err := strconv.ParseInt(timestamp, 10, 64)
fmt.Println(parsedTime)
if err != nil {
panic(err)
}
unixTime := time.Unix(parsedTime, 0)
lightValue := api.Get(unixTime)

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Fatal(err)
} else {
w.WriteHeader(http.StatusOK)
}
w.Header().Set("Content-Type", "plain/text")
_, _ = fmt.Fprintf(w, lightValue)
})

port := "8080"
log.Infow("API server is running", "port", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}

0 comments on commit 36bdca3

Please sign in to comment.