Skip to main content

Learning GO, Buffalo Web Framework

 

LEARNING BUFFALO

MY ADVENTURE IN GOLANG

Intro

After finishing a fun and simple way creating hello world in GO, I decided to extend the learning adventure into web framework. My choice is Buffalo, because some web recommended me to do so. I will try other framework as well but that is for later writing. My plan in this will be

1. Setting Environment. I think it’s already set

2. Install Buffalo. Based on https://gobuffalo.io/en/docs/getting-started/installation. Install scoop check, Install buffalo check.

3. Follow The Document. From here https://gobuffalo.io/en/docs/getting-started/new-project

Follow The Document

Based on https://gobuffalo.io/en/docs/getting-started/new-project I will begin my own adventure. I hope I found a new error.

So it says I have to create my new application on my GOPATH



I will not do that. I’m going to create in another folder. And I found some error



as usual I just copy paste the error message with keyword golang. And stackoverflow answer with



let’s go install the thing. And see if it resolve.



After restarting visual studio code, and the messages says successfully built. I guess it’s resolve.

Next to do is start the app. I did “$ buffalo dev” But again bump into error. Well the document said “Generate project” so generate is done. But it says the app need database if you look into error messages



the getting started didn’t said anything about database and database configuration. So I just go into the files and found out that Readme.md said something about the database.

I think this one at Readme.md should be mention in the Getting Started document https://gobuffalo.io/en/docs/getting-started/new-project and place it before running the app. After looking into the doc, it is said that I have to look into Database section before running the app.



I guess I need to read better. Well anyway this one is screencap from Readme.md



You’ll need a postgres as default database, and edit the configuration in database.yml file, don’t forget to comment out test and production.





After that you can go start the $ buffalo pop create -a command. It will automatically create the database.

Then you can run $ buffalo dev as Readme.md mention it. If it’s work fine, you can view it in browser

go to http://localhost:3000/ and if you see this, then it’s working.


Reading Database Section

After this I decided to read the Database section https://gobuffalo.io/en/docs/db/getting-started/ it’s a quite heavy reading, but if you are already familiar with other programming languages like Nodejs this should be easier to understand. If you just started coding and your choice is Go then you don’t have to read all of this in one go, just comeback when you need an information. For example, usually I read the first one and then I’ll try it without finishing the whole article. And after few days I’ll encounter relationship or migration or something else related to database but I don’t know how the correct Go approach, then I’ll read some more. I think that it’s important to try it at least once before continuing to read.

Reading Routing Section

Again if you already experienced in ExpressJS you’ll catch up quick, but it always nice to read it once. https://gobuffalo.io/en/docs/routing/

Reading Action Section

Umm yeah good read. https://gobuffalo.io/en/docs/actions/ have a good tools to generate the files and code at the same time.

Reading Resources Section

This one section is very interesting for me. It generate CRUD for a table in one go along with the needed view. If you look into the code, it show a standard CRUD operation.

But what if we got a relation with another table? Apparently if you read at https://gobuffalo.io/en/docs/resources/#nesting-resources section it covers the question.

Reading Context Section

I don’t understand this one. https://gobuffalo.io/en/docs/context/

Reading Request Binding

https://gobuffalo.io/en/docs/bind/. So model can bind into form or json/xml

Reading Middleware

Again it’s good read, and of course I will use already existing middleware rather than create my own.

Reading The rest of Request Handling Section

Session and cookies, I think this one still the same as other programming language.

Conclusion from Reading a few article

Are this reading can make you a website with GO ? Yes, a simple one. Without Authentication and Authorization.

So what to do if I want to make a website complete with Authentication and Authorization also User Management with Role based Access ? Of course you need to google more.

Finding a Complete Website

My complete website is a website that have User Management module with Role Based Access and a good Authentication and Authorization middleware.

So how are we gonna achieve this ? Like I said before we need to google more. And with google more I found this article https://blog.john-pfeiffer.com/golang-buffalo-tutorial-to-create-a-web-site-with-authentication. It explain to use minimal Authentication and Authorization. But I can’t seem to find Role based one.

So let’s start making one. For a little bit of design in RBAC you can read these article https://elang2.github.io/myblog/posts/2018-09-29-Role-Based-Access-Control-MicroServices.html

