Skip to content

Commit

Permalink
chore: refactor dev script and auto-open browser
Browse files Browse the repository at this point in the history
- Move dev.sh to the tools directory
- Print colorful section headers to delimit dev script operations
- Auto-open the admin page after otto8 server is ready

Signed-off-by: Nick Hale <[email protected]>
  • Loading branch information
njhale committed Oct 22, 2024
1 parent 585cf71 commit a37e69a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 25 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ build:
go build -o bin/otto8 -v

dev: ui
@echo "Starting dev otto8 server and admin UI..."
./dev.sh
./tools/dev.sh

# Lint the project
lint: lint-admin
Expand Down
23 changes: 0 additions & 23 deletions dev.sh

This file was deleted.

69 changes: 69 additions & 0 deletions tools/dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash

set -e # Exit on any command failure

print_section_header() {
local color_code=$1
local message=$2
local terminal_width=$(tput cols || echo 80) # Default to 80 if tput fails
local padding_width=$(( (terminal_width - ${#message} - 2) / 2 ))
local padding=$(printf '%*s' "$padding_width" '' | tr ' ' '-')

echo -e "\033[38;5;${color_code}m${padding} ${message} ${padding}\033[0m"
}

cleanup() {
kill 0
}

trap cleanup EXIT # Handles script exit and Ctrl-C

print_section_header 120 "Starting otto8 server and admin UI..."

# Start the otto server
(
go run main.go server --dev-mode 2>&1 | while IFS= read -r line; do
printf "\033[38;5;183m[server]\033[0m %s\n" "$line"
done
) &
otto_pid=$!

# Start the admin UI
(
cd ui/admin
VITE_API_IN_BROWSER=true npm run dev 2>&1 | while IFS= read -r line; do
printf "\033[38;5;153m[admin-ui]\033[0m %s\n" "$line"
done
) &
npm_pid=$!

# Health check and open the browser
(
source .envrc.dev
healthcheck_passed=false
for _ in {1..60}; do # ~1 minute timeout
if kubectl get --raw /healthz &>/dev/null; then
healthcheck_passed=true
break
fi
sleep 1
done

if [[ "$healthcheck_passed" != true ]]; then
print_section_header 196 "Timeout waiting for otto8 server to be ready"
cleanup
fi

if command -v open >/dev/null; then
open http://localhost:8080/admin/
elif command -v xdg-open >/dev/null; then
xdg-open http://localhost:8080/admin/
else
echo "Open http://localhost:8080/admin/ in your browser."
fi
print_section_header 120 "otto8 server and admin UI ready"
) &
healthcheck_pid=$!

# Wait for either otto or npm process to exit, then trigger cleanup
wait "$healthcheck_pid" "$otto_pid" "$npm_pid"

0 comments on commit a37e69a

Please sign in to comment.