Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/josephspurrier/gowebapp

Basic MVC Web Application in Go
https://github.com/josephspurrier/gowebapp

go golang web-application

Last synced: 2 months ago
JSON representation

Basic MVC Web Application in Go

Awesome Lists containing this project

README

        

# GoWebApp

[![Go Report Card](https://goreportcard.com/badge/github.com/josephspurrier/gowebapp)](https://goreportcard.com/report/github.com/josephspurrier/gowebapp)
[![GoDoc](https://godoc.org/github.com/josephspurrier/gowebapp?status.svg)](https://godoc.org/github.com/josephspurrier/gowebapp)

Basic MVC Web Application in Go

#### I recommend you use Blue Jay which is the latest version of this project: [https://github.com/blue-jay/blueprint](https://github.com/blue-jay/blueprint).

This project demonstrates how to structure and build a website using the Go language without a framework. There is a blog article you can read at [http://www.josephspurrier.com/go-web-app-example/](http://www.josephspurrier.com/go-web-app-example/). There is a full application I built with an earlier version of the project at [https://github.com/verifiedninja/webapp](https://github.com/verifiedninja/webapp). There is an API version of this project at [https://github.com/josephspurrier/gowebapi](https://github.com/josephspurrier/gowebapi).

To download, run the following command:

~~~
go get github.com/josephspurrier/gowebapp
~~~

If you are on Go 1.5, you need to set GOVENDOREXPERIMENT to 1. If you are on Go 1.4 or earlier, the code will not work because it uses the vendor folder.

### Other Branches

- 2021-02-17: There is a branch using Go modules (requires Go 1.13) available here: https://github.com/josephspurrier/gowebapp/tree/modules.
- 2021-05-23: There is a branch using Go embedded assets (requires Go 1.16) available here: https://github.com/josephspurrier/gowebapp/tree/embedded-templates.

## Quick Start with Bolt

The gowebapp.db file will be created once you start the application.

Build and run from the root directory. Open your web browser to: http://localhost. You should see the welcome page.

Navigate to the login page, and then to the register page. Create a new user and you should be able to login. That's it.

## Quick Start with MongoDB

Start MongoDB.

Open config/config.json and edit the Database section so the connection information matches your MongoDB instance. Also, change Type from Bolt to MongoDB.

Build and run from the root directory. Open your web browser to: http://localhost. You should see the welcome page.

Navigate to the login page, and then to the register page. Create a new user and you should be able to login. That's it.

## Quick Start with MySQL

Start MySQL and import config/mysql.sql to create the database and tables.

Open config/config.json and edit the Database section so the connection information matches your MySQL instance. Also, change Type from Bolt to MySQL.

Build and run from the root directory. Open your web browser to: http://localhost. You should see the welcome page.

Navigate to the login page, and then to the register page. Create a new user and you should be able to login. That's it.

## Overview

The web app has a public home page, authenticated home page, login page, register page,
about page, and a simple notepad to demonstrate the CRUD operations.

The entrypoint for the web app is gowebapp.go. The file loads the application settings,
starts the session, connects to the database, sets up the templates, loads
the routes, attaches the middleware, and starts the web server.

The front end is built using Bootstrap with a few small changes to fonts and spacing. The flash
messages are customized so they show up at the bottom right of the screen.

All of the error and warning messages should be either displayed either to the
user or in the console. Informational messages are displayed to the user via
flash messages that disappear after 4 seconds. The flash messages are controlled
by JavaScript in the static folder.

## Structure

Recently, the folder structure changed. After looking at all the forks
and reusing my project in different places, I decided to move the Go code to the
**app** folder inside the **vendor** folder so the github path is not littered
throughout the many imports. I did not want to use relative paths so the vendor
folder seemed like the best option.

The project is organized into the following folders:

~~~
config - application settings and database schema
static - location of statically served files like CSS and JS
template - HTML templates

vendor/app/controller - page logic organized by HTTP methods (GET, POST)
vendor/app/shared - packages for templates, MySQL, cryptography, sessions, and json
vendor/app/model - database queries
vendor/app/route - route information and middleware
~~~

There are a few external packages:

~~~
github.com/gorilla/context - registry for global request variables
github.com/gorilla/sessions - cookie and filesystem sessions
github.com/go-sql-driver/mysql - MySQL driver
github.com/haisum/recaptcha - Google reCAPTCHA support
github.com/jmoiron/sqlx - MySQL general purpose extensions
github.com/josephspurrier/csrfbanana - CSRF protection for gorilla sessions
github.com/julienschmidt/httprouter - high performance HTTP request router
github.com/justinas/alice - middleware chaining
github.com/mattn/go-sqlite3 - SQLite driver
golang.org/x/crypto/bcrypt - password hashing algorithm
~~~

The templates are organized into folders under the **template** folder:

~~~
about/about.tmpl - quick info about the app
index/anon.tmpl - public home page
index/auth.tmpl - home page once you login
login/login.tmpl - login page
notepad/create.tmpl - create note
notepad/read.tmpl - read a note
notepad/update.tmpl - update a note
partial/footer.tmpl - footer
partial/menu.tmpl - menu at the top of all the pages
register/register.tmpl - register page
base.tmpl - base template for all the pages
~~~

## Templates

There are a few template funcs that are available to make working with the templates
and static files easier:

~~~ html

{{CSS "static/css/normalize3.0.0.min.css"}}
parses to

{{JS "static/js/jquery1.11.0.min.js"}}
parses to

{{LINK "register" "Create a new account."}}
parses to
Create a new account.

{{.SomeVariable | NOESCAPE}}

{{.SomeTime | PRETTYTIME}}
parses to format
3:04 PM 01/02/2006
~~~

There are a few variables you can use in templates as well:

~~~ html

{{if eq .AuthLevel "auth"}}
You are logged in.
{{else}}
You are not logged in.
{{end}}

  • About
  • ~~~

    It's also easy to add template-specific code before the closing and