This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
handler.go
147 lines (121 loc) · 3.34 KB
/
handler.go
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
package gossip
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/patrickmn/go-cache"
"github.com/parkr/gossip/database"
)
type Handler struct {
DB *database.DB
Cache *cache.Cache
allRooms []string
skippedAuthors []string
defaultRoom string
}
func NewHandler() *Handler {
return &Handler{
DB: database.New(),
// Create a cache with a default expiration time of 5 minutes, and which
// purges expired items every 10 minutes
Cache: cache.New(5*time.Minute, 10*time.Minute),
}
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/_health" {
h.HealthCheck(w, r)
return
}
if r.URL.Path == "/" {
h.Index(w, r)
return
}
if strings.HasPrefix(r.URL.Path, "/api/") {
h.API(w, r)
return
}
if r.URL.Path == "/search" {
h.Search(w, r)
return
}
if strings.HasPrefix(r.URL.Path, "/room/") {
h.LatestMessagesByRoom(w, r)
return
}
if strings.HasPrefix(r.URL.Path, "/messages/by/") {
h.LatestMessagesByAuthor(w, r)
return
}
if strings.HasPrefix(r.URL.Path, "/messages/") && strings.HasSuffix(r.URL.Path, "/context") {
h.MessageContext(w, r)
return
}
http.Error(w, fmt.Sprintf("404 Not Found: %s", r.URL.Path), http.StatusNotFound)
}
func (h *Handler) AllRooms() []string {
if h.allRooms == nil {
if rooms := os.Getenv("GOSSIP_ROOMS"); rooms != "" {
// pull rooms in from the env
h.allRooms = strings.Split(rooms, ",")
} else {
// pull rooms in from the DB
var err error
h.allRooms, err = h.DB.AllRooms()
if err != nil {
log.Printf("error fetching rooms: %+v", err)
}
}
}
return h.allRooms
}
func (h *Handler) SkippedAuthors() []string {
if h.skippedAuthors == nil {
h.skippedAuthors = strings.Split(os.Getenv("GOSSIP_SKIPPED_AUTHORS"), ",")
}
return h.skippedAuthors
}
func (h *Handler) DefaultRoom() string {
if h.defaultRoom == "" {
h.defaultRoom = os.Getenv("GOSSIP_DEFAULT_ROOM") // this should NOT have a hash/pound symbol
}
return h.defaultRoom
}
type messagesListFunc func() ([]database.Message, error)
func (h *Handler) FetchAndCacheList(r *http.Request, key string, f messagesListFunc) ([]database.Message, error) {
var messages []database.Message
var err error
messagesInterface, found := h.Cache.Get(key)
if found {
LogWithRequestID(r, fmt.Sprintf("Pulling %s results from cache", key))
messages = messagesInterface.([]database.Message)
} else {
LogWithRequestID(r, fmt.Sprintf("Pulling %s results from database, stuffing in cache", key))
messages, err = f()
if err != nil {
return messages, err // don't set the cache if there's an error
}
h.Cache.Set(key, messages, cache.DefaultExpiration)
}
return messages, err
}
type messagesGetFunc func() (*database.Message, error)
func (h *Handler) FetchAndCacheGet(r *http.Request, key string, f messagesGetFunc) (*database.Message, error) {
var message *database.Message
var err error
messageInterface, found := h.Cache.Get(key)
if found {
LogWithRequestID(r, fmt.Sprintf("Pulling %s result from cache", key))
message = messageInterface.(*database.Message)
} else {
LogWithRequestID(r, fmt.Sprintf("Pulling %s result from database, stuffing in cache", key))
message, err = f()
if err != nil {
return message, err // don't set the cache if there's an error
}
h.Cache.Set(key, message, cache.DefaultExpiration)
}
return message, err
}