-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (121 loc) · 3.61 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"net/http"
"os"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/icco/gutil/logging"
"github.com/icco/tab-archive/lib"
"go.uber.org/zap"
_ "github.com/lib/pq"
)
var (
log = logging.Must(logging.NewLogger("tabs"))
gcpproject = "icco-cloud"
)
type pageData struct {
TabCount int64
UserCount int64
}
func main() {
port := "8080"
if fromEnv := os.Getenv("PORT"); fromEnv != "" {
port = fromEnv
}
log.Infow("Starting up", "host", fmt.Sprintf("http://localhost:%s", port))
db, err := lib.InitDB(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatalw("cannot connect to database", zap.Error(err))
}
defer db.Close()
r := chi.NewRouter()
r.Use(middleware.RealIP)
r.Use(logging.Middleware(log.Desugar(), gcpproject))
crs := cors.New(cors.Options{
AllowCredentials: true,
OptionsPassthrough: false,
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"},
ExposedHeaders: []string{"Link"},
MaxAge: 300, // Maximum value not ignored by any of major browsers
})
r.Use(crs.Handler)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
tc, err := lib.TabCount(ctx, db)
if err != nil {
log.Errorw("tab count", zap.Error(err))
jsError(w, err, http.StatusInternalServerError)
return
}
uc, err := lib.UserCount(ctx, db)
if err != nil {
log.Errorw("user count", zap.Error(err))
jsError(w, err, http.StatusInternalServerError)
return
}
tmpl := template.Must(template.ParseFiles("index.tmpl"))
if err := tmpl.Execute(w, &pageData{TabCount: tc, UserCount: uc}); err != nil {
log.Errorw("template execution", zap.Error(err))
}
})
r.Get("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hi."))
})
r.With(AuthMiddleware(db)).Post("/hook", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
u := lib.UserFromContext(ctx)
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Errorw("could not read buffer", zap.Error(err))
jsError(w, err, http.StatusInternalServerError)
return
}
ct := r.Header.Get("content-type")
if ct != "application/json" {
err := fmt.Errorf("expected 'application/json' content type, got %q", ct)
log.Errorw("bad content type", zap.Error(err))
jsError(w, err, http.StatusBadRequest)
return
}
if err := lib.ParseAndStore(ctx, db, u, buf); err != nil {
log.Errorw("could not parse request", zap.Error(err))
jsError(w, err, http.StatusInternalServerError)
return
}
w.Write([]byte(`{"status": "success"}`))
})
r.With(AuthMiddleware(db)).Get("/archive", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
u := lib.UserFromContext(ctx)
j := json.NewEncoder(w)
tabs, err := u.GetArchive(ctx, db)
if err != nil {
log.Errorw("could not get user tabs", zap.Error(err))
jsError(w, err, http.StatusInternalServerError)
return
}
if err := j.Encode(map[string]interface{}{
"status": "success",
"tabs": tabs,
}); err != nil {
log.Errorw("could not marshal data", zap.Error(err))
jsError(w, err, http.StatusInternalServerError)
return
}
})
log.Fatal(http.ListenAndServe(":"+port, r))
}
func jsError(w http.ResponseWriter, err error, statusCode int) {
w.WriteHeader(statusCode)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
enc := json.NewEncoder(w)
enc.Encode(map[string]string{"error": err.Error()})
}