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...

iOS 5 Creating eCommerce with Shopping Cart App Part 1

[Created 11-28-2012] it seems like there are tons of tutorial ios and xcode for beginner out there. you can view the list at my post here http://javalearning-adventure.blogspot.com/2012/11/learning-xcode.html that's the link that i always update if i find anything. this tutorial will consist of step by step in creating Shopping Cart App. There will be Browse Product, Shopping Cart, and Checkout Method. I will update these later after i satisfied with the end result. These tutorial will also include web programming using php. but these php will only using basic sample not the very complex with mysql database and everything. these php pages will only creating json output. i think you don't need to learn php and json. you should only learn how to handle json using objective-c. I am still a beginner in xcode and objective-c, its still only 1 month since i started these tutorial. requirements suppose to be iOS 5 with xcode 4. [i will update these later][postponed...

BlackBerry ListField Tutorial Part 3

This Part 3 will consist of Networking, ListField, JSON and Thread. I'm really sorry for pending this part 3. I've been busy making money and still failed. Its just really hard to live in third world country by coding. anyway, There are few Library that you will need creating ListField with JSON data, but we wont touch that area first. we need to learn the basic. * note added : Aug 26 2013 The basic should be : Networking Thread ListField [if i find anything, will be add here]