Skip to content

xehrad/goge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Goge Logo

goge

goge is a code generator for the Fiber framework. It automatically creates API handlers and OpenAPI specs from annotated Go functions.

  • Annotate your Fiber handler with a comment:

    type PingParams struct {
        ID    string `gogeUrl:"id"`              // read from the URL path
        Auth  string `gogeHeader:"Authorization"`// read from HTTP header
        Query string `gogeQuery:"filter"`        // read from the query String
        Page  int    `gogeQuery:"page,default=1"`// read from the query Int
        Name  string `json:"name"`               // from POST/PATCH/PUT body
    }
    
    //goge:api method=POST path=/ping/:id
    func Ping(c *fiber.Ctx, params *PingParams) (*PingResp, error) {
        return &PingResp{ID: params.ID}, nil
    }
  • After running go generate ./... goge generates a handler and router automatically:

    // Code generated by goge; DO NOT EDIT.
    
    func PingHandler(c *fiber.Ctx) error {
      req := new(PingParams)
      c.BodyParser(req)
    
      req.ID = c.Params("id")
      req.Auth = c.Get("Authorization")
      req.Query = c.Query("filter")
      req.Page = c.QueryInt("page", 1)
    
      res, _ := Ping(c, req)
      return c.JSON(res)
    }
    
    
    func GogeRouter(app *fiber.App) {
      app.Add("GET",  "/swagger", SwaggerUIHandler) // Swagger endpoints
      app.Add("POST", "/ping/:id", PingHandler)     // API endpoints
    }
  • Inside your project, add the following to your main.go

    //go:generate goge
    
    package main
    
    func main() {
      app := fiber.New()
      lib.GogeRouter(app)
      log.Fatal(app.Listen(":8080"))
    }

Installation

  1. Make sure you have Go 1.24+ installed.

  2. This will place the goge binary in your GOPATH/bin (by default ~/go/bin)

    go install github.com/xehrad/goge@latest
  3. Add $GOPATH/bin to your PATH. If not already configured, add this to your shell config (~/.bashrc or ~/.zshrc):

    export PATH=$PATH:$(go env GOPATH)/bin
  4. Reload your shell:

    source ~/.bashrc
    # or
    source ~/.zshrc
  5. Now you should be able to run goge

    • A ready-to-use handler function at lib/api_generated.go
    • OpenAPI documentation wit params at lib/openapi.json
    goge [package] [src]
    
    # package:  is name of package, default is "lib"
    # src:      is path of src, default is "./lib"

Name

goge — pronounced like "Go Dje" — comes from Go + Generate, reflecting its purpose of generating Go code automatically.

In Persian, goge also means tomato 🍅.

About

goge creates API handlers and OpenAPI specs from annotated Go functions.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published