Skip to content

Commit

Permalink
add embed image
Browse files Browse the repository at this point in the history
  • Loading branch information
srliao committed Apr 15, 2024
1 parent 8160999 commit 9d07e61
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 22 deletions.
51 changes: 51 additions & 0 deletions .github/actions/containers/embedgenerator/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: "Build embedgenerator container"
description: "build embed generator container"
inputs:
githubToken:
required: true
description: github token
githubActor:
required: true
description: github actor
githubRepo:
required: true
description: github repo

runs:
using: composite
steps:
- name: Build UI
working-directory: ./ui
shell: bash
run: yarn workspace @gcsim/embed build

- name: List UI dist
working-directory: ./ui/packages/embed/dist
shell: bash
run: ls -lh

- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ghcr.io
username: ${{ inputs.githubActor }}
password: ${{ inputs.githubToken }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ghcr.io/${{ inputs.githubRepo }}

- name: Build go executable
working-directory: ./cmd/services/embedgenerator
run: GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build .

- name: Build and push Docker image
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
file: ./build/docker/embedgenerator/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
42 changes: 42 additions & 0 deletions .github/workflows/containers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: deployment build
on:
workflow_dispatch:
push:
branches:
# this should be changed to on main pr + specific tags
- containers

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Get branch names
id: branch-name
uses: tj-actions/branch-names@v7

# grab latest sha
- name: Setup Environment (PR)
if: ${{ github.event_name == 'pull_request' }}
shell: bash
run: |
echo "LAST_COMMIT_SHA=${{ github.event.pull_request.head.sha }}" >> ${GITHUB_ENV}
- name: Setup Environment (Push)
if: ${{ github.event_name == 'push' }}
shell: bash
run: |
echo "LAST_COMMIT_SHA=${GITHUB_SHA}" >> ${GITHUB_ENV}
# setup + test go and yarn
- uses: actions/checkout@v3
- name: go-setup-and-test
uses: ./.github/actions/go-setup-and-test
- name: yarn-setup-and-test
uses: ./.github/actions/yarn-setup-and-test

# embed container
- name: build embedgenerator container
uses: ./.github/actions/containers/embedgenerator
with:
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
githubActor: $${{ github.actor}}
githubRepo: $${{ github.repository }}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*.pprof
.DS_Store
/*.txt
/build/*
*.out
!.keep
out.json
Expand Down
6 changes: 6 additions & 0 deletions build/bash/embedgenerator/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cd ui
yarn workspace @gcsim/embed build
cd ..
cd cmd/services/embedgenerator
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build .
docker build -f build/docker/embedgenerator/Dockerfile --no-cache --progress=plain
7 changes: 7 additions & 0 deletions build/docker/embedgenerator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM alpine:3.16.3
WORKDIR /
ADD ui/packages/embed/dist /dist
COPY cmd/services/embedgenerator/embedgenerator /embedgenerator
RUN chmod +x /embedgenerator
RUN ls -la
ENTRYPOINT ["embedgenerator"]
2 changes: 0 additions & 2 deletions cmd/services/embedgenerator/dist/.gitignore

This file was deleted.

31 changes: 17 additions & 14 deletions cmd/services/embedgenerator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"embed"
"errors"
"fmt"
"log"
Expand All @@ -17,15 +16,13 @@ import (
"github.com/redis/go-redis/v9"
)

//go:embed dist/*
var content embed.FS

type config struct {
Host string `env:"HOST"`
Port string `env:"PORT" envDefault:"3000"`
LauncherURL string `env:"LAUNCHER_URL" envDefault:"ws://launcher:7317"`
AuthKey string `env:"AUTH_KEY"`
PreviewURL string `env:"PREVIEW_URL" envDefault:"http://preview:3000"`
Host string `env:"HOST"`
Port string `env:"PORT" envDefault:"3000"`
LauncherURL string `env:"LAUNCHER_URL" envDefault:"ws://launcher:7317"`
AuthKey string `env:"AUTH_KEY"`
PreviewURL string `env:"PREVIEW_URL" envDefault:"http://preview:3000"`
StaticAssets string `env:"STATIC_ASSETS" envDefault:"/dist"`
// proxy is always used
ProxyTO string `env:"PROXY_TO" envDefault:"https://gcsim.app"`
ProxyPrefix string `env:"PROXY_PREFIX" envDefault:"/api"`
Expand Down Expand Up @@ -56,11 +53,17 @@ func main() {
}
log.Println("running with config ", cfg)

server, err := embedgenerator.New(content, redis.UniversalOptions{
Addrs: cfg.RedisURL,
DB: cfg.RedisDB,
MasterName: cfg.RedisMasterName,
}, cfg.LauncherURL, cfg.PreviewURL, cfg.AuthKey)
server, err := embedgenerator.New(
cfg.StaticAssets,
redis.UniversalOptions{
Addrs: cfg.RedisURL,
DB: cfg.RedisDB,
MasterName: cfg.RedisMasterName,
},
cfg.LauncherURL,
cfg.PreviewURL,
cfg.AuthKey,
)

panicErr(err)

Expand Down
3 changes: 2 additions & 1 deletion internal/services/embedgenerator/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"mime"
"net/http"
"os"
"path"
"path/filepath"
"strings"
Expand All @@ -27,7 +28,7 @@ func (s *Server) handleProxy(prefix string) http.HandlerFunc {
var ErrDir = errors.New("path is dir")

func (s *Server) tryRead(requestedPath string, w http.ResponseWriter) error {
f, err := s.staticFS.Open(path.Join("dist", requestedPath))
f, err := os.Open(path.Join(s.staticDir, requestedPath))
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions internal/services/embedgenerator/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package embedgenerator
import (
"context"
"crypto/tls"
"embed"
"fmt"
"log/slog"
"net/http"
Expand Down Expand Up @@ -33,7 +32,7 @@ type Server struct {
authKey string

// static asset; mandatory to serve from
staticFS embed.FS
staticDir string

// additional local assets
useLocalAssets bool
Expand All @@ -57,9 +56,9 @@ type Server struct {
browser *rod.Browser
}

func New(fs embed.FS, connOpt redis.UniversalOptions, launcherURL, previewURL, authKey string) (*Server, error) {
func New(staticDir string, connOpt redis.UniversalOptions, launcherURL, previewURL, authKey string) (*Server, error) {
s := &Server{
staticFS: fs,
staticDir: staticDir,
work: make(chan string),
l: launcher.MustNewManaged(launcherURL),
Mux: chi.NewRouter(),
Expand Down

0 comments on commit 9d07e61

Please sign in to comment.