routegen

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2022 License: MIT Imports: 0 Imported by: 0

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

  • 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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(interface{}) string

Types

This section is empty.

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL