-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
418 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: build | ||
|
||
on: | ||
- push | ||
- pull_request | ||
|
||
jobs: | ||
go: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version: '1.22.5' | ||
- run: 'go build' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
name: deploy | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
caprover: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- run: | | ||
echo ${{ secrets.GHCR_TOKEN }} | docker login ghcr.io -u gleich --password-stdin | ||
docker build -t ghcr.io/gleich/lcp . | ||
docker push ghcr.io/gleich/lcp | ||
- uses: caprover/[email protected] | ||
with: | ||
server: https://dev.mattglei.ch | ||
app: lcp | ||
token: '${{ secrets.CAPROVER_APP_TOKEN }}' | ||
image: ghcr.io/gleich/lcp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# air | ||
tmp/ | ||
|
||
# goreleaser | ||
dist/ | ||
|
||
# macOS Finder | ||
.DS_Store | ||
|
||
# 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 | ||
|
||
# Secrets | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"github-actions.workflows.pinned.workflows": [".github/workflows/deploy.yml"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM golang:1.22.5 | ||
|
||
COPY . . | ||
|
||
RUN go build && touch .env | ||
|
||
CMD ["lcp"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Matt Gleich | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# [lcp](https://mattglei.ch/lcp) | ||
|
||
[![deploy](https://github.com/gleich/lcp-v2/actions/workflows/deploy.yml/badge.svg)](https://github.com/gleich/lcp-v2/actions/workflows/deploy.yml) | ||
[![build](https://github.com/gleich/lcp-v2/actions/workflows/build.yml/badge.svg)](https://github.com/gleich/lcp-v2/actions/workflows/build.yml) | ||
|
||
Lightweight Cache Proxy Service. Powers [mattglei.ch](https://mattglei.ch). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module github.com/gleich/lcp-v2 | ||
|
||
go 1.22.5 | ||
|
||
require ( | ||
github.com/caarlos0/env/v11 v11.1.0 | ||
github.com/gleich/lumber/v2 v2.2.0 | ||
github.com/go-chi/chi/v5 v5.1.0 | ||
github.com/joho/godotenv v1.5.1 | ||
) | ||
|
||
require ( | ||
github.com/fatih/color v1.17.0 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/stretchr/testify v1.9.0 // indirect | ||
github.com/wayneashleyberry/truecolor v1.0.1 // indirect | ||
golang.org/x/sys v0.22.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
github.com/caarlos0/env/v11 v11.1.0 h1:a5qZqieE9ZfzdvbbdhTalRrHT5vu/4V1/ad1Ka6frhI= | ||
github.com/caarlos0/env/v11 v11.1.0/go.mod h1:LwgkYk1kDvfGpHthrWWLof3Ny7PezzFwS4QrsJdHTMo= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= | ||
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= | ||
github.com/gleich/lumber/v2 v2.2.0 h1:PQoASuBheK+pzqeDtiPR/rvzvyqEPFP0cZDN9kz2Fqk= | ||
github.com/gleich/lumber/v2 v2.2.0/go.mod h1:pkfSf79z7hol7GyiXlvjOP6SL0vCAtyK2blrluRcc78= | ||
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= | ||
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= | ||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= | ||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= | ||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= | ||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= | ||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= | ||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= | ||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk= | ||
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk= | ||
github.com/wayneashleyberry/truecolor v1.0.1 h1:REnJBycjnvg0AFErbLx2GCmLLar8brlqm62kOKnRsGs= | ||
github.com/wayneashleyberry/truecolor v1.0.1/go.mod h1:fyL3jRES70g94n+Eu+XLhXYvcseza55ph8zlkmUKW7Q= | ||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= | ||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/caarlos0/env/v11" | ||
"github.com/gleich/lcp-v2/pkg/cache" | ||
"github.com/gleich/lcp-v2/pkg/secrets" | ||
"github.com/gleich/lcp-v2/pkg/strava" | ||
"github.com/gleich/lumber/v2" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/chi/v5/middleware" | ||
"github.com/joho/godotenv" | ||
) | ||
|
||
func main() { | ||
lumber.Info("booted") | ||
|
||
err := godotenv.Load() | ||
if err != nil { | ||
lumber.Fatal(err, "Error loading .env file") | ||
} | ||
loadedSecrets, err := env.ParseAs[secrets.Secrets]() | ||
if err != nil { | ||
lumber.Fatal(err, "parsing required env vars failed") | ||
} | ||
|
||
stravaTokens := strava.LoadTokens(loadedSecrets) | ||
stravaTokens.RefreshIfNeeded(loadedSecrets) | ||
stravaActivities := strava.FetchActivities(stravaTokens) | ||
stravaCache := cache.New("strava", stravaActivities) | ||
lumber.Success("init strava cache") | ||
|
||
r := chi.NewRouter() | ||
r.Use(middleware.Logger) | ||
r.HandleFunc("/", rootRedirect) | ||
r.Get("/strava/cache", stravaCache.Route(loadedSecrets)) | ||
err = http.ListenAndServe(":8000", r) | ||
if err != nil { | ||
lumber.Fatal(err, "failed to start router") | ||
} | ||
} | ||
|
||
func rootRedirect(w http.ResponseWriter, r *http.Request) { | ||
http.Redirect(w, r, "https://mattglei.ch", http.StatusTemporaryRedirect) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cache | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"reflect" | ||
"sync" | ||
"time" | ||
|
||
"github.com/gleich/lcp-v2/pkg/secrets" | ||
"github.com/gleich/lumber/v2" | ||
) | ||
|
||
type Cache[T any] struct { | ||
Name string | ||
mutex sync.RWMutex | ||
data T | ||
updated time.Time | ||
} | ||
|
||
func New[T any](name string, data T) Cache[T] { | ||
return Cache[T]{ | ||
Name: name, | ||
data: data, | ||
updated: time.Now(), | ||
} | ||
} | ||
|
||
type response[T any] struct { | ||
Data T `json:"data"` | ||
Updated time.Time `json:"updated"` | ||
} | ||
|
||
// Handle a GET request to load data from the given cache | ||
func (c *Cache[T]) Route(loadedSecrets secrets.Secrets) http.HandlerFunc { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Header.Get("Authorization") != "Bearer "+loadedSecrets.ValidToken { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
c.mutex.RLock() | ||
w.Header().Set("Content-Type", "application/json") | ||
err := json.NewEncoder(w).Encode(response[T]{Data: c.data, Updated: c.updated}) | ||
c.mutex.RUnlock() | ||
if err != nil { | ||
lumber.Error(err, "failed to write data") | ||
} | ||
}) | ||
} | ||
|
||
// Update the given cache | ||
func (c *Cache[T]) Update(data T) { | ||
var updated bool | ||
c.mutex.Lock() | ||
if !reflect.DeepEqual(data, c.data) { | ||
c.data = data | ||
c.updated = time.Now() | ||
} | ||
c.mutex.Unlock() | ||
if updated { | ||
lumber.Success(c.Name, "updated") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package secrets | ||
|
||
type Secrets struct { | ||
ValidToken string `env:"VALID_TOKEN"` | ||
|
||
StravaClientID string `env:"STRAVA_CLIENT_ID"` | ||
StravaClientSecret string `env:"STRAVA_CLIENT_SECRET"` | ||
StravaOAuthCode string `env:"STRAVA_OAUTH_CODE"` | ||
StravaAccessToken string `env:"STRAVA_ACCESS_TOKEN"` | ||
StravaRefreshToken string `env:"STRAVA_REFRESH_TOKEN"` | ||
StravaRefreshTokenExpiration int64 `env:"STRAVA_REFRESH_TOKEN_EXPIRATION"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package strava | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gleich/lumber/v2" | ||
) | ||
|
||
type Activity struct { | ||
Name string `json:"name"` | ||
SportType string `json:"sport_type"` | ||
StartDate time.Time `json:"start_date"` | ||
Timezone string `json:"timezone"` | ||
Map struct { | ||
SummaryPolyline string `json:"summary_polyline"` | ||
} `json:"map"` | ||
Trainer bool `json:"trainer"` | ||
Commute bool `json:"commute"` | ||
Private bool `json:"private"` | ||
AverageSpeed float32 `json:"average_speed"` | ||
MaxSpeed float32 `json:"max_speed"` | ||
AverageTemp int32 `json:"average_temp,omitempty"` | ||
AverageCadence float32 `json:"average_cadence,omitempty"` | ||
AverageWatts float32 `json:"average_watts,omitempty"` | ||
DeviceWatts bool `json:"device_watts,omitempty"` | ||
AverageHeartrate float32 `json:"average_heartrate,omitempty"` | ||
TotalElevationGain float32 `json:"total_elevation_gain"` | ||
MovingTime uint32 `json:"moving_time"` | ||
SufferScore float32 `json:"suffer_score,omitempty"` | ||
PrCount uint32 `json:"pr_count"` | ||
Distance float32 `json:"distance"` | ||
ID uint64 `json:"id"` | ||
} | ||
|
||
func FetchActivities(tokens Tokens) []Activity { | ||
req, err := http.NewRequest("GET", "https://www.strava.com/api/v3/athlete/activities", nil) | ||
if err != nil { | ||
lumber.Error(err, "Failed to create new request") | ||
return nil | ||
} | ||
req.Header.Set("Authorization", "Bearer "+tokens.Access) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
lumber.Error(err, "Failed to send request for Strava activities") | ||
return nil | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
lumber.Error(err, "reading response body failed") | ||
return nil | ||
} | ||
|
||
var activities []Activity | ||
err = json.Unmarshal(body, &activities) | ||
if err != nil { | ||
lumber.Error(err, "failed to parse json") | ||
lumber.Debug(string(body)) | ||
return nil | ||
} | ||
|
||
return activities | ||
} |
Oops, something went wrong.