~reesmichael1/chantpointer

e54e9a432f7bde8f4421ab64c8c0eef774789d62 — Michael Rees 3 years ago a0fbff0
Reorganize files
3 files changed, 55 insertions(+), 48 deletions(-)

A chant.go
A handlers.go
M main.go
A chant.go => chant.go +18 -0
@@ 0,0 1,18 @@
package main

// Chant represents the collection of information that make up the chant
// In practice, it is the type given to the template to generate
// the LaTeX source
type Chant struct {
	Title     string
	Subtitle  string
	PointSize int
	Verses    []Verse
}

// Verse holds the two lines that make up
// an individual verse in the chant
type Verse struct {
	FirstPart  string
	SecondPart string
}

A handlers.go => handlers.go +36 -0
@@ 0,0 1,36 @@
package main

import (
	"net/http"
	"text/template"
)

// IndexHandler handles incoming requests to the index endpoint
func IndexHandler(w http.ResponseWriter, r *http.Request) {
	t, err := template.ParseFiles("templates/psalm.tem")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	c := Chant{
		Verses: []Verse{
			Verse{
				FirstPart:  "this is | the first part",
				SecondPart: `this is | the sec\"ond | part`,
			},
			Verse{
				FirstPart:  "and another verse,",
				SecondPart: "here you go",
			},
		},
		Title:     "Psalm Title",
		Subtitle:  "Subtitle",
		PointSize: 14,
	}

	err = t.Execute(w, c)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

M main.go => main.go +1 -48
@@ 3,56 3,9 @@ package main
import (
	"log"
	"net/http"
	"text/template"
)

func handler(w http.ResponseWriter, r *http.Request) {
	t, err := template.ParseFiles("templates/psalm.tem")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	c := Chant{
		Verses: []Verse{
			Verse{
				FirstPart:  "this is the first part",
				SecondPart: "this is the second part",
			},
			Verse{
				FirstPart:  "and another verse,",
				SecondPart: "here you go",
			},
		},
		Title:     "Psalm Title",
		Subtitle:  "Subtitle",
		PointSize: 14,
	}

	err = t.Execute(w, c)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

// Verse holds the two lines that make up
// an individual verse in the chant
type Verse struct {
	FirstPart  string
	SecondPart string
}

// Chant represents the collection of information that make up the chant
// In practice, it is the type given to the template to generate
// the LaTeX source
type Chant struct {
	Title     string
	Subtitle  string
	PointSize int
	Verses    []Verse
}

func main() {
	http.HandleFunc("/", handler)
	http.HandleFunc("/", IndexHandler)
	log.Fatal(http.ListenAndServe(":8080", nil))
}