Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/majidsajadi/sariaf
Fast, simple, and lightweight HTTP router for Golang
https://github.com/majidsajadi/sariaf
golang handler http router
Last synced: about 2 months ago
JSON representation
Fast, simple, and lightweight HTTP router for Golang
- Host: GitHub
- URL: https://github.com/majidsajadi/sariaf
- Owner: majidsajadi
- License: mit
- Created: 2020-11-29T15:26:50.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-12-17T05:02:54.000Z (about 4 years ago)
- Last Synced: 2024-06-18T21:49:18.862Z (7 months ago)
- Topics: golang, handler, http, router
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 34
- Watchers: 3
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Sariaf
Fast, simple and lightweight HTTP router for golang
## Install
`go get -u github.com/majidsajadi/sariaf`
## Features
- **Lightweight**
- **compatible with net/http**
- **No external dependencies**
- **Panic handler**
- **Custom not found handler**
- **No external dependencies**
- **Middleware support**
- **URL parameters**## Usage
```go
package mainimport (
"net/http""github.com/majidsajadi/sariaf"
)func main() {
r := sariaf.New()r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("welcome"))
})http.ListenAndServe(":3000", r)
}
```## Advanced Usage
```go
package mainimport (
"fmt"
"log"
"net/http""github.com/majidsajadi/sariaf"
)func main() {
router := sariaf.New()router.SetNotFound(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "Not Found")
})router.SetPanicHandler(func(w http.ResponseWriter, r *http.Request, err interface{}) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Println("error:", err)
fmt.Fprint(w, "Internal Server Error")
})router.GET("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello World"))
})router.GET("/posts", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("GET: Get All Posts"))
})router.GET("/posts/:id", func(w http.ResponseWriter, r *http.Request) {
params, _ := sariaf.GetParams(r)
w.Write([]byte("GET: Get Post With ID:" + params["id"]))
})router.POST("/posts", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("POST: Create New Post"))
})router.PATCH("/posts/:id", func(w http.ResponseWriter, r *http.Request) {
params, _ := sariaf.GetParams(r)
w.Write([]byte("PATCH: Update Post With ID:" + params["id"]))
})router.PUT("/posts/:id", func(w http.ResponseWriter, r *http.Request) {
params, _ := sariaf.GetParams(r)
w.Write([]byte("PUT: Update Post With ID:" + params["id"]))
})router.DELETE("/posts/:id", func(w http.ResponseWriter, r *http.Request) {
params, _ := sariaf.GetParams(r)
w.Write([]byte("DELETE: Delete Post With ID:" + params["id"]))
})router.GET("/error", func(w http.ResponseWriter, r *http.Request) {
panic("Some Error Message")
})log.Fatal(http.ListenAndServe(":8181", router))
}
```