Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/ije/rex
- Owner: ije
- License: mit
- Created: 2019-02-15T14:34:24.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-11-01T14:25:16.000Z (17 days ago)
- Last Synced: 2024-11-01T15:25:36.013Z (17 days ago)
- Topics: go, golang, web
- Language: Go
- Homepage:
- Size: 395 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 mainimport (
"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"))
})
```