2023-06-17 21:25:46 +08:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2023-10-09 23:10:41 +08:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-06-17 21:25:46 +08:00
|
|
|
"github.com/labstack/echo/v4"
|
2023-10-09 23:10:41 +08:00
|
|
|
"github.com/labstack/echo/v4/middleware"
|
2023-09-17 22:55:13 +08:00
|
|
|
|
2023-09-13 21:36:43 +08:00
|
|
|
"github.com/usememos/memos/plugin/telegram"
|
2023-06-17 21:25:46 +08:00
|
|
|
"github.com/usememos/memos/server/profile"
|
2024-02-29 01:16:43 +08:00
|
|
|
"github.com/usememos/memos/server/route/resource"
|
|
|
|
"github.com/usememos/memos/server/route/rss"
|
2023-06-17 21:25:46 +08:00
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
type APIV1Service struct {
|
2023-09-13 21:36:43 +08:00
|
|
|
Secret string
|
|
|
|
Profile *profile.Profile
|
|
|
|
Store *store.Store
|
|
|
|
telegramBot *telegram.Bot
|
2023-06-17 21:25:46 +08:00
|
|
|
}
|
|
|
|
|
2023-08-26 08:07:43 +08:00
|
|
|
// @title memos API
|
|
|
|
// @version 1.0
|
|
|
|
// @description A privacy-first, lightweight note-taking service.
|
|
|
|
//
|
|
|
|
// @contact.name API Support
|
|
|
|
// @contact.url https://github.com/orgs/usememos/discussions
|
|
|
|
//
|
|
|
|
// @license.name MIT License
|
|
|
|
// @license.url https://github.com/usememos/memos/blob/main/LICENSE
|
|
|
|
//
|
|
|
|
// @BasePath /
|
|
|
|
//
|
|
|
|
// @externalDocs.url https://usememos.com/
|
2023-09-14 22:57:27 +08:00
|
|
|
// @externalDocs.description Find out more about Memos.
|
2023-09-13 21:36:43 +08:00
|
|
|
func NewAPIV1Service(secret string, profile *profile.Profile, store *store.Store, telegramBot *telegram.Bot) *APIV1Service {
|
2023-06-17 21:25:46 +08:00
|
|
|
return &APIV1Service{
|
2023-09-13 21:36:43 +08:00
|
|
|
Secret: secret,
|
|
|
|
Profile: profile,
|
|
|
|
Store: store,
|
|
|
|
telegramBot: telegramBot,
|
2023-06-17 21:25:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-01 00:03:28 +08:00
|
|
|
func (s *APIV1Service) Register(rootGroup *echo.Group) {
|
2023-07-06 22:53:38 +08:00
|
|
|
// Register API v1 routes.
|
2023-07-01 00:03:28 +08:00
|
|
|
apiV1Group := rootGroup.Group("/api/v1")
|
2023-10-09 23:10:41 +08:00
|
|
|
apiV1Group.Use(middleware.RateLimiterWithConfig(middleware.RateLimiterConfig{
|
|
|
|
Store: middleware.NewRateLimiterMemoryStoreWithConfig(
|
|
|
|
middleware.RateLimiterMemoryStoreConfig{Rate: 30, Burst: 100, ExpiresIn: 3 * time.Minute},
|
|
|
|
),
|
|
|
|
IdentifierExtractor: func(ctx echo.Context) (string, error) {
|
|
|
|
id := ctx.RealIP()
|
|
|
|
return id, nil
|
|
|
|
},
|
|
|
|
ErrorHandler: func(context echo.Context, err error) error {
|
|
|
|
return context.JSON(http.StatusForbidden, nil)
|
|
|
|
},
|
|
|
|
DenyHandler: func(context echo.Context, identifier string, err error) error {
|
|
|
|
return context.JSON(http.StatusTooManyRequests, nil)
|
|
|
|
},
|
|
|
|
}))
|
2023-07-01 00:03:28 +08:00
|
|
|
apiV1Group.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return JWTMiddleware(s, next, s.Secret)
|
|
|
|
})
|
2023-07-02 18:56:25 +08:00
|
|
|
s.registerSystemRoutes(apiV1Group)
|
|
|
|
s.registerSystemSettingRoutes(apiV1Group)
|
2023-07-01 00:03:28 +08:00
|
|
|
s.registerAuthRoutes(apiV1Group)
|
2023-07-02 18:56:25 +08:00
|
|
|
s.registerUserRoutes(apiV1Group)
|
|
|
|
s.registerTagRoutes(apiV1Group)
|
2023-07-04 10:05:57 +08:00
|
|
|
s.registerStorageRoutes(apiV1Group)
|
2023-07-06 00:01:40 +08:00
|
|
|
s.registerResourceRoutes(apiV1Group)
|
2023-07-06 21:56:42 +08:00
|
|
|
s.registerMemoRoutes(apiV1Group)
|
|
|
|
s.registerMemoOrganizerRoutes(apiV1Group)
|
|
|
|
s.registerMemoRelationRoutes(apiV1Group)
|
2023-07-06 00:01:40 +08:00
|
|
|
|
2023-07-06 22:53:38 +08:00
|
|
|
// Register public routes.
|
2023-07-06 00:01:40 +08:00
|
|
|
publicGroup := rootGroup.Group("/o")
|
|
|
|
publicGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return JWTMiddleware(s, next, s.Secret)
|
|
|
|
})
|
2023-07-06 21:56:42 +08:00
|
|
|
s.registerGetterPublicRoutes(publicGroup)
|
2024-01-29 21:04:35 +08:00
|
|
|
|
2023-10-08 19:40:30 +08:00
|
|
|
// Create and register resource public routes.
|
2024-01-29 21:04:35 +08:00
|
|
|
resource.NewResourceService(s.Profile, s.Store).RegisterRoutes(publicGroup)
|
|
|
|
|
|
|
|
// Create and register rss public routes.
|
|
|
|
rss.NewRSSService(s.Profile, s.Store).RegisterRoutes(rootGroup)
|
2023-08-26 08:07:43 +08:00
|
|
|
|
|
|
|
// programmatically set API version same as the server version
|
|
|
|
SwaggerInfo.Version = s.Profile.Version
|
2023-06-17 21:25:46 +08:00
|
|
|
}
|