Skip to content

Learning Go web dev by rebuilding the πŸ’Ώ Remix Jokes app πŸ€ͺ

Notifications You must be signed in to change notification settings

Fghurayri/go-remix-jokes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

28 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Go Remix Jokes

Remix Jokes - Go Edition

Remix is a React framework for building web applications. I used my momentum after learning Remix by building Thankful to rebuild their Jokes app in Go.

In summary, the application is a trivial CRUD app that allows authenticated users to browse and add jokes.

From the Go side, I was pleased by the typing system and how the compiler was helping me explore the language and assisting me towards having a functional code!

Project Structure

The following is the folder structure for the project.

.
β”œβ”€β”€ html
β”‚   β”œβ”€β”€ auth.go.html
β”‚   β”œβ”€β”€ index.go.html
β”‚   β”œβ”€β”€ jokes
β”‚   β”‚   β”œβ”€β”€ index.go.html
β”‚   β”‚   β”œβ”€β”€ joke.go.html
β”‚   β”‚   └── new.go.html
β”‚   └── layouts
β”‚       β”œβ”€β”€ jokes.go.html
β”‚       β”œβ”€β”€ nav.go.html
β”‚       └── root.go.html
β”œβ”€β”€ lib
β”‚   β”œβ”€β”€ db
β”‚   β”‚   └── db.go
β”‚   β”œβ”€β”€ handlers
β”‚   β”‚   β”œβ”€β”€ auth.go
β”‚   β”‚   β”œβ”€β”€ handlers.go
β”‚   β”‚   β”œβ”€β”€ index.go
β”‚   β”‚   └── jokes.go
β”‚   β”œβ”€β”€ models
β”‚   β”‚   β”œβ”€β”€ joke.go
β”‚   β”‚   └── user.go
β”‚   └── utils
β”œβ”€β”€ main.go
└── readme.md

Moreover, this project has 4 different packages:

  • The handlers package is responsible for all the HTTP side.
  • The models package is responsible for modeling the domain.
  • The utils package is responsible for all the helpers needed.
  • The db package is responsible for all stuff related to the DB.

I must say that this is not a best practice. I am not even sure about this organization. However, this feels ok now.

What I Learned

Learning a new language by building a web CRUD application may seem dull. However, having simple requirements in a controlled scope is powerful to focus on what's more important - learning the language!

Rebuilding the Remix Jokes app in Go allowed me to explore the rich standard library. Moreover, I have a better understanding of packages VS folders VS files. Finally, I got to experience how to integrate 3rd party libraries into my application.

To build a simple CRUD web application, you need an HTTP server, a way to serve some HTML files dynamically, and a mechanism to persist and manipulate your data.

The net/http Standard Package

The net/http standard package helps in serving web requests.

http.HandleFunc("/", Index)
http.ListenAndServe(":3000", nil)

The above snippet enables serving web requests on port 3000 on the / path by executing the Index handler.

The html/template Standard Package

The html/template package helps serve HTML templates with dynamic data and reusable layout.

	t, _ := template.ParseFiles(files...)
	t.Execute(w, d)

The above snippet enables parsing any number of go.html files and allows for the {{}} syntax to have dynamic HTML.

The Gorm ORM

Although the Go standard library supports working with SQL DBs using database/sql, I decided to go with Gorm as the ORM in this CRUD application. The reason is to get a taste of using (install, read the docs, and utilize) external libraries.

type User struct {
	gorm.Model
	Username     string
	PasswordHash string
	Jokes        []Joke
}

The above snippet helps create the user model, which reflects the users table with a one-to-many relationship.

About

Learning Go web dev by rebuilding the πŸ’Ώ Remix Jokes app πŸ€ͺ

Topics

Resources

Stars

Watchers

Forks