-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrouters.go
More file actions
164 lines (126 loc) · 4.58 KB
/
routers.go
File metadata and controls
164 lines (126 loc) · 4.58 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/gofrs/uuid/v5"
"github.com/rs/zerolog/log"
"app/csrf"
)
const oauthURL = "https://next.bgm.tv/oauth/authorize"
func routers(h *handler) *chi.Mux {
mux := chi.NewRouter()
mux.Use(middleware.Recoverer)
r := mux.With(SessionMiddleware(h), csrf.New())
r.Get("/login", h.loginView)
r.Get("/callback", handleError(h.callback))
r.Get("/badge.svg", h.badge)
r.Get("/", handleError(h.indexView))
r.Get("/s/{patchID}", handleError(h.subjectPatchShortLink))
r.Get("/e/{patchID}", handleError(h.episodePatchShortLink))
r.Get("/subject/{patchID}", handleError(h.subjectPatchDetailView))
r.Get("/episode/{patchID}", handleError(h.episodePatchDetailView))
r.Get("/contrib/{user-id}", handleError(h.userContributionView))
r.Get("/review/{user-id}", handleError(h.userReviewView))
r.Post("/api/review/{patch-type}/{patch-id}", handleError(h.handleReview))
// subjects
r.Get("/suggest", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, fmt.Sprintf("/edit/subject/%s", r.URL.Query().Get("subject_id")), http.StatusSeeOther)
})
r.Get("/edit/subject/{subject-id}", handleError(h.editSubjectView))
r.Post("/edit/subject/{subject-id}", handleError(h.createSubjectEditPatch))
// json API to create patch from partial subjects
r.Patch("/edit/subject/{subject-id}", handleError(h.createSubjectEditPatchAPI))
r.Get("/edit/patch/subject/{patch-id}", handleError(h.editSubjectPatchView))
r.Post("/edit/patch/subject/{patch-id}", handleError(h.updateSubjectEditPatch))
r.Post("/api/delete/patch/subject/{patch-id}", handleError(h.deleteSubjectPatch))
// episodes
r.Get("/suggest-episode", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, fmt.Sprintf("/edit/episode/%s", r.URL.Query().Get("episode_id")), http.StatusSeeOther)
})
r.Get("/edit/episode/{episode-id}", handleError(h.editEpisodeView))
r.Post("/edit/episode/{episode-id}", handleError(h.createEpisodeEditPatch))
r.Get("/edit/patch/episode/{patch-id}", handleError(h.editEpisodePatchView))
r.Post("/edit/patch/episode/{patch-id}", handleError(h.updateEpisodeEditPatch))
// json API to create patch from partial episode
r.Patch("/edit/episode/{episode-id}", handleError(h.createEpisodeEditPatchAPI))
r.Post("/api/delete/patch/episode/{patch-id}", handleError(h.deleteEpisodePatch))
// other json APIS
r.Get("/api/subject/pending", handleError(func(w http.ResponseWriter, r *http.Request) error {
rows, err := h.q.ListPendingSubjectPatches(r.Context())
if err != nil {
return err
}
type Res struct {
ID uuid.UUID `json:"id"`
SubjectID int32 `json:"subject_id"`
FromUser int32 `json:"from_user"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
var res = make([]Res, 0, len(rows))
for _, row := range rows {
res = append(res, Res{
ID: row.ID,
SubjectID: row.SubjectID,
FromUser: row.FromUserID,
CreatedAt: row.CreatedAt.Time.Unix(),
UpdatedAt: row.UpdatedAt.Time.Unix(),
})
}
w.Header().Set("content-type", contentTypeApplicationJSON)
w.WriteHeader(http.StatusOK)
return json.NewEncoder(w).Encode(map[string]any{
"data": res,
})
}))
r.Get("/api/episode/pending", handleError(func(w http.ResponseWriter, r *http.Request) error {
rows, err := h.q.ListPendingEpisodePatches(r.Context())
if err != nil {
return err
}
type Res struct {
ID uuid.UUID `json:"id"`
EpisodeID int32 `json:"episode_id"`
FromUser int32 `json:"from_user"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
var res = make([]Res, 0, len(rows))
for _, row := range rows {
res = append(res, Res{
ID: row.ID,
EpisodeID: row.EpisodeID,
FromUser: row.FromUserID,
CreatedAt: row.CreatedAt.Time.Unix(),
UpdatedAt: row.UpdatedAt.Time.Unix(),
})
}
w.Header().Set("content-type", contentTypeApplicationJSON)
w.WriteHeader(http.StatusOK)
return json.NewEncoder(w).Encode(map[string]any{
"data": res,
})
}))
return mux
}
func handleError(fn func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := fn(w, r)
if err != nil {
if errors.Is(err, ErrLoginRequired) {
return
}
var he *HttpError
if errors.As(err, &he) {
http.Error(w, he.Message, he.StatusCode)
return
}
log.Error().Err(err).Msg("error")
http.Error(w, "unexpected error", http.StatusInternalServerError)
}
}
}