[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

File-system based route generator for Go. Compatible with any web frameworks.

License

Notifications You must be signed in to change notification settings

serkodev/routegen

Repository files navigation

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

  • Generate routes from file-system
  • All web frameworks compatible (customizable, default supports Gin & echo)
  • Support middleware
  • Support route with wildcard /foo/*
  • Support route with named parameter /foo/:id
  • Support route with alias

Install

go install github.com/serkodev/routegen/cmd/routegen@latest

Guides

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.

Here we will use 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:build routegeninject

package 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

package foo

import "github.com/gin-gonic/gin"

func GET(c *gin.Context) {}

Create ./foo/bar/handle.go

package bar

import "github.com/gin-gonic/gin"

func GET(c *gin.Context) {}

Run generate command at your project root

routegen .

main_gen.go will be generated. 🎉

// Code generated by routegen. DO NOT EDIT.

//go:generate go run -mod=mod github.com/serkodev/routegen/cmd/routegen
//go:build !routegeninject
// +build !routegeninject

package 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

About

File-system based route generator for Go. Compatible with any web frameworks.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published