Skip to content

Commit

Permalink
feat: add devcontainer support (#234)
Browse files Browse the repository at this point in the history
* style: format

* feat: devcontainer

* feat: minio

* fix: pgadmin password

* fix: remove `just env`

* feat: adjust configurations

* feat: custom docker image

* fix: direnv

* fix: initialize minio
  • Loading branch information
uonr authored Dec 25, 2023
1 parent abeac25 commit 41eefcd
Show file tree
Hide file tree
Showing 17 changed files with 222 additions and 76 deletions.
7 changes: 7 additions & 0 deletions .devcontainer/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
POSTGRES_USER=boluo
POSTGRES_PASSWORD=boluo
PGADMIN_DEFAULT_EMAIL=[email protected]
PGADMIN_DEFAULT_PASSWORD=boluo
PGADMIN_LISTEN_PORT=4841
MINIO_ROOT_USER=boluo
MINIO_ROOT_PASSWORD=boluo-development
19 changes: 19 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM ubuntu:latest
RUN apt update -y
RUN apt install -y curl xz-utils git && mkdir /nix
RUN \
useradd -m -s /bin/bash -u 1000 dev;\
chown dev /nix;\
mkdir -p /etc/nix;\
echo "experimental-features = nix-command flakes" >> /etc/nix/nix.conf
ENV USER=dev
USER dev
WORKDIR /home/dev
RUN curl -L https://nixos.org/nix/install | sh -s -- --no-daemon;
ENV PATH="/home/dev/.nix-profile/bin:${PATH}"

ARG PNPM_STORE_DIR=/home/dev/.local/share/pnpm
ENV PNPM_STORE_DIR=${PNPM_STORE_DIR}
RUN mkdir -p "${PNPM_STORE_DIR}"
COPY --chown=dev:dev flake.lock flake.nix rust-toolchain.toml ./
RUN nix develop --command bash -c "pnpm config set store-dir ${PNPM_STORE_DIR}" && rm flake.lock flake.nix rust-toolchain.toml
36 changes: 36 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/javascript-node-postgres
{
"name": "Boluo",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
"ghcr.io/christophermacgown/devcontainer-features/direnv:1": {}
},

// Configure tool-specific properties.
"customizations": {
"vscode": {
"extensions": [
"EditorConfig.EditorConfig",
"bradlc.vscode-tailwindcss",
"dprint.dprint",
"rust-lang.rust-analyzer",
"mkhl.direnv",
"dbaeumer.vscode-eslint"
]
}
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// This can be used to network with other containers or with the host.
"forwardPorts": [3000, 4841, 9000, 9090, 8080],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "cp .env.local.example .env.local && direnv allow",

// More info: https://aka.ms/dev-containers-non-root.
"remoteUser": "dev",
"containerUser": "dev"
}
93 changes: 93 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
version: '3.8'

services:
app:
build:
context: ..
dockerfile: .devcontainer/Dockerfile

volumes:
- ../..:/workspaces:cached

# Overrides default command so things don't shut down after the process ends.
command: sleep infinity

# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
network_mode: service:db

# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)

redis:
image: redis
restart: unless-stopped

db:
image: postgres:latest
restart: unless-stopped
volumes:
- postgres-data:/var/lib/postgresql/data
- ../apps/server/schema.sql:/docker-entrypoint-initdb.d/schema.sql:ro
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}

db-test:
image: postgres:latest
restart: unless-stopped
volumes:
- ../apps/server/schema.sql:/docker-entrypoint-initdb.d/schema.sql:ro
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}

pgadmin:
image: dpage/pgadmin4
restart: unless-stopped
volumes:
- ./servers.json:/pgadmin4/servers.json:ro
depends_on:
- db
- db-test
environment:
- PGADMIN_DEFAULT_EMAIL=${PGADMIN_DEFAULT_EMAIL}
- PGADMIN_DEFAULT_PASSWORD=${PGADMIN_DEFAULT_PASSWORD}
- PGADMIN_LISTEN_PORT=${PGADMIN_LISTEN_PORT}
- PGADMIN_CONFIG_SERVER_MODE=FALSE
- PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED=False
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
network_mode: service:db

store:
image: quay.io/minio/minio
restart: unless-stopped
network_mode: service:db
volumes:
- minio-data:/data
environment:
- MINIO_ROOT_USER=${MINIO_ROOT_USER}
- MINIO_ROOT_PASSWORD=${MINIO_ROOT_PASSWORD}
command: server /data --console-address ":9090"


store-init:
image: quay.io/minio/mc
network_mode: service:db
depends_on:
- store
entrypoint: >
/bin/sh -c "
sleep 10;
/usr/bin/mc config host add devminio http://127.0.0.1:9000 ${MINIO_ROOT_USER} ${MINIO_ROOT_PASSWORD};
/usr/bin/mc rm -r --force devminio/boluo;
/usr/bin/mc mb devminio/boluo;
/usr/bin/mc anonymous set public devminio/boluo;
exit 0;
"
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)

volumes:
nix-store:
postgres-data:
minio-data:
21 changes: 21 additions & 0 deletions .devcontainer/servers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"Servers": {
"1": {
"Name": "boluo",
"Group": "Servers",
"Host": "db",
"Port": 5432,
"MaintenanceDB": "postgres",
"Username": "boluo",
"UseSSHTunnel": 0,
"TunnelPort": "22",
"TunnelAuthentication": 0,
"KerberosAuthentication": false,
"ConnectionParameters": {
"sslmode": "prefer",
"connect_timeout": 10
},
"PasswordExecCommand": "echo $POSTGRES_PASSWORD"
}
}
}
12 changes: 12 additions & 0 deletions .env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
DATABASE_URL=postgresql://boluo:boluo@db/boluo?sslmode=disable
TEST_DATABASE_URL=postgresql://boluo:boluo@db-test/boluo?sslmode=disable
REDIS_URL=redis://redis:6379
BACKEND_URL=http://127.0.0.1:3000

