-
Notifications
You must be signed in to change notification settings - Fork 62
/
handler.go
170 lines (146 loc) · 7.07 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package web
import (
"fmt"
"net/http"
"github.com/gofiber/fiber/v2"
"github.com/bangumi/server/internal/config"
"github.com/bangumi/server/internal/web/handler"
"github.com/bangumi/server/internal/web/handler/character"
"github.com/bangumi/server/internal/web/handler/index"
"github.com/bangumi/server/internal/web/handler/notification"
"github.com/bangumi/server/internal/web/handler/person"
"github.com/bangumi/server/internal/web/handler/pm"
"github.com/bangumi/server/internal/web/handler/subject"
"github.com/bangumi/server/internal/web/handler/user"
"github.com/bangumi/server/internal/web/middleware/origin"
"github.com/bangumi/server/internal/web/middleware/referer"
"github.com/bangumi/server/internal/web/middleware/ua"
"github.com/bangumi/server/internal/web/req"
"github.com/bangumi/server/internal/web/res"
"github.com/bangumi/server/internal/web/util"
)
// AddRouters add all router and default 404 Handler to app.
//
//nolint:funlen
func AddRouters(
app *fiber.App,
c config.AppConfig,
h handler.Handler,
userHandler user.User,
personHandler person.Person,
characterHandler character.Character,
pmHandler pm.PrivateMessage,
notificationHandler notification.Notification,
subjectHandler subject.Subject,
indexHandler index.Handler,
) {
app.Get("/", indexPage())
app.Use(ua.DisableDefaultHTTPLibrary)
v0 := app.Group("/v0/", h.MiddlewareAccessTokenAuth)
v0.Post("/search/subjects", h.Search)
v0.Get("/subjects/:id", subjectHandler.Get)
v0.Get("/subjects/:id/image", subjectHandler.GetImage)
v0.Get("/subjects/:id/persons", subjectHandler.GetRelatedPersons)
v0.Get("/subjects/:id/subjects", subjectHandler.GetRelatedSubjects)
v0.Get("/subjects/:id/characters", subjectHandler.GetRelatedCharacters)
v0.Get("/persons/:id", personHandler.Get)
v0.Get("/persons/:id/image", personHandler.GetImage)
v0.Get("/persons/:id/subjects", personHandler.GetRelatedSubjects)
v0.Get("/persons/:id/characters", personHandler.GetRelatedCharacters)
v0.Get("/characters/:id", characterHandler.Get)
v0.Get("/characters/:id/image", characterHandler.GetImage)
v0.Get("/characters/:id/subjects", characterHandler.GetRelatedSubjects)
v0.Get("/characters/:id/persons", characterHandler.GetRelatedPersons)
v0.Get("/episodes/:id", h.GetEpisode)
v0.Get("/episodes", h.ListEpisode)
v0.Get("/me", userHandler.GetCurrent)
v0.Get("/users/:username", userHandler.Get)
v0.Get("/users/:username/collections", userHandler.ListSubjectCollection)
v0.Get("/users/:username/collections/:subject_id", userHandler.GetSubjectCollection)
v0.Get("/users/-/collections/-/episodes/:episode_id", h.NeedLogin, userHandler.GetEpisodeCollection)
v0.Put("/users/-/collections/-/episodes/:episode_id", req.JSON, h.NeedLogin, userHandler.PutEpisodeCollection)
v0.Get("/users/-/collections/:subject_id/episodes", h.NeedLogin, userHandler.GetSubjectEpisodeCollection)
v0.Patch("/users/-/collections/:subject_id", req.JSON, h.NeedLogin, userHandler.PatchSubjectCollection)
v0.Patch("/users/-/collections/:subject_id/episodes",
req.JSON, h.NeedLogin, userHandler.PatchEpisodeCollectionBatch,
)
v0.Get("/users/:username/avatar", userHandler.GetAvatar)
{
i := indexHandler
v0.Get("/indices/:id", i.GetIndex)
v0.Get("/indices/:id/subjects", i.GetIndexSubjects)
// indices
v0.Post("/indices", req.JSON, i.NeedLogin, i.NewIndex)
v0.Put("/indices/:id", req.JSON, i.NeedLogin, i.UpdateIndex)
// indices subjects
v0.Post("/indices/:id/subjects", req.JSON, i.NeedLogin, i.AddIndexSubject)
v0.Put("/indices/:id/subjects/:subject_id", req.JSON, i.NeedLogin, i.UpdateIndexSubject)
v0.Delete("/indices/:id/subjects/:subject_id", i.NeedLogin, i.RemoveIndexSubject)
}
v0.Get("/revisions/persons/:id", h.GetPersonRevision)
v0.Get("/revisions/persons", h.ListPersonRevision)
v0.Get("/revisions/subjects/:id", h.GetSubjectRevision)
v0.Get("/revisions/subjects", h.ListSubjectRevision)
v0.Get("/revisions/characters/:id", h.GetCharacterRevision)
v0.Get("/revisions/characters", h.ListCharacterRevision)
var originMiddleware = origin.New(fmt.Sprintf("https://%s", c.WebDomain))
var refererMiddleware = referer.New(fmt.Sprintf("https://%s/", c.WebDomain))
var CORSBlockMiddleware []fiber.Handler
if c.WebDomain != "" {
CORSBlockMiddleware = []fiber.Handler{originMiddleware, refererMiddleware}
}
// frontend private api
private := app.Group("/p/", append(CORSBlockMiddleware, h.MiddlewareSessionAuth)...)
private.Post("/login", req.JSON, h.PrivateLogin)
private.Post("/logout", h.PrivateLogout)
private.Get("/me", userHandler.GetCurrent)
private.Get("/groups/:name", h.GetGroupProfileByNamePrivate)
private.Get("/groups/:name/members", h.ListGroupMembersPrivate)
private.Get("/groups/:name/topics", h.ListGroupTopics)
private.Get("/subjects/:id/topics", h.ListSubjectTopics)
private.Get("/groups/-/topics/:topic_id", h.GetGroupTopic)
private.Get("/subjects/:id/topics/:topic_id", h.GetSubjectTopic)
private.Get("/indices/:id/comments", h.GetIndexComments)
private.Get("/episodes/:id/comments", h.GetEpisodeComments)
private.Get("/characters/:id/comments", h.GetCharacterComments)
private.Get("/persons/:id/comments", h.GetPersonComments)
private.Get("/pms/list", h.NeedLogin, pmHandler.List)
private.Get("/pms/related-msgs/:id", h.NeedLogin, pmHandler.ListRelated)
private.Get("/pms/counts", h.NeedLogin, pmHandler.CountTypes)
private.Get("/pms/contacts/recent", h.NeedLogin, pmHandler.ListRecentContact)
private.Patch("/pms/read", req.JSON, h.NeedLogin, pmHandler.MarkRead)
private.Post("/pms", req.JSON, h.NeedLogin, pmHandler.Create)
private.Delete("/pms", req.JSON, h.NeedLogin, pmHandler.Delete)
private.Get("/notifications/count", h.NeedLogin, notificationHandler.Count)
// un-documented
private.Post("/access-tokens", req.JSON, h.CreatePersonalAccessToken)
private.Delete("/access-tokens", req.JSON, h.DeletePersonalAccessToken)
if c.WebDomain != "" {
CORSBlockMiddleware = []fiber.Handler{originMiddleware}
}
privateHTML := app.Group("/demo/", append(CORSBlockMiddleware, h.MiddlewareSessionAuth)...)
privateHTML.Get("/login", h.PageLogin)
privateHTML.Get("/access-token", h.PageListAccessToken)
privateHTML.Get("/access-token/create", h.PageCreateAccessToken)
// default 404 Handler, all router should be added before this router
app.Use(func(c *fiber.Ctx) error {
return res.JSON(c.Status(http.StatusNotFound), res.Error{
Title: "Not Found",
Description: "This is default response, if you see this response, please check your request",
Details: util.Detail(c),
})
})
}