From 6ad70370d1449e4d3769f41a20b7d0bc51571a82 Mon Sep 17 00:00:00 2001 From: Khairul Hidayat Date: Sun, 18 Aug 2024 06:33:58 +0700 Subject: [PATCH] chore: update dockerfile, readme, etc --- .dockerignore | 1 + Dockerfile | 30 ++++++++++-------------- README.md | 6 ++--- backend/.air.toml | 51 +++++++++++++++++++++++++++++++++++++++++ backend/Makefile | 2 +- backend/go.mod | 5 +++- backend/go.sum | 2 ++ backend/main.go | 6 ++++- backend/utils/garage.go | 2 +- docker-compose.yml | 4 +--- package.json | 2 +- 11 files changed, 82 insertions(+), 29 deletions(-) create mode 100644 backend/.air.toml diff --git a/.dockerignore b/.dockerignore index 4f75c95..68ef3c7 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,4 +2,5 @@ node_modules .git .gitignore *.md +.env* dist diff --git a/Dockerfile b/Dockerfile index ed88577..45a2ac2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,34 +1,28 @@ -FROM node:20-slim AS base +FROM node:20-slim AS frontend WORKDIR /app ENV PNPM_HOME="/pnpm" ENV PATH="$PNPM_HOME:$PATH" RUN corepack enable -FROM base AS frontend COPY package.json pnpm-lock.yaml ./ RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile COPY . . RUN pnpm run build -FROM base AS backend-deps -COPY backend/package.json backend/pnpm-lock.yaml ./ -RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install -prod --frozen-lockfile -COPY backend . - -FROM oven/bun:alpine AS backend -WORKDIR /app -COPY --from=backend-deps /app . -RUN bun run build - -FROM oven/bun:alpine +FROM golang:1.22.5 AS backend WORKDIR /app -ENV NODE_ENV=production -ENV DIST_ROOT=./dist +COPY backend/go.mod backend/go.sum ./ +RUN go mod download -COPY --from=frontend /app/dist /app/dist -COPY --from=backend /app/dist /app +COPY backend/ ./ +COPY --from=frontend /app/dist ./ui +RUN make -ENTRYPOINT [ "bun" , "run", "main.js" ] +FROM scratch + +COPY --from=backend /app/main /bin/main + +CMD [ "/bin/main" ] diff --git a/README.md b/README.md index 6be8727..b474500 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ The Garage Web UI is available as a Docker image. You can install it using the c ### Docker CLI ```sh -$ docker run -p 3909:3909 -v ./garage.toml:/etc/garage.toml --restart unless-stopped --name garage-webui khairul169/garage-webui:latest +$ docker run -p 3909:3909 -v ./garage.toml:/etc/garage.toml:ro --restart unless-stopped --name garage-webui khairul169/garage-webui:latest ``` ### Docker Compose @@ -37,7 +37,7 @@ services: container_name: garage-webui restart: unless-stopped volumes: - - ./garage.toml:/etc/garage.toml + - ./garage.toml:/etc/garage.toml:ro ports: - 3909:3909 ``` @@ -89,7 +89,7 @@ Once your instance of Garage Web UI is started, you can open the web UI at http: ## Development -This project is bootstrapped using TypeScript, Bun, React, and Hono. If you want to build it yourself or add additional features, follow these steps: +This project is bootstrapped using TypeScript & React for the UI, and Go for backend. If you want to build it yourself or add additional features, follow these steps: ### Setup diff --git a/backend/.air.toml b/backend/.air.toml new file mode 100644 index 0000000..58fff2a --- /dev/null +++ b/backend/.air.toml @@ -0,0 +1,51 @@ +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] + args_bin = [] + bin = "./tmp/main" + cmd = "go build -o ./tmp/main ." + delay = 1000 + exclude_dir = ["assets", "tmp", "vendor", "testdata"] + exclude_file = [] + exclude_regex = ["_test.go"] + exclude_unchanged = false + follow_symlink = false + full_bin = "" + include_dir = [] + include_ext = ["go", "tpl", "tmpl", "html"] + include_file = [] + kill_delay = "0s" + log = "build-errors.log" + poll = false + poll_interval = 0 + post_cmd = [] + pre_cmd = [] + rerun = false + rerun_delay = 500 + send_interrupt = false + stop_on_error = false + +[color] + app = "" + build = "yellow" + main = "magenta" + runner = "green" + watcher = "cyan" + +[log] + main_only = false + time = false + +[misc] + clean_on_exit = false + +[proxy] + app_port = 0 + enabled = false + proxy_port = 0 + +[screen] + clear_on_rebuild = false + keep_scroll = true diff --git a/backend/Makefile b/backend/Makefile index b88a0bb..9d7c9a6 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -1,6 +1,6 @@ build: - go build -o main -tags="prod" main.go + CGO_ENABLED=0 go build -o main -tags="prod" main.go run: go run main.go diff --git a/backend/go.mod b/backend/go.mod index 0df54ba..67b5d32 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -2,4 +2,7 @@ module khairul169/garage-webui go 1.22.5 -require github.com/pelletier/go-toml/v2 v2.2.2 // indirect +require ( + github.com/joho/godotenv v1.5.1 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect +) diff --git a/backend/go.sum b/backend/go.sum index ca42a46..677be4e 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/backend/main.go b/backend/main.go index 7e6ddf9..0dec923 100644 --- a/backend/main.go +++ b/backend/main.go @@ -7,9 +7,13 @@ import ( "khairul169/garage-webui/utils" "log" "net/http" + + "github.com/joho/godotenv" ) func main() { + godotenv.Load() + if err := utils.Garage.LoadConfig(); err != nil { log.Fatal("Failed to load config! ", err) } @@ -21,7 +25,7 @@ func main() { ui.ServeUI() host := utils.GetEnv("HOST", "0.0.0.0") - port := utils.GetEnv("PORT", "3908") + port := utils.GetEnv("PORT", "3909") addr := fmt.Sprintf("%s:%s", host, port) log.Printf("Starting server on http://%s", addr) diff --git a/backend/utils/garage.go b/backend/utils/garage.go index af5dd60..2a8b28c 100644 --- a/backend/utils/garage.go +++ b/backend/utils/garage.go @@ -21,7 +21,7 @@ type garage struct { var Garage = &garage{} func (g *garage) LoadConfig() error { - path := GetEnv("CONFIG_PATH", "/etc/garage/garage.toml") + path := GetEnv("CONFIG_PATH", "/etc/garage.toml") data, err := os.ReadFile(path) if err != nil { diff --git a/docker-compose.yml b/docker-compose.yml index 13f7409..5e0f08f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,9 +13,7 @@ services: image: khairul169/garage-webui:latest container_name: garage-webui restart: unless-stopped - environment: - - CONFIG_PATH=/etc/garage.toml volumes: - - ./garage.toml:/etc/garage.toml + - ./garage.toml:/etc/garage.toml:ro ports: - 3909:3909 diff --git a/package.json b/package.json index 8bca875..ec4ad01 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "build": "tsc -b && vite build", "lint": "eslint .", "preview": "vite preview", - "dev:server": "cd backend && npm run dev", + "dev:server": "cd backend && air", "dev": "concurrently \"npm run dev:client\" \"npm run dev:server\"" }, "dependencies": {