Stringdemo is a program that demonstrates the service features of WeGO. It has been discussed previously here
In this page, we will see how to generate a stringdemo completely using WeGO-Gen.
Steps
First download and set up wego-gen
Comments are given below each instruction preceded by #
Next create the interface file
We need to create a GO file that contains the interface for the new service that we are going to write.
cd stringdemo
Next use your favorite editor to write the GO code below. The # preceded lines annotate the code and should not be literally typed.
Interface, Methods and Parameters
Let us look at the code line by line: The chief objective of the code is to define the interface called StringdemoService. This interface defines three methods which have the following properties:
All of the methods accept a context as the first param.
The second param for the first two methods is accepted as a pointer. They are of a non primitive GO type defined in the same file. The parameter is expected to be passed as a payload in the HTTP request. There cannot be more than one parameter of a custom type in the signature of the method
The third method accepts only primitive types - hence there are two of them. These are expected to be passed as headers. (or query params or path params) in the HTTP request.
All of the methods are returning two values - one a response object which will be serialized using an encoding mechanism (by default JSON) and an error object.
The Request params and response params are defined in the same file.
Comments in the file
The interface file is expected to have comments which will eventually make it into the Swagger description. The following comments are currently supported in WeGO-Gen.
Interface type comments. These get put into the service description.
The comments for each of the request and response parameters. - these become the parameter documentation
Method comments - these become the operation documentation.
Preparation for Generating the service
First checkout the contents of config/setenv.sh. It should look like the following:
Tweak the file if required. The comments are self explanatory
Generating the stringdemo Service
What Got Generated?
WeGO-Gen-gen.sh creates two WeGO modules - one for API and one for the service. It confirms the URLs for both the modules. The module structure of these modules is discussed in this article. WeGO-Gen parses the GO file and creates a service that returns an empty response. The service file is created in stringdemoservice. The API module is copied to the api package under stringdemoapi module.
Makefile, configs, error handling structures, environment files etc. are automatically generated. Test case, main file, swagger generation utility etc. are also created. In short, the entire boiler plate required for proper plumbing is done.