DEV Community

Cover image for Building a Web App With Go, Gin and React

Building a Web App With Go, Gin and React

Francis Sunday on April 21, 2018

This article was originally posted on My Blog TL;DR: In this tutorial, I'll show you how easy it is to build a web application with Go and the G...
Collapse
 
agrim profile image
Agrim Prasad • Edited

Many thanks for this great blog post!

We've been using Gin as our web framework in production, and while it holds up very well under high load, we've seen some issues with the router used within the framework. The issue is detailed in github.com/gin-gonic/gin/issues/388

In brief, if you have two routes defined as follows, you'll see a runtime panic :

r.GET("/teachers/list", func (c *gin.Context){})
r.GET("/teachers/:id/profile", func (c *gin.Context){})
Enter fullscreen mode Exit fullscreen mode

This is because gin's built-in router (based on httprouter) can't handle such wildcard based routing properly. As a result, RESTful services are difficult to define around resource names.

I've been exploring alternatives to using gin, and it seems like go-chi is the most simple and idiomatic router around. It could successfully handle such wildcard based routing. Furthermore, it uses the native Go context object (rather than Gin's custom context object), and its middlewares and handlers are portable across other routing libraries such as gorilla/mux (in case you want to change in the future).

Some comparisons between simple Go http routers made by me in this repo

Collapse
 
criszelaya24 profile image
Cristopher Palacios • Edited

I had an issue and is the next:

"Error parsing token: Invalid issuer."

I followed all the steps and I don't have a clue what could be my problem, I made the right configuration on auth0 and I got all the right credencial from it, Any advise?

I new in all these (Go,Gin and React)! and this post is the best that I have found so far!

Thanks for the time you took it to make it!

Collapse
 
codehakase profile image
Francis Sunday

Hello there Christopher! Glad you checked out the article.

The error message, That's coming from the method that validates the token sent from the Auth0 client. Confirm you have the .env file setup and source on runtime with the Go server

Collapse
 
jstnblau profile image
Justin Blau

Hello, I ran into this same issue. The solution on my side was that although on the Auth0 site the domain is listed as 'example.auth0.com' in the .env and app.jsx files it needed to be set to 'example.auth0.com'

Collapse
 
atinmathur profile image
Atin Mathur • Edited

Hi Justin, I am getting the "Error parsing token: invalid issuer". I think I have set the right domain which is "username.us.auth0.com". And I didn't get your solution to this problem. Can you please explain again?

Collapse
 
zxiang77 profile image
zili

Nice post! I successfully setup a simple application with your example without any problem. But now I have a question beyond the scope of this article: I want to setup some React router e.g.: having a /jokes for the page in your example. but gin is unable to comprehend the url /jokes (vs. the previous /) and return a 404. is there a way to setup the route in gin to also respect the routes defined in React router?
Thanks!

Collapse
 
zxiang77 profile image
zili • Edited

Did some research on serving React with static file (create-react-app.dev/docs/deployme...), so basically I have to define the client side routing paths in the go server, and point them to the same static files, e.g.:
router.Use(static.Serve("/jokes", static.LocalFile("./views", true)))

Also because gin doesn't support path regex, I have to either customize a handler for that case or define all my client side routing on the go backend. a bit tedious, but that works! :)
github.com/gin-gonic/gin/issues/229

Collapse
 
jvarness profile image
Jake Varness

Looks quite extensive! Definitely going to need to check this out later. Bookmarked!

Collapse
 
codehakase profile image
Francis Sunday

Great

Collapse
 
jvarness profile image
Jake Varness

For real though, pretty sure this post could replace this book:

Cloud Native Go: Building Web Applications and Microservices for the Cloud with Go and React (Developer's Library) amazon.com/dp/0672337797/ref=cm_sw...

Collapse
 
natashabieber2 profile image
Natasha Bieber

Thank you Francis for providing the detailed information about web application development. The blog was absolutely amazing! Lot of information which can be useful for web app users, Keep it up. bit.ly/2RHSivk

Collapse
 
samrocksc profile image
Sam Clark

This is amazing! Thank you so much Francis!

Collapse
 
codehakase profile image
Francis Sunday

You're welcome Sam! I'm glad you liked it :)

Collapse
 
codehakase profile image
Francis Sunday

Hello Nico!

I'm glad you enjoyed reading this piece of mine. Do try it out, and also explore other libraries available in the Go ecosystem, Good luck :D

Collapse
 
iwilsonq profile image
Ian Wilson

Awesome post, you went into great detail!

Collapse
 
codehakase profile image
Francis Sunday

Thank you Lan, I'm glad you liked it :)

Collapse
 
leo_png profile image
Leo.png

Great, great post ! Thanks

Collapse
 
codehakase profile image
Francis Sunday

Thanks Leo

Collapse
 
crosdev profile image
José Martínez

Thanks for sharing. It's a great work.

Collapse
 
codehakase profile image
Francis Sunday

Thanks, I'm glad you liked it José.

Collapse
 
bajusz15 profile image
Máté Bajusz

Are you sure it's safe to store JWT in localstorage? From my understanding, this is vulnerable to any XSS attack. Apart from that, I liked the tutorial!

Collapse
 
bajusz15 profile image
Máté Bajusz

Are you sure it's safe to store JWT in localstorage? From my understanding this is vulnerable to XSS attacks. Apart from that, I liked the tutorial!

Collapse
 
bikrambhattacharya profile image
Bikram Bhattacharya

Thanks a lot.

Collapse
 
codehakase profile image
Francis Sunday

You're welcome ☺

Collapse
 
noncototient profile image
Bo

Why use external service for auth and not the Go server itself?

Collapse
 
codehakase profile image
Francis Sunday

The article was focused on that particular service integration.

Collapse
 
rafaela314 profile image
Rafaela Cavalcante

I tried to run and got "unknown field 'SigninMethod' in struct literal of type jwtmiddleware.Options". Any clues on how to solve It?

Collapse
 
crojas profile image
Carlos Rojas L.

well that jokes are very funny...