web

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 29 Imported by: 0

README

Web App Handler for Go

[!WARNING] The web package is still in development. Use it at your own risk.

A golang http.Handler for serving no-build web applications with esm.sh CDN.

  • Web applications are served as-is without build step.
  • Transpiles TypeScript, JSX, Vue, Svelte on-the-fly.
  • Built-in TailwindCSS generator.
  • Static files are served from the application directory.
  • Support Hot Module Replacement (HMR) for development.

Installation

go get -u github.com/esm-dev/esm.sh

Usage

Create a web server in golang that serves web application from a directory:

package main

import (
  "net/http"
  "log"

  "github.com/esm-dev/esm.sh/web"
)

func main() {
  http.Handle("GET /", web.NewHandler(web.Config{
    AppDir: "/path/to/webapp",
    Fallback: "/index.html", // fallback to root index.html (SPA mode)
    Dev: false, // change to `true` to enable HMR
  }))
  log.Fatal(http.ListenAndServe(":8080", nil))
}

Create a index.html file in the webapp directory:

<!DOCTYPE html>
<html>
<head>
  <title>My Application</title>
  <link rel="stylesheet" href="/tailwind.css">
  <script type="importmap">
    {
      "imports": {
        "react": "https://esm.sh/react@19.2.4",
        "react/jsx-runtime": "https://esm.sh/react@19.2.4/jsx-runtime",
        "react-dom": "https://esm.sh/react-dom@19.2.4",
        "react-dom/client": "https://esm.sh/react-dom@19.2.4/client"
      }
    }
  </script>
</head>
<body class="flex justify-center items-center h-screen">
  <div id="app"></div>
  <script type="module" src="/app.tsx"></script>
</body>
</html>

Create a app.tsx file in the webapp directory:

import { createRoot } from "react-dom/client"

function App() {
  return <h1>Hello, World!</h1>
}

createRoot(document.getElementById("app")).render(<App />)

Create a tailwind.css file in the webapp directory:

@import "tailwindcss";

Run the web server:

go run .

Open the web browser and navigate to http://localhost:8080.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	AppDir   string
	Fallback string
	Dev      bool
}

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

func NewHandler

func NewHandler(config Config) *Handler

func (*Handler) ServeCSSModule

func (s *Handler) ServeCSSModule(w http.ResponseWriter, r *http.Request, query url.Values)

func (*Handler) ServeFrameworkCSS

func (s *Handler) ServeFrameworkCSS(w http.ResponseWriter, r *http.Request, query url.Values, framework string)

func (*Handler) ServeHTTP

func (s *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Handler) ServeHmrWS

func (s *Handler) ServeHmrWS(w http.ResponseWriter, r *http.Request)

func (*Handler) ServeHtml

func (s *Handler) ServeHtml(w http.ResponseWriter, r *http.Request, filename string)

func (*Handler) ServeInternalJS

func (s *Handler) ServeInternalJS(w http.ResponseWriter, r *http.Request, name string)

func (*Handler) ServeModule

func (s *Handler) ServeModule(w http.ResponseWriter, r *http.Request, filename string, query url.Values, preTransformContent []byte)

type JSWorker

type JSWorker struct {
	// contains filtered or unexported fields
}

func (*JSWorker) Call

func (jsw *JSWorker) Call(args ...any) (format string, output string, err error)

func (*JSWorker) Start

func (jsw *JSWorker) Start() (err error)

func (*JSWorker) Stop

func (jsw *JSWorker) Stop() (err error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL