Skip to content

jcbwlkr/goblog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goblog

This is a simple example Go application based on Building Web Apps With Go by Jeremy Saenz @codegangsta.

The files and folders included are documented here:

.
├── README.md       # This file. How meta!
├── main.go         # The main entry point to our application. It boots up the server and listens for requests.
├── posts.go        # Additional types and functions used by main. It is part of this package so it's included automatically.
├── Procfile        # Required by Heroku. It defines how to run our app.
├── public          # A folder of static assets that will be served up directly.
│   ├── app.js      # Our front end code for adding & removing blog posts.
│   ├── favicon.ico # A beautiful favicon image borrowed from golang.org
│   └── index.html  # Our presentation file.
└── vendor          # Third party Go libraries we are using.
    ├── github.com  #
    │   └── ...     # snipped...
    └── vendor.json # A file generated by govendor that describes our dependencies

Our Go dependencies are managed with govendor. If you add or remove a dependency you should use that tool to update the vendor/ folder.

Development

To run this application locally first clone it.

go get github.com/jcbwlkr/goblog

Then switch to that directory, build the app, and run the executable.

cd $GOPATH/src/github.com/jcbwlkr/goblog
go build
./goblog

Your app will then be accessible through your browser at http://localhost:8080.

If you change the Go code you must stop the server, rebuild it, and restart it. You can stop the server with Ctrl-C. You can build and run the file in one step by doing go run *.go.

Alternatively if you want the app to restart itself when you change any .go files you can install gin with go get github.com/codegangsta/gin and then building/running the app just requires the command gin. If you do this then your application will, by default, listen on port 3000 http://localhost:3000.

Deployment

You can easily deploy this app to Heroku. First create a Heroku account.

Next install the heroku toolbelt.

Once the toolbelt is installed you must log in from your terminal. Do this with the command

heroku login

This will prompt you for your Heroku email and password.

Before the first time you deploy this app you must create it on heroku. Ensure you are in the goblog directory in your terminal then run

heroku create

This will create an application on Heroku and set the git remote url in the local git config.

To deploy run

git push heroku master

You will see a lot of text scroll by as Heroku builds your app.

To view your app in a browser run

heroku open

Note that if you see an application error you may need to run a command to scale up a Heroku dyno. If you get an error try running this and see if it works

heroku ps:scale web=1

Ideas For Improvement

Persistence

Note that the "database" in this application is just an in-memory slice. Every time you restart the application (or the heroku dyno goes to sleep) it forgets all of your blog posts. To change this look into using the database/sql package.

Middleware

It is common for web applications to have a chain of middleware handlers that run before / after your regular handlers. An easy middleware to add would be one that logs before and after each request (and includes the total time it took). I recommend using github.com/justinas/alice for this purpose.

Authentication

Right now any user can delete any post. Require your users to log in with GitHub using github.com/markbates/goth.

Cross Site Scripting

Our front end exposes a huge security flaw. When we get the HTML from the user we stick it straight into the DOM. Any <script> tags will get executed by the browser. Research Cross Site Scripting (XSS) and find a way to mitigate this.

About

A simple example web application

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published