Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/serkodev/routegen
File-system based route generator for Go. Compatible with any web frameworks.
https://github.com/serkodev/routegen
echo file-system-based generator gin go go-generate middleware router
Last synced: 2 months ago
JSON representation
File-system based route generator for Go. Compatible with any web frameworks.
- Host: GitHub
- URL: https://github.com/serkodev/routegen
- Owner: serkodev
- License: mit
- Created: 2021-12-17T12:03:47.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-07-25T11:08:12.000Z (over 2 years ago)
- Last Synced: 2024-10-31T11:51:31.760Z (2 months ago)
- Topics: echo, file-system-based, generator, gin, go, go-generate, middleware, router
- Language: Go
- Homepage:
- Size: 110 KB
- Stars: 20
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# routegen
File-system based route generator for Go. Compatible with any web frameworks.
> **Note**
> This project is in beta, it may contain bugs and have not being tested at all. Use under your own risk, but feel free to test, make pull request and improve this project.## Features
- [x] Generate routes from file-system
- [x] All web frameworks compatible ([customizable](./internal/routegen/engineconfig), default supports [Gin](https://github.com/gin-gonic/gin) & [echo](https://github.com/labstack/echo))
- [x] Support middleware
- [x] Support route with wildcard `/foo/*`
- [x] Support route with named parameter `/foo/:id`
- [x] Support route with alias## Install
```
go install github.com/serkodev/routegen/cmd/routegen@latest
```## Guides
- [Tutorial](#how-it-works)
- [Work with all web frameworks](./internal/routegen/engineconfig)
- [Wildcard & named parameter](./internal/routegen/testdata/wildcard)
- [Middleware](./internal/routegen/testdata/middleware)
- [Sub-route](./internal/routegen/testdata/subroute): Create routes with public type
- [Route alias](./internal/routegen/testdata/alias): Customize sub-route name## How it works?
`routegen` will scan your go project folders and generate routes when detects special function name (`GET`, `POST`, etc) in your package. It will use the relative file path as the route path, you may also modify the route name by `alias` and use wildcard or named parameter. The method of code injection refers to [wire](https://github.com/google/wire).
Here we will use [Gin](https://github.com/gin-gonic/gin) as a test example. Create the folder strcuture as below
```
π
|-πfoo
| |-handle.go
| |-πbar
| | |-handle.go
|-main.go
|-go.mod
```Create `./main.go`
```go
//go:build routegeninjectpackage main
import (
"github.com/gin-gonic/gin"
"github.com/serkodev/routegen"
)func Build(g *gin.Engine) {
// important! placeholder for routes output
routegen.Build(g)
}func main() {
g := gin.Default()
Build(g)
g.Run()
}
```Create `./foo/handle.go`
```go
package fooimport "github.com/gin-gonic/gin"
func GET(c *gin.Context) {}
```Create `./foo/bar/handle.go`
```go
package barimport "github.com/gin-gonic/gin"
func GET(c *gin.Context) {}
```Run generate command at your project root
```
routegen .
````main_gen.go` will be generated. π
```go
// Code generated by routegen. DO NOT EDIT.//go:generate go run -mod=mod github.com/serkodev/routegen/cmd/routegen
//go:build !routegeninject
// +build !routegeninjectpackage main
import (
"github.com/gin-gonic/gin"
routegen_r "example.com/helloworld/foo"
routegen_r2 "example.com/helloworld/foo/bar"
)func Build(g *gin.Engine) {
g.GET("/foo", routegen_r.GET)
g.GET("/foo/bar", routegen_r2.GET)
}func main() {
g := gin.Default()
Build(g)
g.Run()
}
```## LICENSE
MIT License