From d2902ccbf3ea364503976497a0f0a75c33198b0e Mon Sep 17 00:00:00 2001 From: Nash Gadre Date: Mon, 28 Oct 2024 10:03:28 -0400 Subject: [PATCH 1/4] adding docker compose changes --- Dockerfile | 15 ++-- docker-compose.yml | 35 +++++++++ fast-server/server/embed.go | 6 ++ fast-server/server/server.go | 67 ++++++++++++------ .../templates/file_directory.html | 0 fast-server/test/ssl | 1 + fast-server/test/www | 1 + logs/.keep | 0 test/config.yaml | 42 +++++++++++ test/ssl/.keep | 1 + .../ssl/domain1.lan/fullchain.pem | 0 .../test => test}/ssl/domain1.lan/privkey.pem | 0 .../ssl/domain2.lan/fullchain.pem | 0 .../test => test}/ssl/domain2.lan/privkey.pem | 0 .../ssl/domain3.lan/fullchain.pem | 0 .../test => test}/ssl/domain3.lan/privkey.pem | 0 .../test => test}/ssl/global/fullchain.pem | 0 .../test => test}/ssl/global/privkey.pem | 0 test/www/.keep | 0 .../www}/domain1.lan/index.html | 0 .../www}/domain2.lan/folder_c/folder_1/a.txt | 0 .../folder_c/folder_1/folder_new/b.txt | 0 .../folder_c/folder_1/temp_10MB_file | Bin .../domain2.lan/folder_c/folder_1/test.conf | 0 .../www}/domain2.lan/folder_c/index.html | 0 .../www}/domain2.lan/index.html | 0 .../www}/domain2.lan/temp_10MB_file | Bin 27 files changed, 140 insertions(+), 28 deletions(-) create mode 100644 docker-compose.yml create mode 100644 fast-server/server/embed.go rename fast-server/{ => server}/templates/file_directory.html (100%) create mode 120000 fast-server/test/ssl create mode 120000 fast-server/test/www create mode 100644 logs/.keep create mode 100644 test/config.yaml create mode 100644 test/ssl/.keep rename {fast-server/test => test}/ssl/domain1.lan/fullchain.pem (100%) rename {fast-server/test => test}/ssl/domain1.lan/privkey.pem (100%) rename {fast-server/test => test}/ssl/domain2.lan/fullchain.pem (100%) rename {fast-server/test => test}/ssl/domain2.lan/privkey.pem (100%) rename {fast-server/test => test}/ssl/domain3.lan/fullchain.pem (100%) rename {fast-server/test => test}/ssl/domain3.lan/privkey.pem (100%) rename {fast-server/test => test}/ssl/global/fullchain.pem (100%) rename {fast-server/test => test}/ssl/global/privkey.pem (100%) create mode 100644 test/www/.keep rename {fast-server/test/public => test/www}/domain1.lan/index.html (100%) rename {fast-server/test/public => test/www}/domain2.lan/folder_c/folder_1/a.txt (100%) rename {fast-server/test/public => test/www}/domain2.lan/folder_c/folder_1/folder_new/b.txt (100%) rename {fast-server/test/public => test/www}/domain2.lan/folder_c/folder_1/temp_10MB_file (100%) rename {fast-server/test/public => test/www}/domain2.lan/folder_c/folder_1/test.conf (100%) rename {fast-server/test/public => test/www}/domain2.lan/folder_c/index.html (100%) rename {fast-server/test/public => test/www}/domain2.lan/index.html (100%) rename {fast-server/test/public => test/www}/domain2.lan/temp_10MB_file (100%) diff --git a/Dockerfile b/Dockerfile index 58610b2..03902b6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,12 +4,15 @@ FROM golang:1.21-alpine as builder # Set the working directory inside the container WORKDIR /app -# Copy the entire project -COPY fast-server . +# Copy go.mod and go.sum first (from fast-server directory) +COPY fast-server/go.mod fast-server/go.sum ./ -# Download all dependencies (this will create go.sum if it doesn't exist) +# Download dependencies RUN go mod download +# Copy the entire fast-server directory +COPY fast-server/ . + # Build the application RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o fast_server . @@ -19,12 +22,12 @@ FROM alpine:latest # Install ca-certificates RUN apk --no-cache add ca-certificates -WORKDIR /root/ +WORKDIR /app -# Copy the pre-built binary file from the previous stage +# Copy the pre-built binary COPY --from=builder /app/fast_server . -# Copy the config file +# Copy the config file from root directory COPY config.yaml.example /etc/fast/config.yaml # Create necessary directories diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..10fabd6 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,35 @@ +version: '3.8' + +services: + fast-server: + build: + context: . + dockerfile: Dockerfile + ports: + - "80:80" + - "443:443" + volumes: + # Configuration from root directory + - ./config.yaml:/etc/fast/config.yaml:ro + + # SSL certificates from root ssl directory + - ./ssl:/etc/fast/ssl:ro + + # Web content from root www directory + - ./www:/var/www/fast + + # Logs to root logs directory + - ./logs:/var/log/fast + environment: + - GO_ENV=production + restart: unless-stopped + deploy: + resources: + limits: + memory: 512M + reservations: + memory: 128M + +volumes: + logs: + driver: local \ No newline at end of file diff --git a/fast-server/server/embed.go b/fast-server/server/embed.go new file mode 100644 index 0000000..fcf81a9 --- /dev/null +++ b/fast-server/server/embed.go @@ -0,0 +1,6 @@ +package server + +import "embed" + +//go:embed templates/*.html +var templateFiles embed.FS diff --git a/fast-server/server/server.go b/fast-server/server/server.go index f2f6fb8..19347aa 100644 --- a/fast-server/server/server.go +++ b/fast-server/server/server.go @@ -5,14 +5,15 @@ import ( "fast/config" "fast/handlers" "fmt" + "github.com/labstack/echo/v4/middleware" "html/template" "io" + "io/fs" "log" "net/http" "strings" "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" ) type Server struct { @@ -38,28 +39,50 @@ func New(cfg *config.Config) *Server { e := echo.New() // Initialize and set the renderer with custom functions - renderer := &TemplateRenderer{ - templates: template.Must(template.New("").Funcs(template.FuncMap{ - "splitPath": func(path string) []string { - // Remove leading and trailing slashes - path = strings.Trim(path, "/") - if path == "" { - return []string{} - } - return strings.Split(path, "/") - }, - "joinPath": func(base, path string) string { - if base == "" { - return "/" + path - } - return base + "/" + path - }, - "lastIndex": func(arr []string) int { - return len(arr) - 1 - }, - }).ParseGlob("templates/*.html")), + tmpl := template.New("") + + // Add custom functions + tmpl = tmpl.Funcs(template.FuncMap{ + "splitPath": func(path string) []string { + path = strings.Trim(path, "/") + if path == "" { + return []string{} + } + return strings.Split(path, "/") + }, + "joinPath": func(base, path string) string { + if base == "" { + return "/" + path + } + return base + "/" + path + }, + "lastIndex": func(arr []string) int { + return len(arr) - 1 + }, + }) + + // Parse embedded templates + templates, err := fs.ReadDir(templateFiles, "templates") + if err != nil { + log.Fatalf("Failed to read templates: %v", err) + } + + for _, entry := range templates { + if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".html") { + templateContent, err := templateFiles.ReadFile("templates/" + entry.Name()) + if err != nil { + log.Fatalf("Failed to read template %s: %v", entry.Name(), err) + } + _, err = tmpl.New(entry.Name()).Parse(string(templateContent)) + if err != nil { + log.Fatalf("Failed to parse template %s: %v", entry.Name(), err) + } + } + } + + e.Renderer = &TemplateRenderer{ + templates: tmpl, } - e.Renderer = renderer // Middleware e.Use(middleware.Logger()) diff --git a/fast-server/templates/file_directory.html b/fast-server/server/templates/file_directory.html similarity index 100% rename from fast-server/templates/file_directory.html rename to fast-server/server/templates/file_directory.html diff --git a/fast-server/test/ssl b/fast-server/test/ssl new file mode 120000 index 0000000..2a94922 --- /dev/null +++ b/fast-server/test/ssl @@ -0,0 +1 @@ +../../test/ssl \ No newline at end of file diff --git a/fast-server/test/www b/fast-server/test/www new file mode 120000 index 0000000..5ee15f6 --- /dev/null +++ b/fast-server/test/www @@ -0,0 +1 @@ +../../test/www \ No newline at end of file diff --git a/logs/.keep b/logs/.keep new file mode 100644 index 0000000..e69de29 diff --git a/test/config.yaml b/test/config.yaml new file mode 100644 index 0000000..a209743 --- /dev/null +++ b/test/config.yaml @@ -0,0 +1,42 @@ +server: + port: 443 + http_port: 80 # for HTTP to HTTPS redirect + +domains: + - name: domain1.lan + type: static + public_dir: /var/www/fast/domain1.lan + ssl: + cert_file: /etc/fast/ssl/domain1.lan/fullchain.pem + key_file: /etc/fast/ssl/domain1.lan/privkey.pem + + - name: domain2.lan + type: file_directory + public_dir: /var/www/fast/domain2.lan + ssl: + cert_file: /etc/fast/ssl/domain2.lan/fullchain.pem + key_file: /etc/fast/ssl/domain2.lan/privkey.pem + + - name: domain3.lan + type: proxy + proxy: + host: 127.0.0.1 + port: 8000 + ssl: + cert_file: /etc/fast/ssl/domain2.lan/fullchain.pem + key_file: /etc/fast/ssl/domain2.lan/privkey.pem + +global_ssl: + cert_file: /etc/fast/ssl/global/fullchain.pem + key_file: /etc/fast/ssl/global/privkey.pem + +log: + file: log/server.log + level: info # Options: debug, info, warn, error + +settings: + read_timeout: 5s + write_timeout: 10s + graceful_shutdown_timeout: 30s + +is_development: true \ No newline at end of file diff --git a/test/ssl/.keep b/test/ssl/.keep new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/test/ssl/.keep @@ -0,0 +1 @@ + diff --git a/fast-server/test/ssl/domain1.lan/fullchain.pem b/test/ssl/domain1.lan/fullchain.pem similarity index 100% rename from fast-server/test/ssl/domain1.lan/fullchain.pem rename to test/ssl/domain1.lan/fullchain.pem diff --git a/fast-server/test/ssl/domain1.lan/privkey.pem b/test/ssl/domain1.lan/privkey.pem similarity index 100% rename from fast-server/test/ssl/domain1.lan/privkey.pem rename to test/ssl/domain1.lan/privkey.pem diff --git a/fast-server/test/ssl/domain2.lan/fullchain.pem b/test/ssl/domain2.lan/fullchain.pem similarity index 100% rename from fast-server/test/ssl/domain2.lan/fullchain.pem rename to test/ssl/domain2.lan/fullchain.pem diff --git a/fast-server/test/ssl/domain2.lan/privkey.pem b/test/ssl/domain2.lan/privkey.pem similarity index 100% rename from fast-server/test/ssl/domain2.lan/privkey.pem rename to test/ssl/domain2.lan/privkey.pem diff --git a/fast-server/test/ssl/domain3.lan/fullchain.pem b/test/ssl/domain3.lan/fullchain.pem similarity index 100% rename from fast-server/test/ssl/domain3.lan/fullchain.pem rename to test/ssl/domain3.lan/fullchain.pem diff --git a/fast-server/test/ssl/domain3.lan/privkey.pem b/test/ssl/domain3.lan/privkey.pem similarity index 100% rename from fast-server/test/ssl/domain3.lan/privkey.pem rename to test/ssl/domain3.lan/privkey.pem diff --git a/fast-server/test/ssl/global/fullchain.pem b/test/ssl/global/fullchain.pem similarity index 100% rename from fast-server/test/ssl/global/fullchain.pem rename to test/ssl/global/fullchain.pem diff --git a/fast-server/test/ssl/global/privkey.pem b/test/ssl/global/privkey.pem similarity index 100% rename from fast-server/test/ssl/global/privkey.pem rename to test/ssl/global/privkey.pem diff --git a/test/www/.keep b/test/www/.keep new file mode 100644 index 0000000..e69de29 diff --git a/fast-server/test/public/domain1.lan/index.html b/test/www/domain1.lan/index.html similarity index 100% rename from fast-server/test/public/domain1.lan/index.html rename to test/www/domain1.lan/index.html diff --git a/fast-server/test/public/domain2.lan/folder_c/folder_1/a.txt b/test/www/domain2.lan/folder_c/folder_1/a.txt similarity index 100% rename from fast-server/test/public/domain2.lan/folder_c/folder_1/a.txt rename to test/www/domain2.lan/folder_c/folder_1/a.txt diff --git a/fast-server/test/public/domain2.lan/folder_c/folder_1/folder_new/b.txt b/test/www/domain2.lan/folder_c/folder_1/folder_new/b.txt similarity index 100% rename from fast-server/test/public/domain2.lan/folder_c/folder_1/folder_new/b.txt rename to test/www/domain2.lan/folder_c/folder_1/folder_new/b.txt diff --git a/fast-server/test/public/domain2.lan/folder_c/folder_1/temp_10MB_file b/test/www/domain2.lan/folder_c/folder_1/temp_10MB_file similarity index 100% rename from fast-server/test/public/domain2.lan/folder_c/folder_1/temp_10MB_file rename to test/www/domain2.lan/folder_c/folder_1/temp_10MB_file diff --git a/fast-server/test/public/domain2.lan/folder_c/folder_1/test.conf b/test/www/domain2.lan/folder_c/folder_1/test.conf similarity index 100% rename from fast-server/test/public/domain2.lan/folder_c/folder_1/test.conf rename to test/www/domain2.lan/folder_c/folder_1/test.conf diff --git a/fast-server/test/public/domain2.lan/folder_c/index.html b/test/www/domain2.lan/folder_c/index.html similarity index 100% rename from fast-server/test/public/domain2.lan/folder_c/index.html rename to test/www/domain2.lan/folder_c/index.html diff --git a/fast-server/test/public/domain2.lan/index.html b/test/www/domain2.lan/index.html similarity index 100% rename from fast-server/test/public/domain2.lan/index.html rename to test/www/domain2.lan/index.html diff --git a/fast-server/test/public/domain2.lan/temp_10MB_file b/test/www/domain2.lan/temp_10MB_file similarity index 100% rename from fast-server/test/public/domain2.lan/temp_10MB_file rename to test/www/domain2.lan/temp_10MB_file From e4859b633c3b2b9cc5c716f3fc8c6ea3a997bed6 Mon Sep 17 00:00:00 2001 From: Nash Gadre Date: Mon, 28 Oct 2024 10:14:19 -0400 Subject: [PATCH 2/4] update docker compose to use .env --- .env.example | 9 ++++++++ docker-compose.yml | 38 ++++++++++++++++++-------------- {logs => fast-server/test}/.keep | 0 test/.keep | 0 test/logs/.keep | 0 5 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 .env.example rename {logs => fast-server/test}/.keep (100%) create mode 100644 test/.keep create mode 100644 test/logs/.keep diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..72f5616 --- /dev/null +++ b/.env.example @@ -0,0 +1,9 @@ +# Host Directory Paths +CONFIG_PATH=./config.yaml +SSL_PATH=./ssl +WWW_PATH=./www +LOG_PATH=./logs + +# Server Ports +HTTP_PORT=80 +HTTPS_PORT=443 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 10fabd6..789b560 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,35 +1,39 @@ +# docker-compose.yaml version: '3.8' +x-default-volumes: &default-volumes + - ${CONFIG_PATH:-./config.yaml}:/etc/fast/config.yaml:ro + - ${SSL_PATH:-./ssl}:/etc/fast/ssl:ro + - ${WWW_PATH:-./www}:/var/www/fast:ro + - ${LOG_PATH:-./logs}:/var/log/fast + services: fast-server: + container_name: fast-server build: context: . dockerfile: Dockerfile ports: - - "80:80" - - "443:443" - volumes: - # Configuration from root directory - - ./config.yaml:/etc/fast/config.yaml:ro - - # SSL certificates from root ssl directory - - ./ssl:/etc/fast/ssl:ro - - # Web content from root www directory - - ./www:/var/www/fast - - # Logs to root logs directory - - ./logs:/var/log/fast + - "${HTTP_PORT:-80}:80" + - "${HTTPS_PORT:-443}:443" + volumes: *default-volumes environment: - GO_ENV=production restart: unless-stopped + healthcheck: + test: ["CMD", "wget", "--spider", "-q", "http://localhost:80"] + interval: 30s + timeout: 10s + retries: 3 deploy: resources: limits: memory: 512M reservations: memory: 128M + networks: + - fast-network -volumes: - logs: - driver: local \ No newline at end of file +networks: + fast-network: + driver: bridge \ No newline at end of file diff --git a/logs/.keep b/fast-server/test/.keep similarity index 100% rename from logs/.keep rename to fast-server/test/.keep diff --git a/test/.keep b/test/.keep new file mode 100644 index 0000000..e69de29 diff --git a/test/logs/.keep b/test/logs/.keep new file mode 100644 index 0000000..e69de29 From 15bba0a39ebf9b1f75fc13d1c1770f5924efeae5 Mon Sep 17 00:00:00 2001 From: Nash Gadre Date: Mon, 28 Oct 2024 10:18:02 -0400 Subject: [PATCH 3/4] update Makefile --- Makefile | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index dade072..222cd1a 100644 --- a/Makefile +++ b/Makefile @@ -5,11 +5,9 @@ INSTALL_DIR=/usr/local/bin CONFIG_DIR=/etc/fast LOG_DIR=/var/log/fast WWW_DIR=/var/www/fast -DOCKER_IMAGE_NAME=fast-server CODE_DIR=fast-server -DOCKER_CONTAINER_NAME=fast-server-container -.PHONY: all linux darwin windows clean install uninstall docker-build docker-run docker-stop +.PHONY: all linux darwin windows clean install uninstall docker-compose-up docker-compose-down docker-compose-build docker-compose-logs all: linux darwin windows @@ -30,6 +28,7 @@ windows: @cd $(CODE_DIR) && go get -d -v && \ mkdir -p $(BUILD_DIR) && \ GOOS=windows go build -o $(BUILD_DIR)/$(BINARY_NAME)-windows.exe main.go + clean: @echo "Cleaning..." @rm -rf $(BUILD_DIR) @@ -59,23 +58,30 @@ uninstall: @sudo systemctl daemon-reload @echo "FAST server uninstalled" -docker-build: - @echo "Building Docker image..." - docker build -t $(DOCKER_IMAGE_NAME) . +# Docker Compose commands +docker-compose-build: + @echo "Building with Docker Compose..." + docker-compose build + +docker-compose-up: + @echo "Starting with Docker Compose..." + docker-compose up -d + +docker-compose-down: + @echo "Stopping with Docker Compose..." + docker-compose down -docker-run: - @echo "Running Docker container..." - docker run -d \ - -p 80:80 \ - -p 443:443 \ - -v $(WWW_DIR):/var/www/fast \ - -v $(CONFIG_DIR)/ssl:/etc/fast/ssl \ - -v $(LOG_DIR):/var/log/fast \ - -v $(CONFIG_DIR)/config.yaml:/etc/fast/config.yaml \ - --name $(DOCKER_CONTAINER_NAME) \ - $(DOCKER_IMAGE_NAME) +docker-compose-logs: + @echo "Viewing Docker Compose logs..." + docker-compose logs -f -docker-stop: - @echo "Stopping Docker container..." - docker stop $(DOCKER_CONTAINER_NAME) - docker rm $(DOCKER_CONTAINER_NAME) \ No newline at end of file +# Initialize development environment +init-dev: + @echo "Initializing development environment..." + @cp config.yaml.example config.yaml + @cp .env.example .env + @mkdir -p ssl/domain1.lan ssl/domain2.lan ssl/global www logs + @echo "Generating test certificates..." + @openssl req -x509 -newkey rsa:4096 -keyout ssl/domain1.lan/privkey.pem -out ssl/domain1.lan/fullchain.pem -days 365 -nodes -subj "/CN=domain1.lan" + @openssl req -x509 -newkey rsa:4096 -keyout ssl/domain2.lan/privkey.pem -out ssl/domain2.lan/fullchain.pem -days 365 -nodes -subj "/CN=domain2.lan" + @openssl req -x509 -newkey rsa:4096 -keyout ssl/global/privkey.pem -out ssl/global/fullchain.pem -days 365 -nodes -subj "/CN=localhost" From b8f1bd270497fb43f2b28ee4e941f087d38eca50 Mon Sep 17 00:00:00 2001 From: Nash Gadre Date: Mon, 28 Oct 2024 10:52:19 -0400 Subject: [PATCH 4/4] update readme --- README.md | 312 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 210 insertions(+), 102 deletions(-) diff --git a/README.md b/README.md index c5921c0..3af4e58 100644 --- a/README.md +++ b/README.md @@ -22,27 +22,33 @@ backend services. - Built-in caching mechanisms to further enhance performance - Support for common web technologies like HTTP/2, SSL/TLS, and compression -## Prerequisites +## Deployment Options +FAST can be deployed in two ways: +1. System Service (Traditional) - For direct system installation +2. Docker Compose (Containerized) - For containerized deployment + +## 1. System Service Deployment + +### Prerequisites - Go 1.16 or higher - Make - Systemd (for service installation) - Root access (for installation) -## Installation +### Installation 1. Clone the repository: - ```bash - git clone https://github.com/pollystack/fast.git - cd fast-server + git clone https://github.com/pollystack/fast.git + cd fast-server ``` + 2. Modify the `config.yaml.example` file to suit your needs and rename it to `config.yaml`. 3. Build and install the server: - ```bash - sudo make install + sudo make install ``` This will: @@ -52,14 +58,101 @@ backend services. - Set up a systemd service 4. Start the server: - ```bash - sudo systemctl start fast + sudo systemctl start fast ``` +### System Service Management + +The server runs as a systemd service: +- Start: `sudo systemctl start fast` +- Stop: `sudo systemctl stop fast` +- Restart: `sudo systemctl restart fast` +- Check status: `sudo systemctl status fast` + +### Uninstallation +To uninstall the system service: + ```bash + sudo make uninstall + ``` + +## 2. Docker Compose Deployment + +### Prerequisites + +* Docker +* Docker Compose +* Make (optional) + +### Quick Start + +1. Initialize development environment: + ```bash + make init-dev + ``` + This will: + * Create necessary directories + * Generate test SSL certificates + * Copy example configuration files + +2. Start the server: + ```bash + make docker-compose-up + ``` + Or directly with Docker Compose: + ```bash + docker-compose up -d + ``` + +### Docker Configuration +The server can be configured through environment variables in .env file: + ```env + # Host Directory Paths + CONFIG_PATH=./config.yaml + SSL_PATH=./ssl + WWW_PATH=./www + LOG_PATH=./logs + + # Server Ports + HTTP_PORT=80 + HTTPS_PORT=443 + ``` + +### Docker Commands +Using Make: + +* Build: `make docker-compose-build` +* Start: `make docker-compose-up` +* Stop: `make docker-compose-down` +* View logs: `make docker-compose-logs` + +Using Docker Compose directly: + +* Build: `docker-compose build` +* Start: `docker-compose up -d` +* Stop: `docker-compose down` +* View logs: `docker-compose logs -f` + +### Docker Directory Structure +``` +├── config.yaml # Server configuration +├── docker-compose.yaml # Docker Compose configuration +├── .env # Environment variables +├── ssl/ # SSL certificates +│ ├── domain1.lan/ +│ ├── domain2.lan/ +│ └── global/ +├── www/ # Web content +└── logs/ # Server logs +``` + ## Configuration -The server is configured via the `config.yaml` file. Here's an example configuration: +The server is configured via the `config.yaml` file. The file location depends on your deployment method: +- System Service: `/etc/fast/config.yaml` +- Docker: `./config.yaml` (mounted to `/etc/fast/config.yaml` in container) + +Here's an example configuration: ```yaml server: @@ -67,7 +160,6 @@ server: http_port: 80 # for HTTP to HTTPS redirect domains: - - name: static.example.com type: static public_dir: /var/www/fast/static.example.com @@ -85,7 +177,7 @@ domains: - name: api.example.com type: proxy proxy: - host: 192.168.1.100 + host: 127.0.0.1 port: 8000 ssl: cert_file: /etc/fast/ssl/api.example.com/fullchain.pem @@ -103,58 +195,68 @@ settings: read_timeout: 5s write_timeout: 10s graceful_shutdown_timeout: 30s - -is_development: false -``` - - -## Usage - -### Starting the Server - -The server is set up as a systemd service during installation. You can manage it using standard systemd commands: - -- Start: `sudo systemctl start fast` -- Stop: `sudo systemctl stop fast` -- Restart: `sudo systemctl restart fast` -- Check status: `sudo systemctl status fast` - -### Logs -Logs are stored in /var/log/fast/server.log by default. You can view them using: - -```bash -sudo tail -f /var/log/fast/server.log -``` - -## Usage Docker - -### Prerequisites -- Docker -- Make - -### Build -Build the Docker image: -```bash -make docker-build -``` - -### Run -Start the FAST server in a Docker container: -```bash -make docker-run ``` -### Stop -Stop and remove the running Docker container: -```bash -make docker-stop +### Common Configuration + +Domain Types + +1. **Static Sites** `(type: static)` + * Serves static files from a specified directory + * Perfect for HTML, CSS, JS, and other static assets + ```yaml + type: static + public_dir: /var/www/fast/example.com + ``` +2. **File Directory** `(type: file_directory)` + * Serves directory listings with download capabilities + * Supports resume on disconnect for large files + ```yaml + type: file_directory + public_dir: /var/www/fast/files + ``` +3. **Reverse Proxy** `(type: proxy)` + * Forwards requests to backend services + * Supports HTTP/HTTPS backends + ```yaml + type: proxy + proxy: + host: 127.0.0.1 + port: 8000 + ``` + +### SSL Configuration +SSL certificates can be configured per domain or globally: + +1. **Per Domain** + ```yaml + ssl: + cert_file: /etc/fast/ssl/domain.com/fullchain.pem + key_file: /etc/fast/ssl/domain.com/privkey.pem + ``` +2. **Global Fallback** + ```yaml + global_ssl: + cert_file: /etc/fast/ssl/global/fullchain.pem + key_file: /etc/fast/ssl/global/privkey.pem + ``` + +### Logging + +Configure logging behavior: +```yaml +log: + file: /var/log/fast/server.log + level: info # Options: debug, info, warn, error ``` -### Logs -View logs from the Docker container: - -```bash -docker logs fast-server-container +### Performance Settings +Fine-tune server performance: +```yaml +settings: + read_timeout: 5s + write_timeout: 10s + graceful_shutdown_timeout: 30s ``` ## Development @@ -164,61 +266,67 @@ docker logs fast-server-container - Go 1.16 or higher - gops tool (optional, for improved debug detection) -### Generate Test Certs - -```bash -openssl req -x509 -newkey rsa:4096 -keyout fast-server/test/ssl/domain1.lan/privkey.pem -out fast-server/test/ssl/domain1.lan/fullchain.pem -days 365 -nodes -subj "/CN=domain1.lan" -openssl req -x509 -newkey rsa:4096 -keyout fast-server/test/ssl/domain2.lan/privkey.pem -out fast-server/test/ssl/domain2.lan/fullchain.pem -days 365 -nodes -subj "/CN=domain2.lan" -openssl req -x509 -newkey rsa:4096 -keyout fast-server/test/ssl/global/privkey.pem -out fast-server/test/ssl/global/fullchain.pem -days 365 -nodes -subj "/CN=localhost" - -``` - -To install gops: -```bash -go install github.com/google/gops@latest - -``` - -### Project Structure +### Directory Structure ```azure fast-server/ - ├── main.go ├── config/ - │ └── config.go - ├── server/ - │ └── server.go + │ └── config.go # Configuration handling ├── handlers/ - │ └── handlers.go - ├── public/ - │ └── ... (static files) - ├── Makefile - ├── config.yaml.exmple - └── README.md - └── Dockerfile - └── LICENSE + │ ├── handlers.go # Common handler functions + │ ├── static_handler.go # Static file handling + │ ├── proxy_handler.go # Reverse proxy handling + │ └── file_directory_handler.go # Directory listing + ├── server/ + │ ├── server.go # Main server implementation + │ ├── embed.go # Embedded templates + │ └── templates/ # HTML templates + ├── main.go # Application entry point + └── test/ # Test files and fixtures ``` -### Building - -- Build for all platforms: `make all` -- Build for Linux: `make linux` -- Build for macOS: `make darwin` -- Build for Windows: `make windows` - -### Cleaning +### Generate Test Certs -To remove all built binaries: ```bash -make clean +openssl req -x509 -newkey rsa:4096 -keyout fast-server/test/ssl/domain1.lan/privkey.pem -out fast-server/test/ssl/domain1.lan/fullchain.pem -days 365 -nodes -subj "/CN=domain1.lan" +openssl req -x509 -newkey rsa:4096 -keyout fast-server/test/ssl/domain2.lan/privkey.pem -out fast-server/test/ssl/domain2.lan/fullchain.pem -days 365 -nodes -subj "/CN=domain2.lan" +openssl req -x509 -newkey rsa:4096 -keyout fast-server/test/ssl/global/privkey.pem -out fast-server/test/ssl/global/fullchain.pem -days 365 -nodes -subj "/CN=localhost" + ``` -## Uninstallation -To uninstall the server: +### Debug Tools +Install gops for runtime debugging: ```bash -sudo make uninstall +go install github.com/google/gops@latest ``` -This will stop the service, remove the binary, configuration, and created directories. + +### Local Development +1. Create development configuration: + ```bash + cp config.yaml.example config.yaml + ``` +2. Set up test directories: + ```bash + mkdir -p www/domain1.lan www/domain2.lan ssl/domain1.lan ssl/domain2.lan ssl/global logs + ``` +3. Generate test certificates (see above) +4. Build and run: + ```bash + make linux + ./builds/fast_server + ``` +### Testing +Test different domain configurations: + +1. Add test domains to /etc/hosts: + ``` + 127.0.0.1 domain1.lan domain2.lan + ``` +2. Access test sites: + ``` + https://domain1.lan + https://domain2.lan + ``` ## Acronym