Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricshih committed Jul 23, 2022
1 parent bb981a1 commit ab6c65e
Show file tree
Hide file tree
Showing 43 changed files with 837 additions and 644 deletions.
2 changes: 1 addition & 1 deletion .cloudbuild/cloud-run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ steps:
- '-t'
- 'asia.gcr.io/$PROJECT_ID/rudy-balancer'
- '-f'
- '.docker/Dockerfile'
- 'Dockerfile'
- '.'
- name: 'gcr.io/cloud-builders/docker'
args:
Expand Down
3 changes: 0 additions & 3 deletions .docker/entrypoint.sh

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.vscode/
/.tool
/go.sum
/serviced
Expand Down
2 changes: 1 addition & 1 deletion .make/docker.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ docker/build:
--build-arg GIT_HASH=$(GIT_HASH) \
--build-arg GIT_TAG=$(GIT_TAG) \
-t $(IMAGE_NAME) \
-f .docker/Dockerfile \
-f Dockerfile \
.

# Run docker image.
Expand Down
21 changes: 7 additions & 14 deletions .docker/Dockerfile → Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.15-alpine as builder
FROM golang:1.15-alpine as go-builder

RUN apk update \
&& apk upgrade \
Expand All @@ -12,10 +12,8 @@ COPY . /src/
WORKDIR /src/

ARG GIT_HASH
ARG GIT_TAG
RUN make clean \
&& make tidy \
&& make GIT_HASH=${GIT_HASH} GIT_TAG=${GIT_TAG}
ARG GIT_TAGGIT_TAG
RUN go build -ldflags="-X service/version.GitHash=${GIT_HASH} -X service/version.GitTag=${GIT_TAG}" -o serviced .

FROM alpine:3.12

Expand All @@ -24,17 +22,12 @@ RUN apk update \
&& apk add --no-cache \
&& rm -rf /var/cache/apk/* /tmp/*

COPY --from=builder /src/serviced /usr/sbin/
COPY --from=go-builder /src/serviced /usr/sbin/
RUN mkdir -p /etc/serviced/
COPY .docker/entrypoint.sh /
COPY config.yaml /
COPY mirrors.yaml /
COPY config/docker.yaml /
COPY config/mirrors.yaml /
COPY webroot /webroot

ENV GEOIP2_LICENSE_KEY=

EXPOSE 8080

VOLUME ["/var/lib/serviced"]

CMD ["/entrypoint.sh"]
ENTRYPOINT [ "/usr/sbin/serviced", "-e", "docker", "-w", "/webroot" ]
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ BUILD_TIME ?= $(shell date +'%s')
GIT_HASH ?= $(shell git rev-parse --short HEAD)
GIT_TAG ?= $(shell git describe --tags --exact-match 2>/dev/null || echo "")

IMAGE_NAME := outdoorsafetylab/rudy-balancer

VARS :=
VARS += BuildTime=$(BUILD_TIME)
VARS += GitHash=$(GIT_HASH)
VARS += GitTag=$(GIT_TAG)
LDFLAGS := $(addprefix -X version.,$(VARS))
LDFLAGS := $(addprefix -X service/version.,$(VARS))

all: $(EXEC)

Expand All @@ -24,7 +26,7 @@ include .make/watcher.mk
include .make/docker.mk

watch: $(PBGO) $(WEBINDEX) $(WATCHER) tidy
$(realpath $(WATCHER)) -c local
$(realpath $(WATCHER))

tidy: $(PBGO)
go mod tidy
Expand Down
5 changes: 0 additions & 5 deletions api/version.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ package api;
option go_package = "./api";

message GetVersionResponse {
message Time {
int64 epoch = 1;
string rfc3339 = 2;
}
Time time = 1;
string tag = 2;
string commit = 3;
}
6 changes: 5 additions & 1 deletion cache/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"bytes"
"image"
"image/jpeg"
"io/ioutil"
"net/http"
)
Expand All @@ -29,7 +30,10 @@ func GetImage(url string) (image.Image, error) {
}
img, _, err := image.Decode(bytes.NewBuffer(data))
if err != nil {
return nil, err
img, err = jpeg.Decode(bytes.NewBuffer(data))
if err != nil {
return nil, err
}
}
return img, nil
}
5 changes: 0 additions & 5 deletions config.yaml

This file was deleted.

7 changes: 7 additions & 0 deletions config/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
port: 8080
endpoint: /v1
webroot: /webroot
mirrors:
file: /mirrors.yaml
firestore:
collection: RudyMirrors
12 changes: 12 additions & 0 deletions config/firestoredb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "service_account",
"project_id": "outdoorsafetylab",
"private_key_id": "d53b9674c24ddbbbeef35d31da1b34bc17145695",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDuvV6d1zKdY2Lg\nfG5FgBz7zw2dNZRBxVwzelePwtVhlMwesnmXffwYrPbX0fyNRkn6hs6UVN0KSSfb\nWKykTmc7BQS6U/DXJrVEvMDxIRsV8qIguWTIvNl++1UwnZDMpLh7bGqxNOK9xY22\nL2buk0qAxBGfdS6NMWxIZzFOFqixp7fYO4SNeIFIOr5OfTkwF6iPfb9NRF+nsJ9K\nKuAxicFlP203njJ+9E2Y9H/ayciOY7Rw9+PWuG0ODE1chW4suHZeOuXss7UNiVbA\nq5ygZRGLpIPl6Of3OYE0f5tvzum5EDXmO+r/DcyEDanFp10+5IRiKGEkJO+/hTke\n4NaK+TqhAgMBAAECggEAGdnum2fYhrti0w0WQbdYLBolDr9lAOQ2kipuvdXqlijv\nG2nXCcTuHdvo1MDbuI6CZzretqgj0T/PrYxCEX6d5gGqm+SIATwjR8CqodGkZbGC\nxclddPEiGgJsHJoLOmWhxleci/i+jq8MI8ypOC9B04Snpd0h/874uzRVXe/NL4NU\nrxKTgr/UC0DxgrIChC7C6W9QwiR2a+1XWCixA/IJEkp+/Hg4QSP7rj5n3iLxo+Am\n31cqF46e1xiNUQycPOV92WriI6arkBBOnTLnsrE674tt8UIRJFeswKmfbdz7xvFQ\nEidvsk4C7wi2/8BAlhlYK1IFBulkN/JdY80GZDqgqwKBgQD/X1IqS1BiigogUNAt\neLxoonIp8CO4bWVu5ShQxoubCjQHc72kr4k1/HfI0BEmWg/RRns4doKkbHVgpeBA\n6blFbBla8lmcNvG56hD717tkeXot7QTyIHtNMYDqdA/CGhsmEdlbCRXKXAj7hHqy\nGhtdATYan9Hmr3xSw5CUyBeSMwKBgQDvU5Ved6R0/kIMqUxAUsoxN/slDGv5Q6U9\nAKHpT0k4LrmbhE6gRGXsCyjSE/HoyjkhJD6tsdHzyT0oSUVoXnDjUU3gPOXEr9pz\nmvkJqnQMGSsqfwOCv2Q/uT7voGkBNGQdkzeDZfivay7VBHiAkEVmxeSb3ujPeeQq\nCzbIF5Uz2wKBgHSlJpO5CQOph8yQ/TX38x9cLAdp7/b6yR0pQQv8VKJBu9g3jsUd\nhPAK3O/10g+Rja4Kopf80dmmzMqTmeYsm/rQ0JgRIMtQ8YL5Hj7aoHbZYex+Wgb6\nvTdaSEdPMi932zS9bGm/sZtYBKZWMfEOKbeGqR7qJ2dWetQXDgBXkSSvAoGBAM+p\ndPhNVDP30xPtaq59p9fhPVg+WQomuiAehl9NXrkTQhZBTYAfGVkhrb/rcoKNxdiN\n/Jz2qLiiKyDjwwf2unlkOPCTpsNt211oYSMT9jHxPNVY9JDkxXXNpWCDjdlvvlcz\nQH4RYyHDHRXVFUJo7O9qUx005Eny+BnLNTOV0u71AoGBAMZjIeZXeKJyku8Mn3d7\nteVlRZ9esV3VnYwXs+d2DEslLFwKBIopcN5DvANIejpk6jppgIehA1Dv5C/fVvWw\nNzODws+Lv+nRALszFVDeYV12K04ghTA53flb6KfvNezf3PQgVwYfrFPXR7jtEMiW\nJk63kL5h86cAurcdgn+IawxD\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "117907332197232153525",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firestoredb%40outdoorsafetylab.iam.gserviceaccount.com"
}
15 changes: 10 additions & 5 deletions config/local.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
log:
dev: true
port: 8080
endpoint: /v1
webroot: ./webroot
db:
file: mirrors.yaml
#check: 30s
mirrors:
file: config/mirrors.yaml
firestore:
# project_id: test
project_id: "*detect-project-id*"
credential: config/firestoredb.json
collection: RudyMirrors
statuspage:
key: 9d6e4756-debb-44a7-b6c5-49315faeb4f0
page: 4fbwbd76cnj2
21 changes: 12 additions & 9 deletions mirrors.yaml → config/mirrors.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
sites:
- name: Rex
endpoint: rudy.basecamp.tw/
scheme: https
- name: KCWu
endpoint: moi.kcwu.csie.org/
scheme: https
- name: Happyman
endpoint: map.happyman.idv.tw/rudy/
scheme: https
apps:
- id: gts
name: 綠野遊蹤
icon: https://rudy.basecamp.tw/images/GTS.png
icon: https://github.com/alpha-rudy/taiwan-topo/raw/master/docs/images/GTS.png
variants:
- id: standard
name: 傳統版
Expand Down Expand Up @@ -50,44 +53,44 @@ apps:
file: hgt90.zip
- id: oruxmaps
name: OruxMaps
icon: https://rudy.basecamp.tw/images/OruxMaps.jpeg
icon: https://github.com/alpha-rudy/taiwan-topo/raw/master/docs/images/OruxMaps.jpeg
variants:
- id: default
artifacts:
- id: map
name: 離線地圖
icon: http://rudy.basecamp.tw/images/OruxMaps_map.jpeg
icon: https://github.com/alpha-rudy/taiwan-topo/raw/master/docs/images/OruxMaps_map.jpeg
scheme: "orux-map:"
file: MOI_OSM_Taiwan_TOPO_Rudy.map.zip
- id: style
name: 風格主題
icon: http://rudy.basecamp.tw/images/OruxMaps_style.jpeg
icon: https://github.com/alpha-rudy/taiwan-topo/raw/master/docs/images/OruxMaps_style.jpeg
scheme: "orux-mf-theme:"
file: MOI_OSM_Taiwan_TOPO_Rudy_style.zip
- id: hgt
name: 高程檔
icon: http://rudy.basecamp.tw/images/OruxMaps_dem.jpeg
icon: https://github.com/alpha-rudy/taiwan-topo/raw/master/docs/images/OruxMaps_dem.jpeg
file: hgtmix.zip
- id: carto
name: Cartograph Pro 2
icon: https://rudy.basecamp.tw/images/Cartograph.png
icon: https://github.com/alpha-rudy/taiwan-topo/raw/master/docs/images/Cartograph.png
variants:
- id: default
name: 分開安裝
artifacts:
- id: map
name: 離線地圖
icon: http://rudy.basecamp.tw/images/Cartograph_map.png
icon: https://github.com/alpha-rudy/taiwan-topo/raw/master/docs/images/Cartograph_map.png
scheme: "cartograph-map:"
file: carto_map.cpkg
- id: style
name: 風格主題
icon: http://rudy.basecamp.tw/images/Cartograph_style.png
icon: https://github.com/alpha-rudy/taiwan-topo/raw/master/docs/images/Cartograph_style.png
scheme: "cartograph-map:"
file: carto_style.cpkg
- id: hgt
name: 高程檔
icon: http://rudy.basecamp.tw/images/Cartograph_dem.png
icon: https://github.com/alpha-rudy/taiwan-topo/raw/master/docs/images/Cartograph_dem.png
scheme: "cartograph-map:"
file: carto_dem.cpkg
- id: allinone
Expand Down
41 changes: 41 additions & 0 deletions controller/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package controller

import (
"encoding/json"
"fmt"
"net/http"
"service/config"
"service/dao"
)

type AppController struct{}

func (c *AppController) List(w http.ResponseWriter, r *http.Request) {
dao := &dao.HealthDao{Context: r.Context()}
apps, err := dao.Apps()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
prefix := config.Get().GetString("endpoint")
for _, app := range apps {
for _, v := range app.Variants {
for _, a := range v.Artifacts {
if a.Scheme == "" {
if r.TLS == nil {
a.Scheme = "http:"
} else {
a.Scheme = "https:"
}
}
a.URL = fmt.Sprintf("%s//%s%s/%s", a.Scheme, r.Host, prefix, a.File)
}
}
}
enc := json.NewEncoder(w)
err = enc.Encode(apps)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}
21 changes: 0 additions & 21 deletions controller/apps.go

This file was deleted.

38 changes: 38 additions & 0 deletions controller/artifact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package controller

import (
"fmt"
"math/rand"
"net/http"
"service/dao"
"service/model"

log "github.com/sirupsen/logrus"
)

type ArtifactController struct {
Artifact *model.Artifact
URI string
}

func (c *ArtifactController) URL(w http.ResponseWriter, r *http.Request) {
url := fmt.Sprintf("%s//%s%s", c.Artifact.Scheme, r.Host, c.URI)
http.Error(w, url, 200)
}

func (c *ArtifactController) Download(w http.ResponseWriter, r *http.Request) {
dao := &dao.HealthDao{Context: r.Context()}
urls, err := dao.GetAvailableURLs(c.Artifact)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if urls == nil || len(urls) <= 0 {
log.Warningf("No available sources: %s (%s)", c.Artifact.File, c.Artifact.File)
http.Error(w, err.Error(), 501)
return
}
u := urls[rand.Intn(len(urls))]
log.Warningf("Redircting: %s (%s) => %s", c.Artifact.File, c.Artifact.File, u.String())
http.Redirect(w, r, u.String(), 302)
}
36 changes: 36 additions & 0 deletions controller/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package controller

import (
"net/http"
"service/dao"
"service/mirror"
"time"

log "github.com/sirupsen/logrus"
)

type HealthController struct{}

func (c *HealthController) Check(w http.ResponseWriter, r *http.Request) {
artifacts, err := mirror.Artifacts()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
client := &http.Client{
Timeout: 5 * time.Second,
}
for _, a := range artifacts {
log.Debugf("Checking artifact: %s", a.File)
for _, s := range a.Sources {
_ = s.Check(client)
log.Debugf("%s => %s", s.URL.String(), s.Status.String())
}
}
dao := &dao.HealthDao{Context: r.Context()}
err = dao.Update(artifacts)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}
Loading

0 comments on commit ab6c65e

Please sign in to comment.