Skip to content

Interactive Go script that configures Caddy as a reverse proxy for local development.

License

henrriusdev/autocaddy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

autocaddy

A simple, interactive Go script that automatically scans a project directory and configures Caddy as a reverse proxy for local development.

This tool provides a lightweight, language-agnostic alternative to heavier tools, giving you .localhost domains for all your projects (Go, Node, Vite, React, etc.) with automatic HTTPS on Linux, macOS, or Windows (via WSL).

Features

  • Automatic Project Scanning: Scans a root directory for all your projects.
  • Heuristic Port Detection: Tries to guess the correct port based on project type:
    • go.mod (Go): 8080
    • package.json with vite: 5173
    • package.json with next or react-scripts: 3000
    • Generic Node.js: 3000
  • Smart PORT Detection: Reads .env files to find PORT or APP_PORT variables.
  • Interactive TUI: If it can't guess, it provides a simple terminal prompt to ask for the port.
  • Configuration Caching: Saves your port choice in a .caddyport file within the project folder, so you're only asked once.
  • Automatic Caddy Reload: Generates a new Caddyfile and reloads the Caddy service instantly.

1. Environment Setup

This tool runs in a terminal. You must first install its dependencies: Go and Caddy.

For Linux (Native)

Install dependencies using your package manager.

# Arch Linux
sudo pacman -S go caddy

# Debian/Ubuntu
sudo apt install go caddy
```

### For macOS

Install dependencies using Homebrew.

```bash
brew install go caddy
```

### For Windows (using WSL)

Your environment is **inside WSL**. All commands should be run there.

1.  **Install Dependencies (in WSL):**
    Use your WSL distribution's package manager.

    ```bash
    # Example for Arch WSL
    sudo pacman -S go caddy
    ```

2.  **Configure Network Bridging:**
    To allow your Windows browser to access Caddy (running in WSL), you **must** enable `localhostForwarding`.

    On your **Windows machine** (not in WSL), edit or create the file `C:\Users\<Your-Windows-User>\.wslconfig` and add the following:

    ```ini
    [wsl2]
    localhostForwarding=true
    ```

    After saving, open a **PowerShell** terminal and run `wsl --shutdown` to apply the changes.

## 2\. Configure `autocaddy`

This step is the same for all environments.

Before you run the script, you **must** change the hardcoded paths in `main.go` to match your system.

Edit `main.go` and update these two constants:

```go
const (
	// The root folder where you store your projects
	projectsDir = "/home/henrrius/code" // <<< CHANGE THIS to your code folder

	// The location of your main Caddyfile
	caddyfilePath = "/home/henrrius/Caddyfile" // <<< CHANGE THIS to where you want to store Caddy's config
)
```

## 3\. HTTPS Setup (Trusting the CA)

To get the green "Secure" lock 🔒 in your browser, you need to trust Caddy's local root certificate.

### Step 1: Generate & Find the Certificate (All Environments)

1.  **Run Caddy once with `sudo`:** In your terminal (WSL, Linux, or macOS), run Caddy pointing to your (still empty) Caddyfile. This will generate the CA.

    ```bash
    # Use the path you defined in caddyfilePath
    sudo caddy run --config /home/henrrius/Caddyfile
    ```

    Caddy will ask for your `sudo` password to install the CA *in that environment's* trust store. Let it run.

2.  **Find the Root Certificate:** The CA is now generated. It's usually in `/root/.local/share/caddy/pki/authorities/local/root.crt`.

### Step 2: Install the Certificate (By OS)

You must install this certificate in the trust store of the OS where your **browser** runs.

#### On Windows (from WSL)

1.  **Copy the Certificate to Windows:** From your **WSL terminal**, copy the file to your Windows desktop.
    ```bash
    # Replace <your-windows-user> with your Windows username
    cp /root/.local/share/caddy/pki/authorities/local/root.crt /mnt/c/Users/<your-windows-user>/Desktop/
    ```
2.  **Install in Windows:**
      - Go to your Windows desktop and double-click the `root.crt` file.
      - Click "Install Certificate...".
      - Select Store Location: **"Local Machine"** (Click "Yes" on the admin prompt).
      - Select **"Place all certificates in the following store"**.
      - Click "Browse..." and select **"Trusted Root Certification Authorities"**.
      - Click "Next" and "Finish".

#### On macOS

1.  **Copy the Certificate:** From your **macOS terminal**, copy the file to your Desktop.
    ```bash
    sudo cp /root/.local/share/caddy/pki/authorities/local/root.crt ~/Desktop/caddy_root.crt
    # You may need to chown it to make it readable
    sudo chown $(whoami) ~/Desktop/caddy_root.crt
    ```
2.  **Install in Keychain:**
      - Open the **"Keychain Access"** app.
      - Drag the `caddy_root.crt` file from your Desktop into the **"System"** keychain (on the left sidebar).
      - You will be prompted for your password.
      - Find the certificate (it will be "Caddy Local Authority"), double-click it.
      - Expand the **"Trust"** section.
      - Change "When using this certificate:" to **"Always Trust"**.
      - Close the window (you'll be prompted for your password again).

#### On Desktop Linux

The `sudo caddy run` command *should* have already installed it for you using `nss-tools`. If it failed, you can install it manually.

1.  **Install `nss-tools`:**
    ```bash
    # Arch
    sudo pacman -S nss
    # Debian/Ubuntu
    sudo apt install libnss3-tools
    ```
2.  **Install the certificate:**
    ```bash
    certutil -A -n "Caddy Local Authority" -t "C,," -i /root/.local/share/caddy/pki/authorities/local/root.crt -d sql:$HOME/.pki/nssdb
    ```

**Finally, restart your browser** to apply the changes.

## 4\. Usage Workflow

Your local development workflow will now involve two main processes.

### 1\. Terminal 1: Run Caddy

Caddy must be running to serve your sites. Run this in its own terminal and leave it open.

**Note:** You must use `sudo` to allow Caddy to bind to privileged ports 80 (HTTP) and 443 (HTTPS).

```bash
# Use the path you defined in caddyfilePath
sudo caddy run --config /home/henrrius/Caddyfile
```

### 2\. Terminal 2: Run `autocaddy`

The first time you run this (and any time you add a new project), run this script.

```bash
# Navigate to the autocaddy script directory
cd /path/to/autocaddy

# Run the script
go run main.go
```

The script will scan your `projectsDir`, detect all your projects, and (if necessary) ask you for their ports. It will then generate the `Caddyfile` and reload Caddy automatically.

### 3\. Terminal 3 (and 4, 5...): Run Your Projects

Start your actual application.

```bash
# Example
cd /home/henrrius/code/my-api
go run .
```

You can now visit `https://my-api.localhost` in your browser\!

## 5\. How Port Detection Works

The script finds a port for each project folder using this priority order:

1.  **`.caddyport` file:** (e.g., `my-api/.caddyport`) If this file exists, its content is used as the port. This is the manual override and is created automatically when you answer the interactive prompt.
2.  **`.env` file:** The script parses `.env` files looking for `PORT=...` or `APP_PORT=...`.
3.  **File Heuristics:** If no port is found, it looks for known files:
      - `go.mod`: Assumes port `8080`.
      - `package.json` (with `vite`): Assumes port `5173`.
      - `package.json` (with `next` or `react-scripts`): Assumes port `3000`.
4.  **Interactive Prompt:** If all else fails, it will ask you directly.

About

Interactive Go script that configures Caddy as a reverse proxy for local development.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages