DEV Community

Cover image for Create a Restful API with Golang from scratch

Create a Restful API with Golang from scratch

Thiago Pacheco on November 21, 2021

In this tutorial, we are going to walk through an example of how to create a Restful API with Golang. We are going to cover the initial setup of th...
Collapse
 
mathmul profile image
Matej

Awesome tutorial.

I came to it because I got stuck in my own project. In my project I wrote a different swagger.yml file and then generated with swagger generate... command. This gave gen folder and 2 folders inside, namely models and restapi.

Now I don't know how use the files generated, so I found this great article. I didn't know of Fiber, nor GORM, and they're awesome, thank you! I did your entire tutorial directly inside my project and it works great.

I was hoping though, that once I have that up and running, I will be able to replace "Todo" references with my "Group" and "User" stuff, but with those handlers.go, repositories.go and models.go it's a completely different structure.

Any idea how to go forward, given this poor description?

Collapse
 
pacheco profile image
Thiago Pacheco • Edited

Hi Matej, thanks a lot for your feedback! I am glad you liked it.
Given your explanation above, I would actually suggest you to start by mapping your concepts into models like the todo model example. From there you create the repositories and handlers accordingly.
It is a bit hard to help without knowing much about your context, but I would gladly provide more help if you'd share more details about it. You can DM me on twitter if you want: @dev_pacheco.

Collapse
 
mathmul profile image
Matej

I really would take you up on the offer, but @dev_pacheco can't be messaged.

I did make a copy of "todo" folder (module) and renamed it to "group". Also renamed everything in the files from todo to group, Todo to Group, and TodoSomething to GroupSomething. This wouldn't immediately be OK, since Group was wrong, given my import "github.com/me/project/gen/models", so I had to replace Todo with models.Group, and most errors were gone. It also automatically put an asterisk (*) before group.Name in func (repository) Find (id) in the if condition. Then I suppose I can just delete the "models.go" file in "groups" module. So now I think all it's left is to fix Register func to have the same basePath as in REST API, ie.

func Register(router fiber.Router, database *gorm.DB) {
    database.AutoMigrate(&models.Group{})
    groupRepository := NewGroupRepository(database)
    groupHandler := NewGroupHandler(groupRepository)

    groupsRouter := router.Group("/groups")
    groupsRouter.Get("/", groupHandler.GetAll)
    groupsRouter.Post("/", groupHandler.Create)
    groupsRouter.Get("/:id", groupHandler.Get)
    groupsRouter.Put("/:id", groupHandler.Update)
    groupsRouter.Delete("/:id", groupHandler.Delete)
}
Enter fullscreen mode Exit fullscreen mode

And yes that actually worked! Thank you... I am curious though if I really need to set Router stuff here in the register manually like this, or can I get those methods set somehow from the REST API files generated from Swagger?

Thread Thread
 
mathmul profile image
Matej

Also how does one write tests to make sure the database is connected, or a new Todo can be added?

Thread Thread
 
pacheco profile image
Thiago Pacheco

Hey Matej, I am glad you could figure it out!
About the router, you can definitely setup differently, the Register func is just a suggestion. The idea about having this Register func is to have an unique way to initialize each of your modules.

Thread Thread
 
pacheco profile image
Thiago Pacheco

Definitely, it is a good practice to write tests.
I didn't include here because the article would get too long, but I could definitely create a part 2 of this article talking about tests, if that would be interesting.

Collapse
 
masdjab profile image
Heryudi Praja

I like your tutorial. It's nice, clear, easy to understand. Thanks.

Collapse
 
dryluigi profile image
Mario Prasetya M. • Edited

hey i really love your article. how you explain is kinda concise and clear :). but the dockerfile section attract my attention. if you wouldn't mind, may I ask you a question? how the container is supposed to run when you only define ENTRYPOINT without CMD or other command related to running the built go application?

Collapse
 
pacheco profile image
Thiago Pacheco

Hi @dryluigi, thanks for the feedback!
The command to run is defined in the ENTRYPOINT line.
In the dockerfile we can specify how to run the app through either of these 2 options: CMD or ENTRYPOINT.
The ENTRYPOINT one is prefered if you have an specific built entrypoint to run the app, which in our case we have the air to run on dev mode and the todo built file if we want to run a production version. This option is good for apps that generate a build file, like Golang or Java apps for example.
The CMD is used when you want to define a default command that can be overridden or extended by whoever wants to use this file. In Node.js applications for example you might have a: CMD ['npm', 'start'].

Collapse
 
ujjavala profile image
ujjavala

this is really good. i haven't tried fiber yet. will definitely do now :)

Collapse
 
pacheco profile image
Thiago Pacheco

Thank you @ujjavala ! Fiber is fun, I hope you also like it :)

Collapse
 
geekmaros_23 profile image
Abdulrasaq Mustapha

I recently started learning Go and even with my basic knowledge I was till able to follow the articel, well structured

Thanks @thiago

Collapse
 
pacheco profile image
Thiago Pacheco

I am so happy to know that, thanks for the feedback @geekmaros_23 !

Collapse
 
elserhumano profile image
Fernando

Nice article! I enjoyed a lot! Thx! :)

Collapse
 
pacheco profile image
Thiago Pacheco

Hey Fernando, thanks for your feedback!
I am glad you liked it :D

Collapse
 
Sloan, the sloth mascot
Comment deleted