aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCorey Prophitt <git@prophitt.me>2022-07-26 15:37:45 -0700
committerCorey Prophitt <git@prophitt.me>2022-07-26 15:37:45 -0700
commitdb11ce7fd1020abb1f54443d563a8d328cad3dd9 (patch)
treef540bbd0975860a5d107c88e35ad9ddbac0c8049
parentf6a4fb644e444fd0a566a89e9096668705dc95a3 (diff)
downloadprophitt.me-db11ce7fd1020abb1f54443d563a8d328cad3dd9.tar.gz
prophitt.me-db11ce7fd1020abb1f54443d563a8d328cad3dd9.zip
Added catch all handling of special assets
-rw-r--r--assets/files/robots.txt13
-rw-r--r--assets/files/sitemap.xml17
-rw-r--r--assets/images/favicon.icobin0 -> 7406 bytes
-rw-r--r--config.go24
-rw-r--r--internal/web/index.go58
5 files changed, 111 insertions, 1 deletions
diff --git a/assets/files/robots.txt b/assets/files/robots.txt
new file mode 100644
index 0000000..cf9ec8e
--- /dev/null
+++ b/assets/files/robots.txt
@@ -0,0 +1,13 @@
1User-agent: *
2Allow: /
3
4User-agent: SemrushBot
5Disallow: /
6
7User-agent: SemrushBot-SA
8Disallow: /
9
10User-agent: AdsTxtCrawler
11Disallow: /
12
13Sitemap: https://prophitt.me/sitemap.xml
diff --git a/assets/files/sitemap.xml b/assets/files/sitemap.xml
new file mode 100644
index 0000000..0e5f593
--- /dev/null
+++ b/assets/files/sitemap.xml
@@ -0,0 +1,17 @@
1<?xml version="1.0" encoding="UTF-8"?>
2<urlset
3 xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
6 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
7<url>
8 <loc>https://prophitt.me/</loc>
9 <lastmod>2022-22-07T03:35:22+00:00</lastmod>
10 <priority>1.00</priority>
11</url>
12<url>
13 <loc>https://git.prophitt.me</loc>
14 <lastmod>2022-07-22T03:35:22+00:00</lastmod>
15 <priority>0.80</priority>
16</url>
17</urlset>
diff --git a/assets/images/favicon.ico b/assets/images/favicon.ico
new file mode 100644
index 0000000..dddd50d
--- /dev/null
+++ b/assets/images/favicon.ico
Binary files differ
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..6dcf3d3
--- /dev/null
+++ b/config.go
@@ -0,0 +1,24 @@
1package prophitt
2
3import (
4 "embed"
5 "os"
6)
7
8// VFS is our embedded file system.
9//go:embed assets/**/*
10var VFS embed.FS
11
12// IsDevelopment returns true if our environment is development.
13func IsDevelopment() bool {
14 return os.Getenv("ENV") == "development"
15}
16
17// BaseURL returns the fully qualifier URL to our service.
18func BaseURL() string {
19 if IsDevelopment() {
20 return "http://" + os.Getenv("DOMAIN") + ":" + os.Getenv("PORT")
21 }
22
23 return "https://prophitt.me"
24}
diff --git a/internal/web/index.go b/internal/web/index.go
index 62124cd..1880b8d 100644
--- a/internal/web/index.go
+++ b/internal/web/index.go
@@ -2,9 +2,53 @@ package web
2 2
3import ( 3import (
4 "fmt" 4 "fmt"
5 "io/fs"
6 "log"
5 "net/http" 7 "net/http"
8
9 "prophitt.me"
6) 10)
7 11
12/*
13 * Special Assets
14 */
15
16type specialFilePath struct {
17 IncomingPath string
18 ActualPath string
19 Sub string
20}
21
22// Special files are not accessed through /assets and are handled through the root
23// path. These are mainly to support browsers that default to some location for an
24// asset.
25var specialFilePaths []specialFilePath = []specialFilePath{
26 {IncomingPath: "/favicon.ico", Sub: "assets/images"},
27 {IncomingPath: "/robots.txt", Sub: "assets/files"},
28 {IncomingPath: "/sitemap.xml", Sub: "assets/files"},
29}
30
31// Handle special files like robots.txt, sitemap.xml, etc. These are accessed
32// via the root and not the assets directory itself.
33func handleSpecialAssets(w http.ResponseWriter, r *http.Request) bool {
34 if r.URL.Path != "/" {
35 for _, file := range specialFilePaths {
36 if r.URL.Path == file.IncomingPath {
37 filesystem, err := fs.Sub(prophitt.VFS, file.Sub)
38
39 if err == nil {
40 http.FileServer(http.FS(filesystem)).ServeHTTP(w, r)
41 return true
42 }
43
44 log.Printf("web: failed to sub the FS path; %s\n", err)
45 }
46 }
47 }
48
49 return false
50}
51
8func init() { 52func init() {
9 // The index page handler. This is a bit special because it handles the index 53 // The index page handler. This is a bit special because it handles the index
10 // page ("/") and any pages that don't match a registered route (serves as the 54 // page ("/") and any pages that don't match a registered route (serves as the
@@ -12,7 +56,19 @@ func init() {
12 routes.register(route{ 56 routes.register(route{
13 Path: "/", 57 Path: "/",
14 Handler: logger(func(w http.ResponseWriter, r *http.Request) { 58 Handler: logger(func(w http.ResponseWriter, r *http.Request) {
15 fmt.Fprintf(w, "\tHello World!\n") 59 if r.URL.Path != "/" {
60 // NOTE: This is the catch all code path.
61
62 if handleSpecialAssets(w, r) {
63 return
64 }
65
66 /* TODO: We could render a "not found" page instead of an error response. */
67 http.NotFound(w, r)
68 } else {
69 // NOTE: This is the actual landing page page ("/").
70 fmt.Fprintf(w, "\tHello World!\n")
71 }
16 }), 72 }),
17 }) 73 })
18} 74}