Skip to content

add devcontainer config for codespaces #2248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -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"
}
22 changes: 22 additions & 0 deletions .devcontainer/minimal/devcontainer.json
Original file line number Diff line number Diff line change
@@ -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"
}
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 36 additions & 0 deletions scripts/setup-codespaces.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 37 additions & 1 deletion sndev
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ docker__exec() {
sndev__start() {
shift



if [ $# -eq 0 ]; then
docker__compose up --build
exit 0
Expand Down Expand Up @@ -481,6 +483,14 @@ sndev__login() {
salt="202c90943c313b829e65e3f29164fb5dd7ea3370d7262c4159691c2f6493bb8b"
# upsert user with nym and [email protected]
email="[email protected]"

# 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';
Expand All @@ -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
}

Expand All @@ -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"}}')
Expand Down Expand Up @@ -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
Expand Down