-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewbys.go
More file actions
101 lines (88 loc) · 3.03 KB
/
newbys.go
File metadata and controls
101 lines (88 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
)
type StringPage struct {
Name string
Content string
Stylesheets []string
Javascripts []string
}
type Page struct {
Content template.HTML
Stylesheets template.HTML
Javascripts template.HTML
}
var webPages map[string]Page = make(map[string]Page)
var baseTemplate = template.Must(template.ParseFiles("templates/components/base.html"))
func setUpPages() error {
stringPages := []StringPage{
{Name: "home", Content: "home", Stylesheets: []string{"home"}, Javascripts: []string{"home"}},
{Name: "about", Content: "about", Stylesheets: []string{"about"}, Javascripts: []string{"about"}},
{Name: "projects", Content: "projects", Stylesheets: []string{"projects"}, Javascripts: []string{"projects"}},
{Name: "resume", Content: "resume", Stylesheets: []string{"resume"}, Javascripts: []string{"resume"}},
{Name: "dotfiles", Content: "dotfiles", Stylesheets: []string{"dotfiles"}, Javascripts: []string{"dotfiles"}},
{Name: "contact", Content: "contact", Stylesheets: []string{"contact"}, Javascripts: []string{"contact"}},
{Name: "notFound", Content: "notFound", Stylesheets: []string{"notFound"}, Javascripts: []string{"notFound"}},
}
for _, page := range stringPages {
newPage, err := loadPage(page.Content, page.Stylesheets, page.Javascripts)
if err != nil {
return err
}
webPages[page.Name] = *newPage
}
return nil
}
//Creates page from html, css, and javascript given
func loadPage(page string, style []string, scripts []string) (*Page, error) {
//Converts given styling and scripts to link and script tags
stylesheets, js := "", ""
for _, style := range style {
stylesheets = fmt.Sprintf("%s<link rel='stylesheet' href='styles/%s.css'>", stylesheets, style)
}
for _, script := range scripts {
js = fmt.Sprintf("%s<script src='js/%s.js'></script>", js, script)
}
loadedPage := &Page{
Stylesheets: template.HTML(stylesheets),
Javascripts: template.HTML(js),
}
//Gets html content from the html file
content, err := ioutil.ReadFile(fmt.Sprintf("templates/pages/%s.html", page))
if err != nil {
return nil, err
}
loadedPage.Content = template.HTML(content)
return loadedPage, nil
}
//The route handler
func pageHandler(w http.ResponseWriter, r *http.Request) {
//Gets the page from the webpages
page := r.URL.Path[1:]
pageContent, exists := webPages[page]
if !exists {
pageContent = webPages["notFound"]
}
//Creates the page with the base template
err := baseTemplate.Execute(w, pageContent)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
setUpPages()
//Sets up the file server, allowing access to javascript and styles
fileServer := http.FileServer(http.Dir("./static"))
//Sets up routes for the webpages
http.HandleFunc("/", pageHandler)
//Creates the fileserver the pages will use to access styles and javascript
http.Handle("/styles/", fileServer)
http.Handle("/assets/", fileServer)
http.Handle("/js/", fileServer)
log.Fatal(http.ListenAndServe(":8080", nil))
}