Genie.jl Documentation

repository·main·Indexed 25 days ago

https://github.com/genieframework/genie.jl

Genie.jl is a high-performance web framework for the Julia programming language and the backend for the Genie Framework ecosystem. It provides tools for routing, HTML and JSON rendering, WebSocket communication, and HTTP header management. The framework includes modules for configuration, cookie handling, data encryption, and project scaffolding via FileTemplates and Generator. It integrates with SearchLight.jl for ORM and database migrations, and supports plugins like GenieAuthentication.

Tokens
26.7K
Snippets
82
Records
178
Agent score
81%

What's inside Genie.jl

  1. Overview of the Genie web framework

    main

    Genie is a full-stack web framework for the Julia programming language designed for high developer productivity and run-time performance. It follows a 'no-magic' approach that stays true to Julia's idioms:

    • Controllers: Implemented as plain Julia modules.
    • Models: Leverage Julia's type system and multiple dispatch.
    • App Management: Genie apps are standard Julia projects, using Julia's Pkg for dependency management.
    • Development Workflow: Automatically supports code loading and reloading via Revise.

    The framework is designed to scale from simple scripts or REPL-based prototypes to complex, structured MVC (Model-View-Controller) applications. For database persistence, Genie provides an ORM called SearchLight.

  2. Overview of the Genie Framework ecosystem

    main

    The Genie Framework is a suite of tools for web development in Julia:

    • Genie.jl: The core server backend (routing, templating, authentication).
    • Stipple.jl: For building reactive UIs using a low-code Julia API.
    • Genie Builder: A VSCode plugin for visual drag-and-drop UI building.
    • SearchLight.jl: A complete ORM for database integration (Postgres, MySQL, SQLite, etc.).
  3. Manage user sessions with the Sessions module

    main
    The Sessions module in Genie.jl provides a way to manage persistent user state across multiple HTTP requests. You can interact with sessions using the session function, which provides access to a Session object. This object allows you to store, retrieve, and delete data associated with a specific user session.
  4. Use the Loader module to manage application lifecycle and resources

    main
    The Loader module provides a suite of functions to manage the loading and initialization of various components within a Genie application. It handles the bootstrapping of the environment, loading of libraries, plugins, routes, and resources, as well as the initialization of helpers and initializers. This module is central to the application startup process and resource management.
  5. Use type constraints in route parameters

    main

    You can enforce types on route parameters by appending ::Type to the parameter name in the route definition (e.g., :x::Int).

    Note: By default, Genie extracts parameters as SubString{String}. To use type constraints, you must ensure Julia knows how to convert a string to that type. For Int, you may need to extend Base.convert:

    Base.convert(::Type{Int}, s::AbstractString) = parse(Int, s)

    Once configured, Genie will automatically attempt to convert the URL segment to the specified type before passing it to the handler.

    # 1. Setup conversion for Int
    Base.convert(::Type{Int}, s::AbstractString) = parse(Int, s)
    
    # 2. Define route with type constraints
    route("/sum/:x::Int/:y::Int") do
        params(:x) + params(:y)
    end
  6. Understand the Genie MVC and Resource architecture

    main

    For larger projects, Genie uses a Model-View-Controller (MVC) architecture organized around "resources".

    • Resource: Represents a business entity (e.g., user, product, books).
    • Location: Resources are stored in app/resources/<resource_name>/.
    • Components: A resource typically includes a Controller (e.g., BooksController.jl), a Model (e.g., Books.jl), a Model Validator (e.g., BooksValidator.jl), and a views/ folder.

    Note: The app/ folder is automatically created the first time you add a resource using Genie's generators.

  7. How WebSockets and channels work in Genie

    main

    Genie uses an abstraction called channels to manage client-server communication over WebSockets. Conceptually, channels are the WebSocket equivalent of routes. Instead of HTTP requests, the client and server exchange messages over these channels. Genie's Router handles incoming WebSocket messages by extracting the payload and invoking a designated handler (either a function or a controller method), mirroring the standard MVC workflow used in HTTP routing.

    using Genie.Router
    
    # Define a channel with a block handler
    channel("/foo/bar") do
      # process request
    end
    
    # Define a channel with a controller method handler
    channel("/baz/bax", YourController.your_handler)
  8. Understand the scope and access of initializers

    main

    All definitions (variables, constants, functions, modules, etc.) added within initializer files are loaded directly into your application's module.

    If your application is named MyGenieApp, you can access these definitions via MyGenieApp.variable_name. Because the application name can vary, you can also access these definitions using the Main.UserApp constant, which points to your application's module.

  9. How Controllers, Routes, and Views work together

    main

    In a standard Genie workflow:

    1. A Route maps a URL to a specific method in a Controller.
    2. The Controller method (the route handler) processes logic and calls a View to render the response.
    3. The View (HTML, Markdown, or JSON) generates the final content sent to the client.

    To expose a controller method, add a route in routes.jl:

    using MyGenieApp.BooksController
    route("/endpoint", BooksController.method_name)
  10. Manage HTTP response headers in Genie.jl

    main
    The Genie.Headers module provides functions to manipulate HTTP response headers. You can use these functions to set custom headers, manage CORS (Cross-Origin Resource Sharing) policies, and normalize header keys. These operations are typically performed within a request handler to control how the client perceives and interacts with the server response.
  11. Access secrets via the application module namespace

    main

    Because definitions in config/secrets.jl are loaded into your app's module, they are available under your application's specific namespace.

    If your application module is named MyGenieApp, you can access a secret defined as API_KEY via MyGenieApp.API_KEY.

    Alternatively, you can use the Main.UserApp constant to access these definitions regardless of your specific application name.