Skip to content

Commit

Permalink
feat: week numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
kaihendry committed Nov 27, 2023
1 parent a32ce50 commit ff48db4
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 49 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/sam-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ jobs:
build-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- uses: actions/checkout@v4
- uses: actions/setup-go@v3
with:
go-version: "^1.17"
- uses: aws-actions/setup-sam@v1
- uses: aws-actions/configure-aws-credentials@v1
go-version: "stable"
check-latest: true
- uses: aws-actions/setup-sam@v2
with:
use-installer: true
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::407461997746:role/github-actions-Role-56IHHM969DKJ
aws-region: ap-southeast-1
- run: make deploy
aws-region: eu-west-2
- name: Deploy
run: make deploy
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Kai Hendry
Copyright (c) 2021-2023 Kai Hendry

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module github.com/kaihendry/days

go 1.17
go 1.21

require (
github.com/apex/gateway/v2 v2.0.0
github.com/apex/log v1.9.0
)

require (
github.com/aws/aws-lambda-go v1.28.0 // indirect
github.com/aws/aws-lambda-go v1.41.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ github.com/aws/aws-lambda-go v1.27.0 h1:aLzrJwdyHoF1A18YeVdJjX8Ixkd+bpogdxVInvHc
github.com/aws/aws-lambda-go v1.27.0/go.mod h1:jJmlefzPfGnckuHdXX7/80O3BvUUi12XOkbv4w9SGLU=
github.com/aws/aws-lambda-go v1.28.0 h1:fZiik1PZqW2IyAN4rj+Y0UBaO1IDFlsNo9Zz/XnArK4=
github.com/aws/aws-lambda-go v1.28.0/go.mod h1:jJmlefzPfGnckuHdXX7/80O3BvUUi12XOkbv4w9SGLU=
github.com/aws/aws-lambda-go v1.41.0 h1:l/5fyVb6Ud9uYd411xdHZzSf2n86TakxzpvIoz7l+3Y=
github.com/aws/aws-lambda-go v1.41.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM=
github.com/aws/aws-sdk-go v1.20.6/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
Expand Down
28 changes: 16 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import (
"embed"
"fmt"
"html/template"
"log/slog"
"net/http"
"os"
"time"

"github.com/apex/gateway/v2"
"github.com/apex/log"
jsonhandler "github.com/apex/log/handlers/json"
"github.com/apex/log/handlers/text"
)

//go:embed templates
Expand All @@ -23,18 +21,24 @@ var Version string
func days(month time.Time) (days []time.Time) {
firstDay := time.Date(month.Year(), month.Month(), 1, 0, 0, 0, 0, time.UTC)
monthEnd := firstDay.AddDate(0, 1, -1) // add a month, minus a day
log.WithField("monthEnd", monthEnd).Info("last day")
slog.Info("last day", "monthEnd", monthEnd)
for i := 0; i < monthEnd.Day(); i++ {
days = append(days, firstDay.AddDate(0, 0, i))
}
log.WithFields(log.Fields{"month": month, "days": days}).Debug("days of a month")
slog.Debug("days of a month", "month", month, "days", days)
return days
}

func getWeekNumber(t time.Time) int {
_, week := t.ISOWeek()
return week
}

func main() {
t, err := template.ParseFS(tmpl, "templates/*.html")
t, err := template.New("base").Funcs(template.FuncMap{"weekNumber": getWeekNumber}).ParseFS(tmpl, "templates/*.html")
if err != nil {
log.WithError(err).Fatal("Failed to parse templates")
slog.Error("Failed to parse templates", "error", err)
return
}

http.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
Expand All @@ -43,8 +47,8 @@ func main() {
if inputMonth != "" {
chosenDate, err = time.Parse("2006-01", r.URL.Query().Get("month"))
if err != nil {
log.Warn("bad input, defaulting to current month")
chosenDate = time.Now()
slog.Warn("bad input, defaulting to current month", "month", chosenDate.Format("2006-01"))
}
}
rw.Header().Set("Content-Type", "text/html")
Expand All @@ -60,17 +64,17 @@ func main() {
Version})
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
log.WithError(err).Fatal("Failed to execute templates")
slog.Error("Failed to execute templates", "error", err)
}
})

