Building a Go web "framework"
November 14, 2022
Maximilian Patterson
What is GoWeb?
This article has been updated a few times and a lot of progress has been made. I plan on writing a new article soon once I get some more essential features done. I keep the checklist updated, so you can see what I’ve done so far, alternatively check the Git repo! - Feb 14, 2023
Currently, my largest and most interesting project (GoWeb) is a web “framework” made purely in Go and its standard
library. I mean framework in a loose sense, in reality, it’s a boilerplate project including some packages for helping
with things like CSRF protection and user authentication. I also plan to create a helper utility set of utilities
built into a user’s website to generate a new project with and without desired functionality help with migrations
and things (see checklist). Here is the current file structure of the base project you’ll see when creating a new
project in GoWeb:
Feb 14, 2023 UPDATED file tree:
│ main.go
│
├───app
│ app.go
│
├───config
│ config.go
│
├───controllers
│ getController.go
│ postController.go
│
├───database
│ databaseConnection.go
│ migrate.go
│
├───logs
│ example.log
│
├───models
│ migrations.go
│ user.go
│
├───routes
│ getRoutes.go
│ postRoutes.go
│
├───security
│ csrf.go
│
├───static
│ test.log
│
├───templates
│ │ base.html
│ │
│ └───pages
│ home.html
│ login.html
│ register.html
│
└───templating
templateHelper.go
Why?
I have roughly a year of Laravel work under my belt, I love how clean the projects are structured, things like having all your routes in one place, are extremely useful. To my knowledge, some things like this already exist in the Go ecosystem like Echo and Gin but do not provide a boilerplate project, and rely heavily on non-standard packages, which are outside the scope of Go’s compatibility promise and not-necessarily going to receive the epic performance boosts that we see with different packages in the standard library. So my goal with all of this is to keep it vanilla while still providing a clean and easy-to-use project structure akin to Laravel. When using GoWeb, you’ll be able to create a new project with a single command, and then you’ll be able to start writing your code in the controllers and models folders, and the rest of the project will be handled by GoWeb, just like Laravel. And if you want to dip your toes into some other packages that GoWeb provides, you can do that too, and you’ll be able to see how they work and modify them in your projects.
Current state of the project and future plans
Here’s what I’ve accomplished so far:
- Templating
- Config file loading
- Database functionality (Postgres by default)
- CSRF protection
- User authentication
- Register
- Login
- Logout
- Cookie-based sessions
- Password hashing with bcrypt
- Routing
- Static files
- Make sure everything in the templates and static folders are embedded into the binary (admittedly and oversight on my part)
- Migration functionality and auto migrate on server start
Here’s what I plan to accomplish by the end of the year eventually:
Maybe rework config file handlingConfig files work as intended quite well, rethought this, and I’m going to leave it as is- General clean up, making sure error messages are uniform and helpful
Any necessary project restructuringStructure is good- Make dedicated sessions table and model so that users can have multiple sessions
- Remember me functionality (cookie won’t expire until user logs out or deletes it), make sure server clears AuthTokens after 30 days
- Scheduled tasks
- Ability to run scheduled tasks with CLI argument (this will require arg handler package)
Longer-term goals:
-
Make project structure import utility packages over git, this will allow for easier updates and more flexibility - Create a
CLI utilityset of in repo utilities (that get built into a user’s website) togenerate a new project with and without desired functionalityrun migrations, help create an env file, and perform other tasks. Although inside the repo, these utilities will be imported via git to help with me updating them for users. -
Utility module to help with parsing CLI arguments (migrations and things) -
Built-in email functionality (helper method for sending emails, and config file support for setting up email server)
Final thoughts
Currently, the repo is not public. There are some things I’d like to finish and clean up first, but I expect to have
it
public by the end of this year. If you have any suggestions based on the information given here, specifically the file
structure, or any questions, feel free to email me (linked on the side). I’m always looking to improve, and it’s
very important to me that I get everything right. Thanks for reading!