Building Rest API in Procyon- Framework
By: Burak Köken
Tags: Web, Golang, Go, Web Development, API

Procyon is a comprehensive web framework written in Go. If you need a comprehensive web framework, you will love to use it.

To install the Procyon package, you need to install Go and set your Go workspace first.

Download and install it:

$ go get -u github.com/procyon-projects/procyon

Ok, let’s start coding! Firstly in a new named main.go file, we import our libraries and create our Procyon application and make it run.

main.go

We will move towards dealing with the controllers. Before doing that, we declare the struct User into the model.go as you can see the following code snippet.

model.go

Secondly, we declare the interface UserController and the struct ImpUserController implementing it:

In the above code, note that we also declared a function named NewUserController creating an instance of the controller. What will we use this function for? Before explaining it, we need to implement the interface web.Controller to register our handler method as you can see in the following code.

Next, in a new named init.go file, we register our NewUserController by using core. Register.

init.go

Why do we need to register a function creating an instance of our controller? Because NewUserController is invoked to create an instance of our controller by the framework. Also, if your struct implements the interface web.Controller, the method RegisterHandler is called to allow you to register your handlers while your controller is created.

I assume you have done all steps. To have a clean architecture, We will declare our service and repository.

First, we declare the interface UserRepository and the struct ImpUserRepository implementing it. GetUsers returns dummy data for now.

In the above code, note that we also declared a function named NewUserRepository creating an instance of the repository.

Next, in the init.go file, we register our NewUserRepository by using core. Register.

Secondly, we declare the interface UserService and the struct ImpUserService implementing it as you can see in the following code snippet.

Note that we also declared a function named NewUserService creating an instance of the service. But this time the function takes an instance of the repository.

Then, we register the function NewUserService in the init function. While the application is started, the instance of your repository is automatically passed to NewUserService function as a parameter.

init.go

We have completed our service and repository implementations. Now it’s time to update the controller struct.

First, We need to get the instance of the User Service. To do that, we update our NewUserController function and ImpUserController struct as follows. The instance of User Service will be passed to the NewUserController function by the framework.

controller.go

Finally, we update our handler methods and return the response as you can see in the following code snippet. We use the SetBody function to set our response and invoke the Ok function to return 200 as HTTP code.

Also, we set the content type as JSON by using the SetContentType function as we are going to return a JSON.

controller.go

You can find the complete controller code in the following code snippet.

I assume you have done all steps, let us test it now:

If you run your application without giving any parameter port, it will start on port 8080. If you want to change the port on which the application starts, you can specify the parameter server.port.

When you specify the parameter port as 3030 (server.port=3030), your application will start on port 3030 as follows.

So, Finally worked on creating the First API by using Procyon.

Test yourself to verify in your environment.

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.