~evanj/evanjon.es

ref: 420d2707f2d423977b12534af72c1c90c8dddbe2 evanjon.es/evanjon.es.go -rw-r--r-- 3.1 KiB
420d2707Evan M Jones Feat(prev/next): Adding previous and next post links. 1 year, 6 months ago
                                                                                
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"strconv"

	"git.sr.ht/~evanj/evanjon.es/internal/c"
	"git.sr.ht/~evanj/evanjon.es/internal/c/home"
	"git.sr.ht/~evanj/evanjon.es/internal/c/list"
	"git.sr.ht/~evanj/evanjon.es/internal/c/rss"
	"git.sr.ht/~evanj/evanjon.es/internal/c/search"
	"git.sr.ht/~evanj/evanjon.es/internal/s/cms"
	rssencoder "git.sr.ht/~evanj/evanjon.es/internal/s/rss"
	api "git.sr.ht/~evanj/evanjon.es/pkg/cms"
)

//go:generate go get git.sr.ht/~evanj/embed

type App struct {
	log          *log.Logger
	baseRouter   http.Handler
	homeRouter   http.Handler
	searchRouter http.Handler
	listRouter   http.Handler
	// staticRouter http.Handler
	cacheRouter http.Handler
	rssRouter   http.Handler
}

func New(out io.Writer) *App {
	l := log.New(out, "[evanjon.es] ", 0)
	e := c.New(log.New(out, "[evanjon.es:baseRouter] ", 0))

	typePage, err1 := strconv.Atoi(os.Getenv("CMS_TYPE_PAGE"))
	typePost, err2 := strconv.Atoi(os.Getenv("CMS_TYPE_POST"))
	if err1 != nil || err2 != nil {
		l.Fatal("contenttype not set correctly")
	}

	space, err := strconv.Atoi(os.Getenv("CMS_SPACE"))
	if err != nil {
		l.Fatal("invalid space id")
	}

	// Transformer, match our data types.
	cms := cms.New(
		log.New(out, "[evanjon.es:cms] ", 0),
		// API impl.
		api.New(
			os.Getenv("CMS_USER"),
			os.Getenv("CMS_PASS"),
			os.Getenv("CMS_URL"),
			space,
		),
		os.Getenv("CACHE_SECRET"),
	)

	return &App{
		log:        l,
		baseRouter: e,

		homeRouter: home.New(
			e,
			log.New(out, "[evanjon.es:home] ", 0),
			cms,
			typePage,
			typePost,
		),

		searchRouter: search.New(
			e,
			log.New(out, "[evanjon.es:search] ", 0),
			cms,
			typePage,
			typePost,
		),

		listRouter: list.New(
			e,
			log.New(out, "[evanjon.es:list] ", 0),
			cms,
			typePage,
			typePost,
		),

		// staticRouter: http.FileServer(http.Dir(".")),
		cacheRouter: cms,
		rssRouter: rss.New(
			e,
			log.New(out, "[evanjon.es:rss] ", 0),
			cms,
			rssencoder.New(
				"evanjon.es",
				"My personal corner of th WWW.",
				"https://evanjon.es/",
				"Evan Jones",
				"me@evanjon.es",
			),
			typePage,
			typePost,
		),
	}
}

func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// // Check for static files first.
	// p := strings.Split(r.URL.Path, "/")
	// if len(p) > 1 && p[1] == "static" {
	// 	w.Header().Add("Content-Type", "text/plain")
	// 	app.staticRouter.ServeHTTP(w, r)
	// 	return
	// }

	// Regular routers.
	switch r.URL.Path {
	case "/cache/break":
		app.cacheRouter.ServeHTTP(w, r)
		break
	case "/favicon.ico":
		http.Redirect(w, r, "https://e3.evanjon.es/29d0f596-0ff0-4adf-61d6-9a3934532e77.ico", http.StatusTemporaryRedirect)
		break
	case "/ping":
		app.baseRouter.ServeHTTP(w, r)
		break
	case "/rss":
		app.rssRouter.ServeHTTP(w, r)
		break
	case "/blog":
		fallthrough
	case "/posts":
		app.listRouter.ServeHTTP(w, r)
		break
	case "/":
		app.homeRouter.ServeHTTP(w, r)
		break
	default:
		app.searchRouter.ServeHTTP(w, r)
		break
	}
}

func main() {
	p := fmt.Sprintf(":%s", os.Getenv("PORT"))
	app := New(os.Stdout)
	app.log.Println("listening on port", p)
	log.Fatal(http.ListenAndServe(p, app))
}