memos/server/server.go

148 lines
3.4 KiB
Go
Raw Normal View History

2022-02-03 10:32:03 +03:00
package server
import (
2023-01-02 18:18:12 +03:00
"context"
"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-03 15:05:37 +03:00
"github.com/google/uuid"
2023-01-02 18:18:12 +03:00
"github.com/pkg/errors"
"github.com/usememos/memos/api"
2023-01-03 15:05:37 +03:00
"github.com/usememos/memos/common"
2022-06-27 17:09:06 +03:00
"github.com/usememos/memos/server/profile"
"github.com/usememos/memos/store"
2022-02-05 12:04:23 +03:00
"github.com/gorilla/securecookie"
2022-02-03 10:32:03 +03:00
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
type Server struct {
e *echo.Echo
2023-01-03 15:05:37 +03:00
ID string
2022-10-29 06:15:39 +03:00
Collector *MetricCollector
2022-05-22 04:29:34 +03:00
Profile *profile.Profile
2022-03-29 15:53:43 +03:00
2022-05-16 02:37:23 +03:00
Store *store.Store
2022-02-03 10:32:03 +03:00
}
2022-05-22 04:29:34 +03:00
func NewServer(profile *profile.Profile) *Server {
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-01 16:32:17 +03:00
s := &Server{
e: e,
Profile: profile,
}
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
}))
e.Use(middleware.Gzip())
2022-12-29 19:17:19 +03:00
e.Use(middleware.CSRFWithConfig(middleware.CSRFConfig{
Skipper: func(c echo.Context) bool {
return s.DefaultAuthSkipper(c)
},
2022-12-29 19:17:19 +03:00
TokenLookup: "cookie:_csrf",
}))
2022-07-30 09:52:37 +03:00
e.Use(middleware.CORS())
e.Use(middleware.Secure())
2022-02-04 13:54:24 +03:00
e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Skipper: middleware.DefaultSkipper,
ErrorMessage: "Request timeout",
Timeout: 30 * time.Second,
}))
2022-07-10 04:02:56 +03:00
embedFrontend(e)
2022-02-03 10:32:03 +03:00
// In dev mode, set the const secret key to make signin session persistence.
2022-05-21 07:33:18 +03:00
secret := []byte("usememos")
2022-05-02 04:40:25 +03:00
if profile.Mode == "prod" {
2022-03-29 02:30:29 +03:00
secret = securecookie.GenerateRandomKey(16)
}
e.Use(session.Middleware(sessions.NewCookieStore(secret)))
2022-02-03 10:32:03 +03:00
rootGroup := e.Group("")
s.registerRSSRoutes(rootGroup)
webhookGroup := e.Group("/h")
s.registerResourcePublicRoutes(webhookGroup)
2022-09-09 02:40:21 +03:00
publicGroup := e.Group("/o")
s.registerResourcePublicRoutes(publicGroup)
2023-01-02 18:18:12 +03:00
registerGetterPublicRoutes(publicGroup)
2022-02-03 10:32:03 +03:00
apiGroup := e.Group("/api")
apiGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
2022-07-27 14:45:37 +03:00
return aclMiddleware(s, next)
2022-02-03 10:32:03 +03:00
})
2022-03-29 15:53:43 +03:00
s.registerSystemRoutes(apiGroup)
2022-02-03 10:32:03 +03:00
s.registerAuthRoutes(apiGroup)
s.registerUserRoutes(apiGroup)
s.registerMemoRoutes(apiGroup)
s.registerShortcutRoutes(apiGroup)
s.registerResourceRoutes(apiGroup)
2022-06-21 16:58:33 +03:00
s.registerTagRoutes(apiGroup)
2022-02-03 10:32:03 +03:00
return s
}
2023-01-02 18:18:12 +03:00
func (s *Server) Run(ctx context.Context) error {
2023-01-03 15:05:37 +03:00
serverIDKey := api.SystemSettingServerID
serverIDValue, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
Name: &serverIDKey,
})
if err != nil && common.ErrorCode(err) != common.NotFound {
return err
}
if serverIDValue == nil || serverIDValue.Value == "" {
serverIDValue, err = s.Store.UpsertSystemSetting(ctx, &api.SystemSettingUpsert{
Name: serverIDKey,
Value: uuid.NewString(),
})
if err != nil {
return err
}
}
s.ID = serverIDValue.Value
2023-01-02 18:18:12 +03:00
if err := s.createServerStartActivity(ctx); err != nil {
return errors.Wrap(err, "failed to create activity")
}
2023-01-03 15:05:37 +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
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
}
payloadStr, err := json.Marshal(payload)
if err != nil {
return errors.Wrap(err, "failed to marshal activity payload")
}
_, err = s.Store.CreateActivity(ctx, &api.ActivityCreate{
CreatorID: api.UnknownID,
Type: api.ActivityServerStart,
Level: api.ActivityInfo,
Payload: string(payloadStr),
})
return err
}