Skip to content

Latest commit

 

History

History
272 lines (199 loc) · 7.36 KB

SSGProject.md

File metadata and controls

272 lines (199 loc) · 7.36 KB

📜 Day 3: Project #1 - Static Site Generator

🕑Agenda

  1. [05m] 🏆 Objectives
  2. [10m] 💻 Warm Up: Explore SSGs
  3. [10m] 📖 Overview: Static Sites & Generators
  4. [10m] 📖 Starting a New Project
  5. [05m] 📖 Building, Running, & Installing
  6. [10m] 📖 CLIs & Standard Input/Output (I/O)
  7. [15m] 🌴 BREAK
  8. [10m] 📖 Reading & Writing to the Filesystem
  9. [10m] 📖 Working with Templates
  10. [65m] 💻 Activity: Complete MVP
  11. [15m] 📖 Project Checkin
  12. 📚 Resources & Credits

[05m] 🏆 Objectives

  1. Define what a Static Site Generator is, and what it's best-fit use cases are.
  2. Demonstrate setting up a Golang project in an IDE.
  3. Explore packages in the standard library that read and write to the command line, enable file access, and custom templating.
  4. Apply the syntax rules from the tutorial in a real world project.

[10m] 💻 Warm Up: Explore SSGs

First, write down as many differences you can think of between static sites and dynamic sites.

Then, use staticgen.com to research the following questions. If you have extra time, discuss your answers with your peers.

  1. What problem(s) can a static site generator solve?
  2. As a developer, what could you use a static site generator for?

[10m] 📖 Overview: Static Sites & Generators

Static vs. Dynamic Websites

Four key differences from dynamic sites:

  • No server-side language
  • No database
  • Composed of only HTML, CSS, and JavaScript
  • Site files are delivered to the end user exactly as they are on the server

Static Site Generators (SSGs)

  • Intermediary between static and dynamic sites
  • Run a command to turn text files into HTML pages
  • Commit the generated, static HTML pages to GitHub
  • Serve the HTML from GitHub Pages

Use Cases

  • Blogs
  • Portfolios
  • Marketing Websites
  • Course Websites (like this one!)
  • Research Journals
  • Daily Development Logs

Advantages of SSGs

  • Speed
  • Security
  • Content can be version controlled
  • Don't have to deal with a server
  • Can handle lots of sudden internet traffic

Disadvantages of SSGs

  • No real-time content
  • No user input
  • No administrative user interface

David Walsh blogs more about the specifics in An Introduction to Static Site Generators. Be sure to read over this before you start your project!

[10m] 📖 Starting a New Project

Follow the Getting Started section of the project README.

[05m] 📖 Building, Running, & Installing

Execute the following in command in your project's root directory:

To build your program, run:

go build

To run the latest build:

./makesite

To install it for use system wide, run:

go install

[10m] 📖 CLIs & Standard Input/Output (I/O)

Writing to Standard Out (stdout)

  • fmt.Print()
  • fmt.Println()
  • fmt.Sprintf()

Reading from Standard In (stdin)

  • flag package offers a standard library for parsing command line input.
  • Defined using flag.String(), Bool(), Int(), etc.
  • Syntax: -example, -example=text, -example text.
  • Both - and -- may be used for flags.

Example

examplePtr := flag.String("example", "defaultValue", " Help text.")

examplePtr will reference the value for either -example or --example.

The initial value at *examplePtr is defaultValue.

Calling flag.Parse() will parse the command line input and write the value following -example or --example to *examplePtr.

[15m] 🌴 BREAK

[10m] 📖 Reading & Writing to the Filesystem

Reading a File

package main

import (
      "fmt"
      "io/ioutil"
)

func main() {
        fileContents, err := ioutil.ReadFile("first-post.txt")
        if err != nil {
            // A common use of `panic` is to abort if a function returns an error
            // value that we don’t know how to (or want to) handle. This example
            // panics if we get an unexpected error when creating a new file.
            panic(err)
        }
        fmt.Print(string(fileContents))
}

Writing a File

package main

import (
      "fmt"
      "io/ioutil"
)

func main() {
        bytesToWrite := []byte("hello\ngo\n")
        err := ioutil.WriteFile("new-file.txt", bytesToWrite, 0644)
        if err != nil {
            panic(err)
        }
}

[10m] 📖 Working with Templates

Storing the Data

type entry struct {
  Name string
  Done bool
}

type ToDo struct {
  User string
  List []entry
}

Creating a Template

<!DOCTYPE html>
<html>
  <head>
    <title>Go To-Do list</title>
  </head>
  <body>
    <p>
      To-Do list for user: {{ .User }}
    </p>
    <table>
        <tr>
          <td>Task</td>
          <td>Done</td>
      </tr>
        {{ with .List }}
          {{ range . }}
            <tr>
                  <td>{{ .Name }}</td>
                  <td>{{ if .Done }}Yes{{ else }}No{{ end }}</td>
            </tr>
          {{ end }}
        {{ end }}
    </table>
  </body>
</html>

Writing a Template to Standard Out (stdout)

package main

import (
        "html/template"
        "os"
)

type entry struct {
        Name string
        Done bool
}

type ToDo struct {
        User string
        List []entry
}

func main() {
        t := template.Must(template.New("template.tmpl").ParseFiles("new.html"))
        err = t.Execute(os.Stdout, todos)
        if err != nil {
          panic(err)
        }
}

[65m] 💻 Activity: Complete MVP

Complete the MVP as described in the requirements in the order they appear. If you finish early, move on to the stretch challenges.

Return 15 minutes before the end of class for your project status update.

[15m] 📖 Project Checkin

Students will check in with the instructor on the status of their MVP.

This is a great time to ask questions or get unblocked so you can make progress before our next class!

📚 Resources & Credits