Gorilla is a Go Routing Library used for handling requests and sessions, url routing in GoLang.
We will start with Handling Request with and without using mux(Gorilla Feature)
Handlers
Handler from it's name, it Handles the upcoming requests from the users, the naming convention for creating the function responsible for handling requests should be named to it's functionality if we are creating handler to handle user request to access Home page then it would be named HomeHandler let's go the code of creating HomeHanlder.
func HomeHandler(Response http.ResponseWriter, Request *http.Request){
template,_ := template.ParseFiles("Home.html")
template.Execute(Response,nil)
}
then we need to create the url which this function will map to and the port where the page will be hosted.
func main(){
http.HandleFunc("/Home",HomeHandler)
http.ListenAndServe(":8000",nil)
}
the above example doesn't used mux(request multiplexer) it uses function from library "net/http"
Mux in gorilla store many Handler's in it and map every one to it's url so if we want to use Gorilla we will slightly change the above code to this :-
func main(){
request := mux.NewRouter()
request.HandleFunc("/Home", HomeHandler)
http.Handle("/", request)
http.ListenAndServe(":8000",nil)
}
so what mux basically does it's store more than one route.
Sessions
Sessions is used to store information about user and retrieve this information whenever it's needed for example if I want to create authentication for Facebook so that no one can access certain pages without log in.
Sessions can be used to authenticate whether users logged in or not,
let's apply this our HomeHandler
First we are going to create cookie which will store our wanted attributes in an encrypted form
//creating a new cookie
var (
key = []byte("super-secret-key")
store = sessions.NewCookieStore(key)
)
func HomeHandler(Response http.ResponseWriter, Request *http.Request){
//create a session with the request and cookiename
session, _ := store.Get(Request, "cookie-name")
//return boolean which check whether this session is authenticated or not (true)
auth, ok := session.Values["authenticated"].(bool)
if !auth || !ok {
http.Error(Response, "You Should Log in to Access this page", http.StatusForbidden)
}
template,_ := template.ParseFiles("Home.html")
template.Execute(Response,nil)
}
so when should i set the Authentication to true when should the user have access to the Home Page (Spoiler Alert) when logging in !
So Simply when logging in or Signing up we are gonna set the session.Values["authenticated"] = true
So let's write a log in handler when the user log in
func LoginHandler(Response http.ResponseWriter, Request *http.Request){
session, _ := store.Get(Request, "cookie-name")
session.Values["authenticated"] = true
//name here is going to be the name user write when filling the log in form
session.Values["username"] = name
session.Save(Request, Response)
template,_ := template.ParseFiles("Login.html")
template.Execute(Response,nil)
}
and Voila.
you can do the same for Signup Handler when creating it.
Of-course Gorilla has more features and many of these features i am still exploring with and don't know anything about it you can check them out at Gorilla Github Repo
Top comments (4)
Personally I prefer go-chi/chi ti gorilla because it allows the use of middleware.
The main reason I use gorilla because it’s compatible with the default net/http package and didn’t come across go-chi before, I think I will give it a shot it looks awesome
Even go-chi is compatibile with default net/http. Try it out ;).
Sure thing I will, thanks man