log.SetHandler(jsonhandler.Default)
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))

if _, ok := os.LookupEnv("AWS_LAMBDA_FUNCTION_NAME"); ok {
err = gateway.ListenAndServe("", nil)
} else {
log.SetHandler(text.Default)
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, nil)))
err = http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("PORT")), nil)
}
log.WithError(err).Fatal("error listening")
slog.Error("error listening", "error", err)
}
6 changes: 6 additions & 0 deletions template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Resources:
AccessLogGroup:
Type: "AWS::Logs::LogGroup"
Properties:
RetentionInDays: 30
LogGroupName: !Ref DomainName

Gateway:
Expand Down Expand Up @@ -39,6 +40,11 @@ Resources:
Metadata:
BuildMethod: makefile

Globals:
Function:
LoggingConfig:
LogFormat: JSON

Outputs:
GatewayEndPoint:
Value: !Sub "https://${Gateway}.execute-api.${AWS::Region}.amazonaws.com/"
90 changes: 63 additions & 27 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,48 +1,84 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<title>Days of {{ .Month.Format "January 2006" }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="data:,">

<style>
html {
max-width: 38rem;
padding: 2rem;
margin: auto;
line-height: 1.5rem;
font-size: 24px;
}

body {
font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
width: 80%;
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 1rem;
color: #333;
display: flex;
flex-direction: column;
align-items: center;
margin: auto;
}
li {
list-style-type: none;

h2 {
color: #4a7c59;
margin: 1rem 0;
font-size: 1.2rem;
}

p {
margin: 0.25rem 0;
font-size: 1rem;
}

p strong {
font-weight: bold;
color: #d35400; /* A distinct color for the current day */
}

form {
margin-bottom: 1rem;
}

input[type="month"] {
padding: 0.5rem;
border: 1px solid #ddd;
border-radius: 0.25rem;
font-size: 1rem;
margin-bottom: 1rem;
}

a {
color: #2a5db0;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}
</style>
<title>Days of {{ .Month.Format "January 2006" }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="data:,">
</head>

<body>
<form>
<input type="month" value='{{ .Month.Format "2006-01" }}' name="month" onchange="this.form.submit()">
</form>
<ol>
{{range $day := .Days -}}
{{ if eq ($day.Format "2006-01-02") ($.Now.Format "2006-01-02") }}
<li><strong>{{ $day.Format "2006-01-02 Mon" }}</strong></li>
{{ else }}
<li>{{ $day.Format "2006-01-02 Mon" }}</li>
{{ end }}
{{end}}
</ol>
{{ $previousWeek := -1 }} <!-- Initialize a variable to track the previous week number -->
{{range $day := .Days -}}
{{ $currentWeek := $day | weekNumber }} <!-- Get the current week number -->
{{ if ne $currentWeek $previousWeek }} <!-- Compare with the previous week number -->
<h2>Week Number: {{ $currentWeek }}</h2> <!-- Print the week number if it's different -->
{{ $previousWeek = $currentWeek }} <!-- Update the previous week number -->
{{ end }}

{{ if eq ($day.Format "2006-01-02") ($.Now.Format "2006-01-02") }}
<p><strong>{{ $day.Format "2006-01-02 Mon" }}</strong></p>
{{ else }}
<p>{{ $day.Format "2006-01-02 Mon" }}</p>
{{ end }}
{{end}}

<br>
<p>Version: {{ .Version }} - <a href="https://github.com/kaihendry/days">Source code</a></p>
</body>

</html>
</html>

0 comments on commit ff48db4

Please sign in to comment.