SECRET=SOME_SECRET
S3_ACCESS_KEY_ID=boluo
S3_SECRET_ACCESS_KEY=boluo-development
S3_ENDPOINT_URL=http://127.0.0.1:9000
S3_BUCKET_NAME=boluo

PUBLIC_MEDIA_URL=http://127.0.0.1:9000/boluo
11 changes: 0 additions & 11 deletions .env.local.template

This file was deleted.

1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

.bin/
/media
.envrc
.env.local
.env*.local
**/*.tsbuildinfo
Expand Down Expand Up @@ -49,4 +48,4 @@ testem.log
.DS_Store
Thumbs.db

.nx/cache
.nx/cache
10 changes: 6 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"files.associations": {
"tailwind.css": "tailwindcss",
"*.tailwind.css": "tailwindcss",
".env.local.example": "properties"
},
// ESLint
"eslint.workingDirectories": [
{
Expand Down Expand Up @@ -37,8 +42,5 @@
"editor.quickSuggestions": {
"strings": true
},
"tailwindCSS.experimental.configFile": "packages/ui/tailwind.config.mjs",
"files.associations": {
"tailwind.css": "tailwindcss"
}
"tailwindCSS.experimental.configFile": "packages/ui/tailwind.config.mjs"
}
4 changes: 2 additions & 2 deletions apps/server/bindings/ClientEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import type { PreviewPost } from './PreviewPost';
import type { StatusKind } from './StatusKind';

export type ClientEvent = { type: 'PREVIEW'; preview: PreviewPost } | {
type: 'STATUS';
export type ClientEvent = { 'type': 'PREVIEW'; preview: PreviewPost } | {
'type': 'STATUS';
kind: StatusKind;
focus: Array<string>;
};
24 changes: 12 additions & 12 deletions apps/server/bindings/EventBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import type { SpaceWithRelated } from './SpaceWithRelated';
import type { UserStatus } from './UserStatus';

export type EventBody =
| { type: 'NEW_MESSAGE'; channelId: string; message: Message; previewId: string | null }
| { type: 'MESSAGE_DELETED'; messageId: string; channelId: string }
| { type: 'MESSAGE_EDITED'; channelId: string; message: Message }
| { type: 'MESSAGE_PREVIEW'; channelId: string; preview: Preview }
| { type: 'CHANNEL_DELETED'; channelId: string }
| { type: 'CHANNEL_EDITED'; channelId: string; channel: Channel }
| { type: 'MEMBERS'; channelId: string; members: Array<Member> }
| { type: 'BATCH'; encodedEvents: Array<string> }
| { type: 'INITIALIZED' }
| { type: 'STATUS_MAP'; statusMap: Record<string, UserStatus>; spaceId: string }
| { type: 'SPACE_UPDATED'; spaceWithRelated: SpaceWithRelated }
| { type: 'APP_UPDATED'; version: string };
| { 'type': 'NEW_MESSAGE'; channelId: string; message: Message; previewId: string | null }
| { 'type': 'MESSAGE_DELETED'; messageId: string; channelId: string }
| { 'type': 'MESSAGE_EDITED'; channelId: string; message: Message }
| { 'type': 'MESSAGE_PREVIEW'; channelId: string; preview: Preview }
| { 'type': 'CHANNEL_DELETED'; channelId: string }
| { 'type': 'CHANNEL_EDITED'; channelId: string; channel: Channel }
| { 'type': 'MEMBERS'; channelId: string; members: Array<Member> }
| { 'type': 'BATCH'; encodedEvents: Array<string> }
| { 'type': 'INITIALIZED' }
| { 'type': 'STATUS_MAP'; statusMap: Record<string, UserStatus>; spaceId: string }
| { 'type': 'SPACE_UPDATED'; spaceWithRelated: SpaceWithRelated }
| { 'type': 'APP_UPDATED'; version: string };
4 changes: 1 addition & 3 deletions apps/server/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ pub fn media_public_url() -> &'static str {
static MEDIA_PUBLIC_URL: OnceCell<String> = OnceCell::new();
MEDIA_PUBLIC_URL.get_or_init(|| {
let url = env::var("PUBLIC_MEDIA_URL").unwrap_or_default();
let url = url.trim_end_matches('/');
let url = url.trim_start_matches("https://");
format!("https://{}", url)
url.trim_end_matches('/').to_owned()
})
}

Expand Down
9 changes: 9 additions & 0 deletions apps/server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ async fn main() {
// https://tokio.rs/tokio/topics/shutdown
let mut stream = signal(SignalKind::terminate()).unwrap();

// check
let s3_client = s3::get_client();
s3_client
.head_bucket()
.bucket(s3::get_bucket_name())
.send()
.await
.expect("s3 bucket not found");

#[allow(clippy::never_loop)]
loop {
tokio::select! {
Expand Down
15 changes: 0 additions & 15 deletions docker-compose.yaml

This file was deleted.

2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ check:
lint:
pnpm exec nx run-many --target=lint
pnpm exec dprint check
cargo fmt --check

dev projects="*":
pnpm exec nx run-many --target=dev --projects={{projects}} --parallel=16
Expand All @@ -28,6 +29,7 @@ install:

format:
pnpm exec dprint fmt
cargo fmt

generate:
pnpm exec nx run-many --target=generate
27 changes: 0 additions & 27 deletions migrations.json

This file was deleted.

0 comments on commit 41eefcd

Please sign in to comment.