Skip to content

DehuaZhao/go-web-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gowebgo

A Golang Wiki Page Tutorial Web Application

Overview

This Application takes a Go leaner through the process of building a web application using package net/http and html/template from Go standard library.

This is a stepping coding instructor to a tutorial article by golang.org. Each tagged commit is a separate teaching towards a very simple web app.

Covered in this tutorial:

  • Creating a data structure with load and save methods
  • Using the net/http package to build web applications
  • Using the html/template package to process HTML templates
  • Using the regexp package to validate user input
  • Using closures

Prerequisites

Git

  • A good place to learn about setting up git is here.
  • You can find documentation and download git here.

Go

Assumed knowledge

  • Understanding of basic web technologies (HTTP, HTML)
  • Basic UNIX/DOS command-line knowledge

Commits / Tutorial Outline

You can check out any point of the tutorial using:

$ git checkout step-?

To see the changes made between any two lessons use the git diff command:

$ git diff step-?..step-?

Step-0 Bootstrapping

  • Create a gowiki.go file.
  • Add the Page struct.
  • Add the save and loadPage function.
  • Write the main function to test what we've written.

Step-1 Error Handling

  • Modify the loadPage to let it return an error if ReadFile encounters one.

You can compile and run the program like this:

$ go build gowiki.go
$ ./gowiki
This is a sample page.

Step-2 Serve Pages

  • Use net/http package to serve our wiki pages.
  • Add the viewHandler to handle URLs.
  • create some page data like test.txt

Let's compile, run our code and visit http://localhost:8080/view/test to see what we have by far.

Step-3 Editing Pages

  • Add the editHandler.

Now we have an editing page http://localhost:8080/edit/test.

As you might have noticed, we comment out the registration of saveHandler in main. We will come back on that later.

Step-4 Better HTML

  • Use html/template to keep the HTML in a separate file.
  • Keep hard-coded HTMLs in separate files.

Step-5 Refactor

In this step, we will not be adding any new functionality to our application. Instead, we are going to take a step back, refactor our codebase and fix a potential issue.

  • Move all templating code to its own function.
  • Handle non-existent pages in viewHandler.

Recompile the code, run and visit http://localhost:8080/view/APageThatDoesntExist

Step-6 Saving Pages

  • Implement the saveHandler and uncommenting the related line in main.

Step-7 More Error Handling

  • Handle errors in renderTemplate.
  • Handle errors in saveTemplate.

Step-8 Template Caching

  • Refactor to call ParseFiles once at program initialization, parsing all templates into a single *Template.
  • Create the global variable named templates, and initialize it with ParseFiles.
  • Modify the renderTemplate function to call the templates.ExecuteTemplate method with the name of the appropriate template.

Step-9 Validation

  • Use regexp package and create the global variable validPath to store our validation expression.
  • Add the function getTitle that uses the validPath expression to validate path and extract the page title.
  • Put a call to getTitle in each of the handlers.

Step-10 Using Function Literals and Closures

  • Re-write the function definition of each of the handlers to accept a title string.
  • Define the wrapper function that takes a function of the above type, and returns a function of type http.HandlerFunc.
  • Wrap the handler functions with makeHandler in main.
  • Remove the calls to getTitle from the handler functions, making them much simpler.

Try it out!

Now we have finished our little wiki page application. Recompile the code, and run the app:

$ go build gowiki.go
$ ./gowiki

Visiting http://localhost:8080/view/ANewPage should present you with the page edit form. You should then be able to enter some text, click 'Save', and be redirected to the newly created page.

More tasks

In the article there are some simple tasks left for readers:

  • Store templates in tmpl/ and page data in data/.
  • Add a handler to make the web root redirect to /view/FrontPage.
  • Spruce up the page templates by making them valid HTML and adding some CSS rules.
  • Implement inter-page linking by converting instances of [PageName] to <a href="/view/PageName">PageName</a>. (hint: you could use regexp.ReplaceAllFunc to do this)

And we will continue Go with these extra tasks.

Step-11 Refactor: File Structure

  • Store templates in tmpl/ and page data in data/.
  • Change file directory in save(), loadPage() and templates.