, but for a starter I think I just need to create a simple one. For the first try, I gonna do a simple User Auth then moving to User management, then add the simple Roles Based Access.

I found some tutorial in creating buffalo with JWT its here https://www.littlewire.dev/2020/03/buffalo-auth/

Creating a simple User Authentication and Authorization

I create this based on article from littlewire https://www.littlewire.dev/2020/03/buffalo-auth/ and if you also look into https://github.com/gobuffalo/authrecipe the code almost the same, anyway so let’s jump into it. Before this I already create the coke app using $ buffalo new coke, without the –api like the article tells you.

Following Article

Up to User Action section everything works fine, until I tested Create User and found error which said “no transaction found” as you can see below. Maybe because the buffalo version is not the same or the imported library is different.


So I delete everything and try “$ buffalo generate resource users email password” and testing it again. It works fine.


So next thing to do should be override Validate and BeforeCreate.

This is the result


 // before create  
 func (u *User) BeforeCreate(tx *pop.Connection) error {  
      hash, err := bcrypt.GenerateFromPassword([]byte(u.Password), bcrypt.DefaultCost)  
      if err != nil {  
           return errors.WithStack(err)  
      }  
      u.Password = string(hash)  
      return nil  
 }  
 // Validate gets run every time you call a "pop.Validate*" (pop.ValidateAndSave, pop.ValidateAndCreate, pop.ValidateAndUpdate) method.  
 // This method is not required and may be deleted.  
 func (u *User) Validate(tx *pop.Connection) (*validate.Errors, error) {  
      var err error  
      return validate.Validate(  
           &validators.StringIsPresent{Field: u.Email, Name: "Email"},  
           &validators.StringIsPresent{Field: u.Password, Name: "Password"},  
           &validators.FuncValidator{  
                Field:  u.Email,  
                Name:  "Email",  
                Message: "%s is already taken",  
                Fn: func() bool {  
                     var b bool  
                     q := tx.Where("email = ?", u.Email)  
                     if u.ID != uuid.Nil {  
                          q = q.Where("id != ?", u.ID)  
                     }  
                     b, err = q.Exists(u)  
                     if err != nil {  
                          return false  
                     }  
                     return !b  
                },  
           },  
      ), nil  
 }  

Anyway now I understand that the code from litewire maybe have different version then the one I’m using. I’m just gonna restart everything again and I hope I found what went wrong from my code.

It's not done yet. my aim is to make web service with Authorization, Authentication, User Management, maybe Role Base Accsess


Comments

Popular posts from this blog

JavaScript Real Time Calculation

I've been around looking for javascript that can do Real Time Calculation. javascript real time calculation, javascript real time calculation textbox. by some lucky keywords i found this code. this is exactly the code that i want. it really do the real time calculation. and it doesn't need onChange or OnBlur function. Just try it Example + = this is the javascript code <script type='text/javascript' > function startCalc(){   interval = setInterval("calc()",1); } function calc(){   one = document.autoSumForm.firstBox.value;   two = document.autoSumForm.secondBox.value;   document.autoSumForm.thirdBox.value = (one * 1) + (two * 1); } function stopCalc(){   clearInterval(interval); } </script> this is the html code <form name="autoSumForm">   <input class="right" type=text name="firstBox" value="" onFocus="startCalc();" onBlur="stopCalc();"><br>

Learning Odoo 13 Module Development and Review

USING ODOO 13 Introduction This writing supposed to be a cheat sheet for helping myself to learn about Odoo, Odoo Development and also anyone who want to read this.   Odoo already have a good documentation in their websites you can check it here https://www.odoo.com/documentation/13.0/ What you need Odoo source you can get the source from odoo.com or github https://www.odoo.com/page/download or https://github.com/odoo/odoo. Right now I'm using the  https://www.odoo.com/page/download  . I did using the github twice so I'm already familiar with it but now i want to explore something new and maybe we will find something. Python v3.6++ (intermediate level). https://www.python.org/downloads/windows/ You can learn some basic “web development with python” first if you still new at programming. Search with google. Postgresql https://www.postgresql.org/download/ I’m using win Os for development and Visual Studio Code for IDE What should you re

BlackBerry ListField Tutorial Part 2

Blackberry ListField with Clicked Row this time i create based on rtm4bb code. it used manager as TableRowManager to create the field for each row. first i create MenuListField.java