genapi

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2020 License: MIT Imports: 6 Imported by: 0

README

kahlys/genapi

PkgGoDev build go report

Quick golang code generation for rest api microservices using gorilla mux.

Installation

With a correctly configured Go toolchain:

go get -u github.com/kahlys/genapi

Usage

Write Rest API description in a configuration file.

ServiceName: "booker"
Endpoints:
  - Name: "GetBook"
    URL: "/api/book/{id}"
    Method: "GET"
  - Name: "SetBook"
    URL: "/api/book"
    Method: "POST"

Run to generate your files by giving the path to the configuration file, and the output directory path.

genapi -config ./example/config.yml - dir mydir

Generated files in this example are

// file service.go
package main

// Booker ...
type Booker struct{}

// GetBook ...
func (b *Booker) GetBook() {
    panic("not implemented")
}

// SetBook ...
func (b *Booker) SetBook() {
    panic("not implemented")
}
// file handler.go
package main

import (
    "net/http"

    "github.com/gorilla/mux"
)

// handleGetBook ...
func (b *Booker) handleGetBook(w http.ResponseWriter, req *http.Request) {
    b.GetBook()
}

// handleSetBook ...
func (b *Booker) handleSetBook(w http.ResponseWriter, req *http.Request) {
    b.SetBook()
}

// Handler returns the Booker HTTP Handler.
func (b *Booker) Handler() http.Handler {
    r := mux.NewRouter()
    r.HandleFunc("/api/book/{id}", b.handleGetBook).Methods("GET")
    r.HandleFunc("/api/book", b.handleSetBook).Methods("POST")
    return r
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Endpoint

type Endpoint struct {
	Name   string
	URL    string
	Method string
}

Endpoint http rest api

type Function

type Function struct {
	Comments string
	Name     string
	Params   []Parameter
	Res      []Parameter
	Content  []string
}

Function represents a function signature.

type Method

type Method struct {
	Recv string
	Function
}

Method represents a method signature.

type Parameter

type Parameter struct {
	Name string
	Type string
}

Parameter represents a parameter in a function or method signature.

type RestAPI

type RestAPI struct {
	ServiceName string
	Endpoints   []Endpoint
}

RestAPI description

func (RestAPI) Generate

func (d RestAPI) Generate(dir string) error

Generate ...

type Structure

type Structure struct {
	Comments string
	Name     string
	Fields   []Parameter
}

Structure represents a type

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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