/gomarvin

Generate boilerplate + endpoints for Go REST servers.

Primary LanguageGoMIT LicenseMIT

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

Generate:

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

from one config file

Install

  1. Either create a custom config file with frontend editor or copy one of the config file from examples dir

  2. Install gomarvin

# go version >= 1.17
go install github.com/tompston/gomarvin@latest

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

# or clone the repo and run go run main.go
git clone https://github.com/tompston/gomarvin.git
  1. run gomarvin
# run this in the same dir as the config file, if the name of the config is "gomarvin.json"
gomarvin

# run this if custom config file name or path
gomarvin -config="PATH_TO_CONFIG"

# generate only the typescript API client file
gomarvin -fetch-only="true"
  1. run lower commands
cd GENERATED_SERVER
go mod tidy
go mod download
go run main.go

CLI

gomarvin -h

-config string
      Specify path to the gomarvin config file (default "gomarvin.json")
-dangerous-regen string
      Regenerate everything. If set to true, init server will be regenerated and  all previous changes will be lost (default "false")
-fetch-only string
      generate only the typescript file that holds fetch function (default "false")

Generated Typescript fetch functions usage example

// import the generated file
import * as F from "../../../gomarvin.gen";
// or import only the Comment module endpoints
import { CommentEndpoints } from "../../../gomarvin.gen";

// fetch a user by id
async function FetchGetUserByIdEndpoint() {
  let res = await F.GetUserById(1);
  let users = await res.json();
  console.log(users);
}

// create a new user
async function FetchCreateUserEndpoint() {
  let res = await F.CreateUser({
    username: "qweqwe",
    email: "qwe@qwe.com",
    age: 20,
    password: "very-long-and-good-password",
  });

  let user = await res.json();
  console.log(user);
}

// append optional string to the existing endpoint url
async function FetchEndpointWithAppendedUrl() {
  const res = await F.GetUserById(10, { append_url: "?name=jim" });
  console.log(res);
}

// define custom options for the fetch request
async function FetchEndpointWithCustomOptions() {
  const res = await F.GetUserById(10, { options: { method: "POST" } });
  console.log(res);
}

// Use both optional values
// - append a string to the fetch url
// - define a new options object used in the fetch request
async function FetchWithAppendedUrlAndCustomOptions() {
  const res = await F.GetUserById(10, {
    options: { method: "DELETE" },
    append_url: "?name=jim",
  });
  console.log(res);
}

// Fetch a single endpoint from the Comment module
async function FetchCommentById() {
  const res = await CommentEndpoints.GetComment(20);
  console.log(res);
}

Notes

If formatting does not work, run this

gofmt -s -w .

Credits to used packages