dryLang
docsplaygrounddownloadgithub

Getting Started

  • Home
  • Getting Started
  • Setup

Language Basics

  • Variables
  • Types
  • Operators
  • Comments
  • Strings

Control Flow

  • Control Flow
  • Loops

Advanced Concepts

  • Functions
  • Structs
  • Collections
  • Modules
  • HTTP Server
  • Database

Reference

  • Built-ins
  • Error Handling
  • Errors

Examples

  • Hello
  • Variables
  • Functions
  • Control Flow
  • Structs
  • Arrays Maps
  • Try Err
  • Imports
  • Math
  • Strings
  • Http Server
  • Database
  • File Io

Templates

  • Automation
  • Cli Tool
  • Crud
  • Fetch Json
  • File Server
  • Hello
  • Html Render
  • Rest Api
Docs Menu

Getting Started

  • Home
  • Getting Started
  • Setup

Language Basics

  • Variables
  • Types
  • Operators
  • Comments
  • Strings

Control Flow

  • Control Flow
  • Loops

Advanced Concepts

  • Functions
  • Structs
  • Collections
  • Modules
  • HTTP Server
  • Database

Reference

  • Built-ins
  • Error Handling
  • Errors

Examples

  • Hello
  • Variables
  • Functions
  • Control Flow
  • Structs
  • Arrays Maps
  • Try Err
  • Imports
  • Math
  • Strings
  • Http Server
  • Database
  • File Io

Templates

  • Automation
  • Cli Tool
  • Crud
  • Fetch Json
  • File Server
  • Hello
  • Html Render
  • Rest Api

File Server

drylang
// template: file-server
// desc: Basic static file server serving files from the current directory

fn handler(req) {
    path = req["path"]
    
     // Default to index.html for root path
    if path = "/" {
        path = "/index.html"
    }

     // Remove leading slash for local file path
    local_path = mod(path, "/", "")

    try {
        content = r(local_path)
        
         // Basic content type guessing
        ctype = "text/plain"
        if has(local_path, ".html") { ctype = "text/html" }
        elif has(local_path, ".json") { ctype = "application/json" }
        elif has(local_path, ".css") { ctype = "text/css" }
        elif has(local_path, ".js") { ctype = "application/javascript" }

        rev {
            "status": 200,
            "headers": {"Content-Type": ctype},
            "body": content
        }
    } err(e) {
        rev {
            "status": 404,
            "body": "File not found: " + local_path
        }
    }
}

PORT 8080
pt "Serving files on http://localhost:${PORT}"
op(PORT, handler, "mul", 100)
PreviousFetch JsonNextHello