Configuration Properties with Procyon

Burak Köken
3 min readJan 4, 2021

--

If you haven’t read my previous article on Handler Interceptors in Procyon, do check it out.

Today, We’re gonna talk about how to set up and use properties in Procyon. We’ll also see how properties work in Procyon.

Procyon makes it easier to configure properties for your application. By default, it converts any command-line option arguments (arguments starting with --, such as --server.port=3000) to property.

You can access command-line properties using ConfigurationProperties interface or Environment.

Injecting Properties Using ConfigurationProperties interface

Injecting a property with the tags is straightforward. If your properties have the same prefix, all you have to do is to implement ConfigurationProperties interface and return a prefix as shown below:

properties.go

The properties are going to be bind to this configuration object.

We can also specify a default value for the property.

Using default tag allows you to set a default value if the requested one, for any reason, isn’t available. For example, if the message.welcome value isn’t present, the value will be set as Hi.

Let’s see how we can use these properties in a simple REST API.

controller.go

Then, in the init function, register the functions which return the instances of your controller and properties so that your instances will be managed by Procyon.

main.go

Accessing Properties Using Environment

This is pretty straightforward. We can also obtain the value of a property using the Environment as shown below:

controller.go

Then, in the init function, register the function which returns an instance of your controller.

main.go

Let’s start our application.

Finally, we have a couple of REST endpoints that simply return these values, and pass in the message.welcome and message.bye as command-line arguments like:

--message.welcome="Hi, I'm a developer" --message.bye="catch ya later"

Lets’s check both of our REST endpoints:

As we can see, message.welcome and message.bye came from command-line arguments.

Hopefully, you got a better understanding of Configuration Properties with Procyon.

HAPPY CODING…

You can find the code on Github. And if you would like to contribute to the library or you find a bug, please report it.

--

--

No responses yet