Northstar Web Application Stack

repository·main·Indexed 42 days ago

https://github.com/zangster300/northstar

A web application stack utilizing Go, NATS, Datastar, and Templ. Northstar demonstrates modern web development patterns, including embedded NATS servers, Web Components, and Lit-based components. It features a Go-based server with support for live reloading via Air and esbuild, and a custom downloader utility for managing external web resources.

Tokens
2K
Snippets
14
Records
17
Agent score
55%

What's inside Northstar

  1. Configure web libraries and components

    main

    The web/libs/ directory serves as the entrypoint for custom JavaScript and TypeScript libraries. Currently, the project utilizes:

    • Web Components: Custom elements located in ./libs/web-components/.
    • Lit Components: Lit-based web components located in ./libs/lit/src/components/.
  2. Manage static asset serving modes via build tags

    main

    The project uses Go build tags to switch between serving static assets from the local filesystem (development) or from within the compiled binary (production).

    Development Mode

    Use the -tags=dev build tag. This utilizes static_dev.go to provide an HTTP handler that serves assets directly from the filesystem without embedding them. This allows for faster iteration without re-compiling the binary for every asset change.

    Production Mode

    Use the -tags=prod build tag (or omit build tags entirely). This utilizes static_prod.go to embed the static assets directly into the binary using hashfs. This ensures the binary is self-contained and ready for deployment.

    go build -tags=dev .
    # or
    go build -tags=prod .
  3. Run NORTHSTAR with live reload

    main

    The project uses Air and esbuild for live reloading. Use the live task from the Taskfile to start the development server with live reload enabled. Once started, the application is accessible at http://localhost:8080.

    go tool task live
  4. Maintain pathing consistency for web assets

    main

    When updating any directory paths within the web/ directory, you must manually synchronize the changes in two locations to prevent build failures:

    1. esbuild entrypoints: Update the entrypoint paths in cmd/web/build/main.go.
    2. Taskfile: Update the pathing definitions in the root Taskfile.yml.
  5. Set up the Web Components development environment

    main

    To set up the development environment for the Web Components library, you must first ensure Go dependencies are synchronized and then run the build script located in the cmd/web/build/main.go path. This process prepares the environment for building custom elements driven by Datastar.

    go mod tidy
    go run cmd/web/build/main.go
  6. Deploy NORTHSTAR using Docker

    main

    You can containerize the application using the provided Dockerfile. Build the image and run it by mapping port 8080 on your host to port 9001 in the container.

    # build an image
    docker build -t northstar:latest .
    
    # run the image in a container
    docker run --name northstar -p 8080:9001 northstar:latest
  7. Set up the Lit library example

    main

    To set up the Lit library example, which is used for creating web components powered by Lit and driven by Datastar, follow these steps:

    1. Install the necessary dependencies using pnpm.
    2. Run the build command using the Go build tool to prepare the environment.
    pnpm install
    go run cmd/web/build/main.go
  8. Start the Northstar web server

    main

    The Northstar web server is the primary entrypoint for the application. It initializes a JSON-based logger, sets up a cookie-based session store, establishes a NATS connection, and configures the HTTP router. The server is designed to handle graceful shutdowns via OS interrupt signals (e.g., Ctrl+C), allowing up to 5 seconds for active connections to close before forcing a shutdown.

    # To run the server, use the standard Go run command from the project root
    go run cmd/web/main.go