api

package
v0.0.0-...-52405dd Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2021 License: MIT Imports: 0 Imported by: 0

README

API Gen

One of the main things developers use Go for is writting microservice and web apis. This is a simple domain driven api example using the standard http libs.

Take a look at the airplanes domain package and you'll see the service definitions in the airplanes.go file. We then have airplanes/http.go containing controllers, service.go contains buisness logic and mem_store.go is a really basic in memory store that returns a list of airplanes and can add new airplanes.

When developing APIs we often need to write the same bootstrapped code to get a new endpoint or service up and running. This is tedious.

Under cmd/ there are two packages api and gen open api and run go run main.go and you'll have the web service running on port :8080. You can hit http://localhost:8080/api/v1/airplanes and get a response.

The second package gen contains a generator. This will generate a new domain, taking away the boring task of bootstrapping. You will then have a full new endpoint you can hit. The idea of this in the real world is you could then go and add in whatever business logic is required a lot faster than manually creating the same boring files and structure over and over. It also enforces consistency. You can ensure everything is commented in your template files, that you are using best practises and everything follows common guidelines that you can determine.

To run the generator cd into the cmd/gen package and run go run main.go -domain=<whatever>. You can then hit /api/v1/s and get a basic response back, awesome!

The generator uses AST in go to firstly update the routes.go file with a new constant. It then updates the code in api/main.go to wire up your new endpoint and add the require import. Pretty cool!

Feel free to use this code for your own generators!

Documentation

Index

Constants

View Source
const (
	RouteAirplane  = "/api/v1/airplanes"
	RouteAirplanes = "/api/v1/airplanes"
)

Variables

This section is empty.

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
cmd
api
gen

Jump to

Keyboard shortcuts

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