https://github.com/floscodes/go-routex
Easy to use router for http-services
https://github.com/floscodes/go-routex
Last synced: 4 months ago
JSON representation
Easy to use router for http-services
- Host: GitHub
- URL: https://github.com/floscodes/go-routex
- Owner: floscodes
- License: mit
- Created: 2022-09-23T19:42:11.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-01-01T11:59:11.000Z (over 3 years ago)
- Last Synced: 2025-12-18T19:29:08.709Z (6 months ago)
- Language: Go
- Homepage:
- Size: 39.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# routex is an easy to use router for http-services
### Set routes and link them to functions
```go
func main() {
router := routex.New()
router.Handle("/hello", hello)
fmt.Println("Server is listening")
http.ListenAndServe(":8080", router)
}
func hello(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello!"))
}
```
You can specify the allowed methods like this:
```go
router.Handle("/hello", hello).Methods("POST", "GET")
```
Or set them for all configured paths:
```go
router.Methods("POST", "GET")
```
The router will accept trailing slashes typed by the client by default, even if none is set in the handling path.
You can disable that this way for a certain route...
```go
router.Handle("/hello", hello).AcceptTrailingSlash(false)
```
... or disable it for all routes:
```go
router.AcceptTrailingSlash(false)
```
### Serve static files
```go
router.ServeStatic("/static", "./static")
```
If the request does not contain a specific filename, the router will automatically look for `index.html`.
Optionally you can set a custom index file:
```go
router.ServeStatic("/static", "./static").IndexFile("template.html")
```
### Allow CORS
You can just allow CORS like this:
```go
router.AllowCORS(true)
```
or for a single route.
```go
router.Handle("/api", api).AllowCORS(true)
```
**Whenever allowing CORS it is highly recommended to define the allowed methods, otherwise all methods will be accepted!**
However, custom CORS heraders could be set by yourself at anytime in your handler functions.
```go
router.Handle("/api", api).AllowCORS(true).Methods("GET")
```