~evanj/evanjon.es

ref: 5d167d6f2878c36e4d862072e2ee821e8c1e122e evanjon.es/evanjon.es.go -rw-r--r-- 2.7 KiB
5d167d6fEvan J Fix(main.css): Revert last <br> changes in last commit. Modify max-width 1 year, 8 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
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/list"
	"git.sr.ht/~evanj/evanjon.es/internal/c/search"

	"git.sr.ht/~evanj/evanjon.es/internal/s/cms"
	api "git.sr.ht/~evanj/evanjon.es/pkg/cms"

	"git.sr.ht/~evanj/evanjon.es/internal/c/rss"
	rssencoder "git.sr.ht/~evanj/evanjon.es/internal/s/rss"

	_ "git.sr.ht/~evanj/embed"
)

//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
	cacheRouter  http.Handler
	rssRouter    http.Handler
}

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

	typePage, err1 := strconv.Atoi(os.Getenv("CMS_TYPE_PAGE"))
	typePost, err2 := strconv.Atoi(os.Getenv("CMS_TYPE_POST"))
	typeMeta, err3 := strconv.Atoi(os.Getenv("CMS_TYPE_META"))
	if err1 != nil || err2 != nil || err3 != 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.New(
			os.Getenv("CMS_USER"),
			os.Getenv("CMS_PASS"),
			os.Getenv("CMS_URL"),
			space,
		),
		os.Getenv("CACHE_SECRET"),
	)

	e := c.New(log.New(out, "[evanjon.es:baseRouter] ", 0), cms)

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

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

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

		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) {
	// 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
	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))
}