2022-02-03 10:32:03 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-01-02 18:18:12 +03:00
|
|
|
"context"
|
2023-02-03 05:30:18 +03:00
|
|
|
"database/sql"
|
2023-01-02 18:18:12 +03:00
|
|
|
"encoding/json"
|
2022-02-03 10:32:03 +03:00
|
|
|
"fmt"
|
2022-02-04 13:54:24 +03:00
|
|
|
"time"
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2023-01-02 18:18:12 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/usememos/memos/api"
|
2023-05-26 04:43:51 +03:00
|
|
|
"github.com/usememos/memos/plugin/telegram"
|
2022-06-27 17:09:06 +03:00
|
|
|
"github.com/usememos/memos/server/profile"
|
|
|
|
"github.com/usememos/memos/store"
|
2023-01-03 18:05:42 +03:00
|
|
|
"github.com/usememos/memos/store/db"
|
2022-06-27 17:09:06 +03:00
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
2023-02-03 05:30:18 +03:00
|
|
|
e *echo.Echo
|
|
|
|
db *sql.DB
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2023-03-05 16:42:32 +03:00
|
|
|
ID string
|
|
|
|
Profile *profile.Profile
|
|
|
|
Store *store.Store
|
2023-05-26 04:43:51 +03:00
|
|
|
|
2023-05-29 14:49:05 +03:00
|
|
|
telegramBot *telegram.Bot
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2023-01-03 18:05:42 +03:00
|
|
|
func NewServer(ctx context.Context, profile *profile.Profile) (*Server, error) {
|
2022-02-03 10:32:03 +03:00
|
|
|
e := echo.New()
|
|
|
|
e.Debug = true
|
|
|
|
e.HideBanner = true
|
2022-05-20 17:48:36 +03:00
|
|
|
e.HidePort = true
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2023-01-03 18:05:42 +03:00
|
|
|
db := db.NewDB(profile)
|
|
|
|
if err := db.Open(ctx); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannot open db")
|
|
|
|
}
|
|
|
|
|
2023-02-03 05:30:18 +03:00
|
|
|
s := &Server{
|
|
|
|
e: e,
|
|
|
|
db: db.DBInstance,
|
|
|
|
Profile: profile,
|
|
|
|
}
|
2023-01-03 18:05:42 +03:00
|
|
|
storeInstance := store.New(db.DBInstance, profile)
|
|
|
|
s.Store = storeInstance
|
|
|
|
|
2023-05-29 14:49:05 +03:00
|
|
|
telegramBotHandler := newTelegramHandler(storeInstance)
|
|
|
|
s.telegramBot = telegram.NewBotWithHandler(telegramBotHandler)
|
2023-05-26 04:43:51 +03:00
|
|
|
|
2022-02-04 13:54:24 +03:00
|
|
|
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
2022-08-18 19:45:02 +03:00
|
|
|
Format: `{"time":"${time_rfc3339}",` +
|
|
|
|
`"method":"${method}","uri":"${uri}",` +
|
|
|
|
`"status":${status},"error":"${error}"}` + "\n",
|
2022-02-04 13:54:24 +03:00
|
|
|
}))
|
|
|
|
|
2023-01-01 18:26:21 +03:00
|
|
|
e.Use(middleware.Gzip())
|
|
|
|
|
2022-07-30 09:52:37 +03:00
|
|
|
e.Use(middleware.CORS())
|
|
|
|
|
2023-01-07 05:51:34 +03:00
|
|
|
e.Use(middleware.SecureWithConfig(middleware.SecureConfig{
|
2023-02-12 12:29:23 +03:00
|
|
|
Skipper: defaultGetRequestSkipper,
|
2023-01-07 05:51:34 +03:00
|
|
|
XSSProtection: "1; mode=block",
|
|
|
|
ContentTypeNosniff: "nosniff",
|
|
|
|
XFrameOptions: "SAMEORIGIN",
|
|
|
|
HSTSPreloadEnabled: false,
|
|
|
|
}))
|
2022-12-31 10:40:53 +03:00
|
|
|
|
2022-02-04 13:54:24 +03:00
|
|
|
e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
|
|
|
|
ErrorMessage: "Request timeout",
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
}))
|
|
|
|
|
2023-01-03 18:05:42 +03:00
|
|
|
serverID, err := s.getSystemServerID(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.ID = serverID
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2023-04-03 08:41:27 +03:00
|
|
|
embedFrontend(e)
|
|
|
|
|
|
|
|
secret := "usememos"
|
2022-05-02 04:40:25 +03:00
|
|
|
if profile.Mode == "prod" {
|
2023-04-03 08:41:27 +03:00
|
|
|
secret, err = s.getSystemSecretSessionName(ctx)
|
2023-01-03 18:05:42 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-29 02:30:29 +03:00
|
|
|
}
|
2023-01-03 18:05:42 +03:00
|
|
|
|
2022-10-26 15:13:02 +03:00
|
|
|
rootGroup := e.Group("")
|
|
|
|
s.registerRSSRoutes(rootGroup)
|
|
|
|
|
2022-09-09 02:40:21 +03:00
|
|
|
publicGroup := e.Group("/o")
|
2023-04-03 08:41:27 +03:00
|
|
|
publicGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return JWTMiddleware(s, next, secret)
|
|
|
|
})
|
2023-01-02 18:18:12 +03:00
|
|
|
registerGetterPublicRoutes(publicGroup)
|
2023-04-03 08:41:27 +03:00
|
|
|
s.registerResourcePublicRoutes(publicGroup)
|
2022-02-03 10:32:03 +03:00
|
|
|
|
|
|
|
apiGroup := e.Group("/api")
|
|
|
|
apiGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
|
2023-04-03 08:41:27 +03:00
|
|
|
return JWTMiddleware(s, next, secret)
|
2022-02-03 10:32:03 +03:00
|
|
|
})
|
2022-03-29 15:53:43 +03:00
|
|
|
s.registerSystemRoutes(apiGroup)
|
2023-04-03 08:41:27 +03:00
|
|
|
s.registerAuthRoutes(apiGroup, secret)
|
2022-02-03 10:32:03 +03:00
|
|
|
s.registerUserRoutes(apiGroup)
|
|
|
|
s.registerMemoRoutes(apiGroup)
|
2023-04-22 05:42:24 +03:00
|
|
|
s.registerMemoResourceRoutes(apiGroup)
|
2022-02-03 10:32:03 +03:00
|
|
|
s.registerShortcutRoutes(apiGroup)
|
|
|
|
s.registerResourceRoutes(apiGroup)
|
2022-06-21 16:58:33 +03:00
|
|
|
s.registerTagRoutes(apiGroup)
|
2023-02-13 14:36:48 +03:00
|
|
|
s.registerStorageRoutes(apiGroup)
|
2023-02-18 06:29:12 +03:00
|
|
|
s.registerIdentityProviderRoutes(apiGroup)
|
2023-03-04 13:22:10 +03:00
|
|
|
s.registerOpenAIRoutes(apiGroup)
|
2023-05-01 11:09:41 +03:00
|
|
|
s.registerMemoRelationRoutes(apiGroup)
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2023-01-03 18:05:42 +03:00
|
|
|
return s, nil
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
|
|
|
|
2023-01-29 19:03:21 +03:00
|
|
|
func (s *Server) Start(ctx context.Context) error {
|
2023-01-02 18:18:12 +03:00
|
|
|
if err := s.createServerStartActivity(ctx); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create activity")
|
|
|
|
}
|
2023-05-26 04:43:51 +03:00
|
|
|
|
2023-05-29 14:49:05 +03:00
|
|
|
go s.telegramBot.Start(ctx)
|
2023-05-26 04:43:51 +03:00
|
|
|
|
2023-01-01 18:26:21 +03:00
|
|
|
return s.e.Start(fmt.Sprintf(":%d", s.Profile.Port))
|
2023-01-01 16:32:17 +03:00
|
|
|
}
|
2023-01-02 18:18:12 +03:00
|
|
|
|
2023-02-03 05:30:18 +03:00
|
|
|
func (s *Server) Shutdown(ctx context.Context) {
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Shutdown echo server
|
|
|
|
if err := s.e.Shutdown(ctx); err != nil {
|
|
|
|
fmt.Printf("failed to shutdown server, error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close database connection
|
|
|
|
if err := s.db.Close(); err != nil {
|
|
|
|
fmt.Printf("failed to close database, error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("memos stopped properly\n")
|
|
|
|
}
|
|
|
|
|
2023-04-17 16:34:59 +03:00
|
|
|
func (s *Server) GetEcho() *echo.Echo {
|
|
|
|
return s.e
|
|
|
|
}
|
|
|
|
|
2023-01-02 18:18:12 +03:00
|
|
|
func (s *Server) createServerStartActivity(ctx context.Context) error {
|
|
|
|
payload := api.ActivityServerStartPayload{
|
2023-01-03 15:05:37 +03:00
|
|
|
ServerID: s.ID,
|
|
|
|
Profile: s.Profile,
|
2023-01-02 18:18:12 +03:00
|
|
|
}
|
2023-02-17 18:55:56 +03:00
|
|
|
payloadBytes, err := json.Marshal(payload)
|
2023-01-02 18:18:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to marshal activity payload")
|
|
|
|
}
|
2023-01-05 15:56:50 +03:00
|
|
|
activity, err := s.Store.CreateActivity(ctx, &api.ActivityCreate{
|
2023-01-02 18:18:12 +03:00
|
|
|
CreatorID: api.UnknownID,
|
|
|
|
Type: api.ActivityServerStart,
|
|
|
|
Level: api.ActivityInfo,
|
2023-02-17 18:55:56 +03:00
|
|
|
Payload: string(payloadBytes),
|
2023-01-02 18:18:12 +03:00
|
|
|
})
|
2023-01-07 06:49:58 +03:00
|
|
|
if err != nil || activity == nil {
|
|
|
|
return errors.Wrap(err, "failed to create activity")
|
|
|
|
}
|
2023-01-02 18:18:12 +03:00
|
|
|
return err
|
|
|
|
}
|