Friday, April 12, 2024
HomeGolangGenerate boilerplate for Gin / Fiber / Echo / Chi Golang REST...

Generate boilerplate for Gin / Fiber / Echo / Chi Golang REST servers


Generate Golang Relaxation Server:

  • Init server + controllers for endpoints
  • Typescript fetch features with type-checked payloads for generated endpoints
  • SQL information with tables and queries for controllers ( Postgres )

from one config file.

Set up

  1. Both create a customized config file with the frontend editor or copy one of many config file from examples dir
  2. Set up gomarvin
# go model >= 1.17
go set up github.com/tompston/[email protected]

# go model < 1.17
go get github.com/tompston/gomarvin

# or clone the repo and run go run most important.go
git clone https://github.com/tompston/gomarvin.git
  1. run gomarvin
# run this in the identical dir because the config file, if the identify of the config is "gomarvin.json"
gomarvin generate

# run this if customized config file identify or path
gomarvin -config="PATH_TO_CONFIG" generate

# or generate solely the typescript API shopper file. Helpful if you wish to generate fetch
# features for a pre-existing REST API in a quick method.
gomarvin -fetch-only="true" generate
  1. run decrease instructions
cd GENERATED_SERVER
go mod tidy
go mod obtain
go run most important.go

CLI

Flags:
  -config		Specify path to the gomarvin config file (default "gomarvin.json")
  -dangerous-regen	Regenerate every thing. If set to true, init server might be regenerated and all earlier modifications might be misplaced (default "false")
  -fetch-only		generate solely the typescript file that holds fetch perform (default "false")`

Generated Typescript fetch features utilization instance

// import the generated file
import * as F from "../../../chi_with_modules/public/gomarvin.gen" 
// or simply import a single fetch perform
import { GetUserById } from "../../../chi_with_modules/public/gomarvin.gen"

// both use the default shopper created from
// the settings of the config file, or create a brand new one
// (helpful when switching environments)
const defaultClient = F.defaultClient

// api shopper when deployed
const productionClient: F.Consumer = {
  host_url: "http://instance.com",
  api_prefix: "/api/v1",
  headers: {
    "Content material-type": "utility/json;charset=UTF-8",
  },
}

const DEV_MODE = true

// swap to productionClient if DEV_MODE is fake
const shopper = DEV_MODE ? defaultClient : productionClient

// fetch GetUserById endpoint
async perform FetchGetUsersById() {
  const res = await F.GetUserById(shopper, 10);
  console.log(res);
}

// append non-compulsory string to the prevailing endpoint url
async perform FetchEndpointWithAppendedUrl() {
  const res = await F.GetUserById(shopper, 10, { append_url: "?identify=jim" });
  console.log(res);
}

// outline customized choices for the fetch request
async perform FetchEndpointWithCustomOptions() {
  const res = await F.GetUserById(shopper, 10, { choices: { technique: "POST" } });
  console.log(res);
}

// Use each non-compulsory values
// - append a string to the fetch url
// - outline a brand new choices object used within the fetch request
async perform FetchWithAppendedUrlAndCustomOptions() {
  const res = await F.GetUserById(shopper, 10, {
    choices: { technique: "DELETE" },
    append_url: "?identify=jim",
  });
  console.log(res);
}

Notes

  • Seeding the db
    • Because the generated fetch features are mapped to the endpoints, you should use them to seed the database fairly simply. Utilizing deno with the faker bundle could make the method trivial.
  • If formatting doesn’t work for some purpose, run thisgofmt -s -w .
  • Utilizing the device to generate fetch features for pre-existing REST APIs
    • If you wish to generate a shopper for an already current REST API in a quick method, you should use the editor to doc the wanted values to codegen the fetch features in a quick method

    # run this after exporting the config file, assuming that # the gomarvin.json file is in the identical dir gomarvin -fetch-only=”true” generate

  • If there are errors after creating a brand new config file, submit a difficulty. There is perhaps some edge instances that haven’t been examined but.
  • Gin and router bugs
    • bugs attributable to url params and routing needs to be mounted manually. Because of the method during which the routers and controllers are generated, the generated Gin servers gained’t have the ability to run initially.

Credit to used packages

Not put in regionally to keep away from any dependencies.

Be aware on versioning

Variations (tags) have a sample of x.y.z

  • x is incremented on huge releases.
  • y is incremented when there’s a change that breaks the beforehand generated servers.
  • z is incremented when there’s a change that doesn’t break the beforehand generated servers.

For instance, updating from 0.1.0 to 0.1.1 doesn’t break something. Going from 0.2.0 to 0.3.0 will break stuff.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments