Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ije/rex

A lightweight, high-performance, and extensible web framework in Go.
https://github.com/ije/rex

go golang web

Last synced: 17 days ago
JSON representation

A lightweight, high-performance, and extensible web framework in Go.

Awesome Lists containing this project

README

        

# REX

[![GoDoc](https://godoc.org/github.com/ije/rex?status.svg)](https://godoc.org/github.com/ije/rex)
[![GoReport](https://goreportcard.com/badge/github.com/ije/rex)](https://goreportcard.com/report/github.com/ije/rex)
[![MIT](https://img.shields.io/badge/license-MIT-green)](./LICENSE)

**REX** is a lightweight, high-performance, and middleware-extensible web framework in Go. Used by [esm.sh](https://esm.sh) project.

## Installation

```bash
go get -u github.com/ije/rex
```

## Usage

```go
package main

import (
"log"
"github.com/ije/rex"
)

func main() {
// use middlewares
rex.Use(
rex.Logger(log.Default()),
rex.Cors(rex.CorsAll()),
rex.Compress(),
)

// match "GET /" route
rex.GET("/{$}", func(ctx *rex.Context) any {
return rex.Render(
rex.Tpl("

My Blog

    {{range .}}
  • {{.Title}}
  • {{end}}
"),
posts.List(),
)
})

// match "GET /posts/:id" route
rex.GET("/posts/{id}", func(ctx *rex.Context) any {
post, ok := posts.Get(ctx.PathValue("id"))
if !ok {
return rex.Err(404, "post not found")
}
return post
})

// match "POST /posts" route
rex.POST("/posts", func(ctx *rex.Context) any {
return posts.Add(ctx.FormValue("title"), ctx.FormValue("author"), ctx.FormValue("content"))
})

// match "DELETE /posts/:id" route
rex.DELETE("/posts/{id}", func(ctx *rex.Context) any {
ok := posts.Delete(ctx.PathValue("id"))
return ok
})

// Starts the server
<-rex.Start(80)

// Starts the server with TLS (powered by Let's Encrypt)
<-rex.StartWithAutoTLS(443)
}
```

More usages please check [examples/](./examples).

## Middleware

In **REX**, a middleware is a function that receives a `*rex.Context` and returns a `any`. If the returned value is not `nil`, the middleware will return the value to the client, or continue to execute the next middleware.

```go
rex.Use(func(ctx *rex.Context) any {
// return a html response
return rex.HTML("

hello world

")

// return nil to continue next handler
return nil
})
```

## Routing

**REX** uses [ServeMux Patterns](https://pkg.go.dev/net/http#hdr-Patterns) (requires Go 1.22+) to define routes.

Patterns can match the method, host and path of a request. Some examples:

- `/index.html` matches the path `/index.html` for any host and method.
- `GET /static/` matches a GET request whose path begins with `/static/`.
- `example.com/` matches any request to the host `example.com`.
- `example.com/{$}` matches requests with host `example.com` and path `/`.
- `/b/{bucket}/o/{objectname...}` matches paths whose first segment is `b` and whose third segment is `o`. The name `bucket` denotes the second segment and `objectname` denotes the remainder of the path.

In general, a pattern looks like:
```
[METHOD ][HOST]/[PATH]
```

You can access the path params via the `ctx.PathValue()`:

```go
rex.GET("/posts/{id}", func(ctx *rex.Context) any {
return fmt.Sprintf("ID is %s", ctx.PathValue("id"))
})
```