add devcontainer config for codespaces (#2248)

* add devcontainer config for codespaces

* fix issues in documenting changes.
This commit is contained in:
Bryan Mutai 2025-08-25 20:47:37 +03:00 committed by GitHub
parent 616c9b3440
commit fd05585eaa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 145 additions and 1 deletions

View File

@ -0,0 +1,21 @@
{
"name": "sndev",
"hostRequirements": {
"cpus": 4,
"memory": "16gb",
"storage": "32gb"
},
"customizations": {
"vscode": {
"extensions": [
"ms-azuretools.vscode-containers"
]
}
},
"containerEnv": {
"CPU_SHARES_IMPORTANT": "1024",
"CPU_SHARES_MODERATE": "512",
"CPU_SHARES_LOW": "128"
},
"postAttachCommand": "./scripts/setup-codespaces.sh && source .env.local && ./sndev start"
}

View File

@ -0,0 +1,22 @@
{
"name": "sndev MINIMAL",
"hostRequirements": {
"cpus": 4,
"memory": "16gb",
"storage": "32gb"
},
"customizations": {
"vscode": {
"extensions": [
"ms-azuretools.vscode-containers"
]
}
},
"containerEnv": {
"CPU_SHARES_IMPORTANT": "1024",
"CPU_SHARES_MODERATE": "512",
"CPU_SHARES_LOW": "128",
"COMPOSE_PROFILES": "minimal"
},
"postAttachCommand": "./scripts/setup-codespaces.sh && source .env.local && ./sndev start"
}

View File

@ -36,6 +36,34 @@ Go to [localhost:3000](http://localhost:3000).
<br>
### GitHub Codespaces
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/stackernews/stacker.news)
You can run Stacker News on Github Codespaces
#### Setup
1. Open the repository on GitHub and click the **"Code"** button
2. Select the Codespaces tab and create a new codespace.
- You can also configure your codespace to run select services based on `COMPOSE_PROFILES` as well as in a different region and machine type by clicking "..." and selecting "New with options...". Check [Modifying services](#modifying-services) for more information on `COMPOSE_PROFILES`
3. Wait for the environment to set up (this may take several minutes the first time)
4. Once ready, you'll see a terminal with the environment initialized
#### Usage
After the codespace is created, the development environment will be automatically set up and services started.
Access your running application at the URL shown in the forwarded ports panel (typically `https://your-codespace-name-3000.app.github.dev`).
#### Port Configuration
⚠️ **Important**: For various internal services and external access to work properly, you must set forwarded ports to **Public** in the Ports tab:
1. In your codespace, look for the "PORTS" tab in the bottom panel
2. Click the lock icon to change visibility from "Private" to "Public"
<br>
## Usage
Start the development environment
@ -170,6 +198,7 @@ To add/remove DNS records you can now use `./sndev domains dns`. More on this [h
# Table of Contents
- [Getting started](#getting-started)
- [Installation](#installation)
- [GitHub Codespaces](#github-codespaces)
- [Usage](#usage)
- [Modifying services](#modifying-services)
- [Running specific services](#running-specific-services)

36
scripts/setup-codespaces.sh Executable file
View File

@ -0,0 +1,36 @@
#!/bin/bash
if [ -z "$CODESPACE_NAME" ]; then
echo "Not in a Codespaces environment, skipping setup"
exit 0
fi
echo "Setting up Codespaces environment variables..."
[ ! -f .env.local ] && touch .env.local && echo "Created .env.local" || echo ".env.local already exists, will preserve existing content"
declare -A env_vars=(
["NEXTAUTH_URL"]="https://${CODESPACE_NAME}-3000.app.github.dev/api/auth"
["NEXT_PUBLIC_MEDIA_URL"]="https://${CODESPACE_NAME}-4566.app.github.dev/uploads"
["LNAUTH_URL"]="https://${CODESPACE_NAME}-3000.app.github.dev/api/lnauth"
["LNWITH_URL"]="https://${CODESPACE_NAME}-3000.app.github.dev/api/lnwith"
["PUBLIC_URL"]="https://${CODESPACE_NAME}-3000.app.github.dev"
["NEXT_PUBLIC_URL"]="https://${CODESPACE_NAME}-3000.app.github.dev"
["NEXT_PUBLIC_IMGPROXY_URL"]="https://${CODESPACE_NAME}-3001.app.github.dev"
["IMGPROXY_ALLOW_ORIGIN"]="https://${CODESPACE_NAME}-3000.app.github.dev"
["NEXT_PUBLIC_MEDIA_DOMAIN"]="${CODESPACE_NAME}-4566.app.github.dev"
)
# Remove existing Codespaces-related entries to avoid duplicates
for var in "${!env_vars[@]}"; do
sed -i.bak "/^${var}=/d" .env.local 2>/dev/null || true
done
# Add Codespaces environment variables
echo "# Codespaces environment variables" >> .env.local
for var in "${!env_vars[@]}"; do
echo "${var}=${env_vars[$var]}" >> .env.local
export "$var"="${env_vars[$var]}"
done
rm -f .env.local.bak 2>/dev/null || true

38
sndev
View File

@ -39,6 +39,8 @@ docker__exec() {
sndev__start() {
shift
if [ $# -eq 0 ]; then
docker__compose up --build
exit 0
@ -481,6 +483,14 @@ sndev__login() {
salt="202c90943c313b829e65e3f29164fb5dd7ea3370d7262c4159691c2f6493bb8b"
# upsert user with nym and nym@sndev.team
email="$1@sndev.team"
# Detect Codespaces and set the correct base URL
if [ -n "$CODESPACE_NAME" ]; then
BASE_URL="https://${CODESPACE_NAME}-3000.app.github.dev"
else
BASE_URL="http://localhost:3000"
fi
docker__exec db psql -U sn -d stackernews -q <<EOF
INSERT INTO users (name) VALUES ('$1') ON CONFLICT DO NOTHING;
UPDATE users SET email = '$email', "emailHash" = encode(digest(LOWER('$email')||'$salt', 'sha256'), 'hex') WHERE name = '$1';
@ -492,7 +502,7 @@ EOF
echo
echo "open url in browser"
echo "http://localhost:3000/api/auth/callback/email?token=SNDEV-TOKEN&email=$1%40sndev.team"
echo "$BASE_URL/api/auth/callback/email?token=SNDEV-TOKEN&email=$1%40sndev.team"
echo
}
@ -507,6 +517,29 @@ USAGE
echo "$help"
}
sndev__setup_codespaces() {
shift
./scripts/setup-codespaces.sh
}
sndev__help_setup_codespaces() {
help="
setup Codespaces environment variables
USAGE
$ sndev setup_codespaces
DESCRIPTION
Creates or updates .env.local with the correct URLs for GitHub Codespaces.
This is automatically run when the Codespace is created.
REQUIREMENTS
- Set forwarded ports to 'Public' in the Ports tab for external access
"
echo "$help"
}
sndev__onion() {
shift
tordir=$(docker__compose ps $1 --format '{{.Label "TORDIR"}}')
@ -636,6 +669,9 @@ COMMANDS
login login as a nym
set_balance set the balance of a nym
codespaces:
setup_codespaces setup environment for GitHub Codespaces
lightning:
fund pay a bolt11 for funding
withdraw create a bolt11 for withdrawal