aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCorey Prophitt <git@prophitt.me>2022-07-26 15:57:24 -0700
committerCorey Prophitt <git@prophitt.me>2022-07-26 15:57:24 -0700
commite0d9439c262aa4ced705badd18bafe58b13f9075 (patch)
tree3f755ab95793570f14fe91e8e8108f46eac2345c
parentdb11ce7fd1020abb1f54443d563a8d328cad3dd9 (diff)
downloadprophitt.me-e0d9439c262aa4ced705badd18bafe58b13f9075.tar.gz
prophitt.me-e0d9439c262aa4ced705badd18bafe58b13f9075.zip
Added asset, file and template handlingv1.0.0
Added an index page.
-rw-r--r--assets/files/site.webmanifest19
-rw-r--r--assets/images/android-chrome-192x192.pngbin0 -> 4797 bytes
-rw-r--r--assets/images/android-chrome-384x384.pngbin0 -> 15440 bytes
-rw-r--r--assets/images/favicon-16x16.pngbin0 -> 468 bytes
-rw-r--r--assets/images/favicon-32x32.pngbin0 -> 904 bytes
-rw-r--r--assets/images/glider.pngbin0 -> 12683 bytes
-rw-r--r--assets/images/safari-pinned-tab.svg1
-rw-r--r--config.go6
-rw-r--r--internal/web/assets.go34
-rw-r--r--internal/web/index.go5
-rw-r--r--internal/web/server.go50
-rw-r--r--templates/pages/index.tmpl20
-rw-r--r--templates/partials/metatags.tmpl15
13 files changed, 145 insertions, 5 deletions
diff --git a/assets/files/site.webmanifest b/assets/files/site.webmanifest
new file mode 100644
index 0000000..7b49266
--- /dev/null
+++ b/assets/files/site.webmanifest
@@ -0,0 +1,19 @@
1{
2 "name": "Corey Prophitt",
3 "short_name": "CP",
4 "icons": [
5 {
6 "src": "/assets/images/android-chrome-192x192.png",
7 "sizes": "192x192",
8 "type": "image/png"
9 },
10 {
11 "src": "/assets/images/android-chrome-384x384.png",
12 "sizes": "384x384",
13 "type": "image/png"
14 }
15 ],
16 "theme_color": "#ffffff",
17 "background_color": "#ffffff",
18 "display": "standalone"
19}
diff --git a/assets/images/android-chrome-192x192.png b/assets/images/android-chrome-192x192.png
new file mode 100644
index 0000000..892dfd7
--- /dev/null
+++ b/assets/images/android-chrome-192x192.png
Binary files differ
diff --git a/assets/images/android-chrome-384x384.png b/assets/images/android-chrome-384x384.png
new file mode 100644
index 0000000..884851a
--- /dev/null
+++ b/assets/images/android-chrome-384x384.png
Binary files differ
diff --git a/assets/images/favicon-16x16.png b/assets/images/favicon-16x16.png
new file mode 100644
index 0000000..5dea424
--- /dev/null
+++ b/assets/images/favicon-16x16.png
Binary files differ
diff --git a/assets/images/favicon-32x32.png b/assets/images/favicon-32x32.png
new file mode 100644
index 0000000..bfe5886
--- /dev/null
+++ b/assets/images/favicon-32x32.png
Binary files differ
diff --git a/assets/images/glider.png b/assets/images/glider.png
new file mode 100644
index 0000000..d5bb39b
--- /dev/null
+++ b/assets/images/glider.png
Binary files differ
diff --git a/assets/images/safari-pinned-tab.svg b/assets/images/safari-pinned-tab.svg
new file mode 100644
index 0000000..85d360e
--- /dev/null
+++ b/assets/images/safari-pinned-tab.svg
@@ -0,0 +1 @@
<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="596" height="596" viewBox="0 0 447 447"><path d="M0 223.5V447h447V0H0v223.5zM244.1 58.4C260.2 63.3 274 78.2 277 94c4.8 25.1-7.2 45.8-32.3 55.9-4.5 1.8-16.6 2.4-23.2 1.2-16.6-3.1-31.1-15.9-36.4-32.1-2.3-7.1-2.9-21.3-1-27.1 5-16 16.4-27.9 31.3-33 6.4-2.2 22.2-2.5 28.7-.5zm108.4 110.1c16.9 4.4 29.9 17.3 34.7 34.4 7.4 26.8-10.6 54.2-38.9 59.1-27.8 4.9-55.3-18.5-55.3-47 0-20.7 15.6-40.9 36-46.6 6.2-1.7 16.8-1.7 23.5.1zM125.7 282.6c17.2 3.9 28.9 14.5 36.1 32.5 1.8 4.4 2.4 15.1 1.3 21.9-1.1 6.8-6.5 18.7-10.3 23-16.1 17.8-39.9 21.9-60.2 10.6C73.6 360 64.1 337 70.1 316c4.7-16.4 19-29.8 35.9-33.6 7.6-1.7 11.8-1.6 19.7.2zm114.3-.2c14.5 3.5 25.9 12 32.7 24.6 3.7 6.9 4.8 12.2 4.7 23.5 0 10.4-.2 11.4-3.6 18.6-7.9 16.9-25 27.9-43.3 27.9-26.6 0-47.5-21.2-47.5-47.9 0-14.4 4.3-24.4 14.6-34.1 12.3-11.4 27.9-16.1 42.4-12.6zm110.6 0c17.9 3.8 32.1 17.6 37 35.9 4.1 15.5-1 32.5-13.4 44.7-3.7 3.6-8.9 7.6-11.6 8.9-27.4 13.5-59.5-1.1-67.8-30.8-4.5-16.2.2-33 12.8-45.5 11.8-11.9 27.3-16.6 43-13.2z"/></svg> \ No newline at end of file
diff --git a/config.go b/config.go
index 6dcf3d3..b7f16d5 100644
--- a/config.go
+++ b/config.go
@@ -3,10 +3,14 @@ package prophitt
3import ( 3import (
4 "embed" 4 "embed"
5 "os" 5 "os"
6 "time"
6) 7)
7 8
9// Version is a timestamp for when the server ran.
10var Version = time.Now().Unix()
11
8// VFS is our embedded file system. 12// VFS is our embedded file system.
9//go:embed assets/**/* 13//go:embed assets/**/* templates/**/*.tmpl
10var VFS embed.FS 14var VFS embed.FS
11 15
12// IsDevelopment returns true if our environment is development. 16// IsDevelopment returns true if our environment is development.
diff --git a/internal/web/assets.go b/internal/web/assets.go
new file mode 100644
index 0000000..470addd
--- /dev/null
+++ b/internal/web/assets.go
@@ -0,0 +1,34 @@
1package web
2
3import (
4 "net/http"
5 "strings"
6
7 "prophitt.me"
8)
9
10func neuter(next http.Handler) http.Handler {
11 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
12 if strings.HasSuffix(r.URL.Path, "/") {
13 http.NotFound(w, r)
14 return
15 }
16
17 next.ServeHTTP(w, r)
18 })
19}
20
21func init() {
22 // Handles all asset requests. Since we're embedding all assets into the
23 // binary we need to serve the assets from Go. If we were not, we could
24 // directly access the assets via Nginx/Apache and leave the application
25 // server alone.
26 routes.register(route{
27 Path: "/assets/",
28 Handler: func() func(http.ResponseWriter, *http.Request) {
29 return logger(func(w http.ResponseWriter, r *http.Request) {
30 neuter(http.FileServer(http.FS(prophitt.VFS))).ServeHTTP(w, r)
31 })
32 }(),
33 })
34}
diff --git a/internal/web/index.go b/internal/web/index.go
index 1880b8d..8517500 100644
--- a/internal/web/index.go
+++ b/internal/web/index.go
@@ -1,7 +1,6 @@
1package web 1package web
2 2
3import ( 3import (
4 "fmt"
5 "io/fs" 4 "io/fs"
6 "log" 5 "log"
7 "net/http" 6 "net/http"
@@ -26,6 +25,7 @@ var specialFilePaths []specialFilePath = []specialFilePath{
26 {IncomingPath: "/favicon.ico", Sub: "assets/images"}, 25 {IncomingPath: "/favicon.ico", Sub: "assets/images"},
27 {IncomingPath: "/robots.txt", Sub: "assets/files"}, 26 {IncomingPath: "/robots.txt", Sub: "assets/files"},
28 {IncomingPath: "/sitemap.xml", Sub: "assets/files"}, 27 {IncomingPath: "/sitemap.xml", Sub: "assets/files"},
28 {IncomingPath: "/site.webmanifest", Sub: "assets/files"},
29} 29}
30 30
31// Handle special files like robots.txt, sitemap.xml, etc. These are accessed 31// Handle special files like robots.txt, sitemap.xml, etc. These are accessed
@@ -66,8 +66,7 @@ func init() {
66 /* TODO: We could render a "not found" page instead of an error response. */ 66 /* TODO: We could render a "not found" page instead of an error response. */
67 http.NotFound(w, r) 67 http.NotFound(w, r)
68 } else { 68 } else {
69 // NOTE: This is the actual landing page page ("/"). 69 exectmpl(w, "pages/index.tmpl", nil)
70 fmt.Fprintf(w, "\tHello World!\n")
71 } 70 }
72 }), 71 }),
73 }) 72 })
diff --git a/internal/web/server.go b/internal/web/server.go
index 9ae5211..4d93fa7 100644
--- a/internal/web/server.go
+++ b/internal/web/server.go
@@ -3,12 +3,16 @@ package web
3import ( 3import (
4 "context" 4 "context"
5 "fmt" 5 "fmt"
6 "html/template"
7 "io"
6 "log" 8 "log"
7 "net/http" 9 "net/http"
8 "os" 10 "os"
9 "os/signal" 11 "os/signal"
10 "syscall" 12 "syscall"
11 "time" 13 "time"
14
15 "prophitt.me"
12) 16)
13 17
14const ( 18const (
@@ -109,6 +113,48 @@ func logger(h http.HandlerFunc) func(w http.ResponseWriter, r *http.Request) {
109 } 113 }
110} 114}
111 115
116var (
117 // Template cache for all of our parsed views and partials.
118 templates *template.Template
119)
120
121func parsetmpls() {
122 patterns := []string{
123 "templates/pages/*.tmpl",
124 "templates/partials/*.tmpl",
125 }
126
127 //
128 // Set up the initial template functions and prepare to parse. These are
129 // all loaded via the "helpers.go" file.
130 //
131 templates = template.
132 New("").
133 Funcs(template.FuncMap{
134 "started_at": func() int64 {
135 return prophitt.Version
136 },
137 /* TODO: Add any helpers here... */
138 })
139
140 //
141 // Parse our embedded templates. If this fails, nothing will work.
142 //
143 if _, err := templates.ParseFS(prophitt.VFS, patterns...); err != nil {
144 log.Fatal(err)
145 }
146}
147
148func exectmpl(w io.Writer, name string, data interface{}) error {
149 err := templates.ExecuteTemplate(w, name, data)
150
151 if err != nil {
152 log.Printf("server.go: failed to execute template; %s", err)
153 }
154
155 return err
156}
157
112// Run will start the web server and handle requests. This is a blocking 158// Run will start the web server and handle requests. This is a blocking
113// function. 159// function.
114func Run() error { 160func Run() error {
@@ -116,6 +162,8 @@ func Run() error {
116 return err 162 return err
117 } 163 }
118 164
165 parsetmpls()
166
119 log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime)) 167 log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
120 168
121 mux := http.NewServeMux() 169 mux := http.NewServeMux()
@@ -135,7 +183,7 @@ func Run() error {
135 go func() { 183 go func() {
136 if err := srv.ListenAndServe(); err != nil { 184 if err := srv.ListenAndServe(); err != nil {
137 if err != http.ErrServerClosed { 185 if err != http.ErrServerClosed {
138 log.Printf("web: srv.ListenAndServe returned an error; %s\n", err) 186 log.Printf("server.go: srv.ListenAndServe returned an error; %s\n", err)
139 } 187 }
140 } 188 }
141 }() 189 }()
diff --git a/templates/pages/index.tmpl b/templates/pages/index.tmpl
new file mode 100644
index 0000000..7bc6044
--- /dev/null
+++ b/templates/pages/index.tmpl
@@ -0,0 +1,20 @@
1{{ define "pages/index.tmpl" }}
2
3<!DOCTYPE html>
4<html lang="en">
5 <head>
6 {{ template "partials/metatags.tmpl" }}
7
8 <title>Corey Prophitt's Website</title>
9
10 <meta name="og:title" content="Corey Prophitt's Website">
11 <meta name="og:description" content="Husband, father of two, developer of many things.">
12 <meta name="og:image" content="https://prophitt.me/assets/images/glider.png">
13 </head>
14
15 <body>
16 <pre>/* prophitt.me: <a href="https://git.prophitt.me" target="_blank">src</a> */</pre>
17 </body>
18</html>
19
20{{ end }}
diff --git a/templates/partials/metatags.tmpl b/templates/partials/metatags.tmpl
new file mode 100644
index 0000000..3bf485f
--- /dev/null
+++ b/templates/partials/metatags.tmpl
@@ -0,0 +1,15 @@
1{{ define "partials/metatags.tmpl" }}
2<meta name="viewport" content="width=device-width, initial-scale=1.0">
3
4<link rel="shortcut icon" href="/assets/images/favicon.ico">
5<link rel="shortcut icon" type="image/png" sizes="32x32" href="/assets/images/favicon-32x32.png">
6<link rel="shortcut icon" type="image/png" sizes="16x16" href="/assets/images/favicon-16x16.png">
7<link rel="manifest" href="/assets/files/site.webmanifest">
8<link rel="mask-icon" href="/assets/images/safari-pinned-tab.svg" color="#000">
9<meta name="msapplication-TileColor" content="#ffffff">
10<meta name="theme-color" content="#ffffff">
11
12<meta name="og:site_name" content="Corey Prophitt's Website">
13<meta name="og:url" content="https://prophitt.me/">
14<meta name="og:type" content="website">
15{{ end }}