memos/server/server.go

154 lines
3.7 KiB
Go
Raw Normal View History

2022-02-03 15:32:03 +08:00
package server
import (
2023-01-02 23:18:12 +08:00
"context"
"encoding/json"
2022-02-03 15:32:03 +08:00
"fmt"
2022-02-04 18:54:24 +08:00
"time"
2022-02-03 15:32:03 +08:00
2023-01-02 23:18:12 +08:00
"github.com/pkg/errors"
"github.com/usememos/memos/api"
2023-01-05 20:56:50 +08:00
metric "github.com/usememos/memos/plugin/metrics"
2022-06-27 22:09:06 +08:00
"github.com/usememos/memos/server/profile"
"github.com/usememos/memos/store"
"github.com/usememos/memos/store/db"
2022-06-27 22:09:06 +08:00
2022-02-03 15:32:03 +08: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
ID string
Profile *profile.Profile
Store *store.Store
2022-10-29 11:15:39 +08:00
Collector *MetricCollector
2022-02-03 15:32:03 +08:00
}
func NewServer(ctx context.Context, profile *profile.Profile) (*Server, error) {
2022-02-03 15:32:03 +08:00
e := echo.New()
e.Debug = true
e.HideBanner = true
2022-05-20 22:48:36 +08:00
e.HidePort = true
2022-02-03 15:32:03 +08:00
2023-01-01 21:32:17 +08:00
s := &Server{
e: e,
Profile: profile,
}
db := db.NewDB(profile)
if err := db.Open(ctx); err != nil {
return nil, errors.Wrap(err, "cannot open db")
}
storeInstance := store.New(db.DBInstance, profile)
s.Store = storeInstance
2022-02-04 18:54:24 +08:00
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
2022-08-19 00:45:02 +08:00
Format: `{"time":"${time_rfc3339}",` +
`"method":"${method}","uri":"${uri}",` +
`"status":${status},"error":"${error}"}` + "\n",
2022-02-04 18:54:24 +08:00
}))
e.Use(middleware.Gzip())
2022-12-30 00:17:19 +08:00
e.Use(middleware.CSRFWithConfig(middleware.CSRFConfig{
Skipper: func(c echo.Context) bool {
return s.DefaultAuthSkipper(c)
},
2022-12-30 00:17:19 +08:00
TokenLookup: "cookie:_csrf",
}))
2022-07-30 14:52:37 +08:00
e.Use(middleware.CORS())
2023-01-07 10:51:34 +08:00
e.Use(middleware.SecureWithConfig(middleware.SecureConfig{
Skipper: DefaultGetRequestSkipper,
XSSProtection: "1; mode=block",
ContentTypeNosniff: "nosniff",
XFrameOptions: "SAMEORIGIN",
HSTSPreloadEnabled: false,
}))
2022-02-04 18:54:24 +08:00
e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
Skipper: middleware.DefaultSkipper,
ErrorMessage: "Request timeout",
Timeout: 30 * time.Second,
}))
serverID, err := s.getSystemServerID(ctx)
if err != nil {
return nil, err
}
s.ID = serverID
2022-02-03 15:32:03 +08:00
secretSessionName := "usememos"
2022-05-02 09:40:25 +08:00
if profile.Mode == "prod" {
secretSessionName, err = s.getSystemSecretSessionName(ctx)
if err != nil {
return nil, err
}
2022-03-29 07:30:29 +08:00
}
e.Use(session.Middleware(sessions.NewCookieStore([]byte(secretSessionName))))
embedFrontend(e)
2022-02-03 15:32:03 +08:00
2023-01-05 20:56:50 +08:00
// Register MetricCollector to server.
s.registerMetricCollector()
rootGroup := e.Group("")
s.registerRSSRoutes(rootGroup)
webhookGroup := e.Group("/h")
s.registerResourcePublicRoutes(webhookGroup)
2022-09-09 07:40:21 +08:00
publicGroup := e.Group("/o")
s.registerResourcePublicRoutes(publicGroup)
2023-01-02 23:18:12 +08:00
registerGetterPublicRoutes(publicGroup)
2022-02-03 15:32:03 +08:00
apiGroup := e.Group("/api")
apiGroup.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
2022-07-27 19:45:37 +08:00
return aclMiddleware(s, next)
2022-02-03 15:32:03 +08:00
})
2022-03-29 20:53:43 +08:00
s.registerSystemRoutes(apiGroup)
2022-02-03 15:32:03 +08:00
s.registerAuthRoutes(apiGroup)
s.registerUserRoutes(apiGroup)
s.registerMemoRoutes(apiGroup)
s.registerShortcutRoutes(apiGroup)
s.registerResourceRoutes(apiGroup)
2022-06-21 21:58:33 +08:00
s.registerTagRoutes(apiGroup)
2022-02-03 15:32:03 +08:00
return s, nil
2022-02-03 15:32:03 +08:00
}
2023-01-02 23:18:12 +08:00
func (s *Server) Run(ctx context.Context) error {
if err := s.createServerStartActivity(ctx); err != nil {
return errors.Wrap(err, "failed to create activity")
}
2023-01-05 20:56:50 +08:00
s.Collector.Identify(ctx)
return s.e.Start(fmt.Sprintf(":%d", s.Profile.Port))
2023-01-01 21:32:17 +08:00
}
2023-01-02 23:18:12 +08:00
func (s *Server) createServerStartActivity(ctx context.Context) error {
payload := api.ActivityServerStartPayload{
2023-01-03 20:05:37 +08:00
ServerID: s.ID,
Profile: s.Profile,
2023-01-02 23:18:12 +08:00
}
payloadStr, err := json.Marshal(payload)
if err != nil {
return errors.Wrap(err, "failed to marshal activity payload")
}
2023-01-05 20:56:50 +08:00
activity, err := s.Store.CreateActivity(ctx, &api.ActivityCreate{
2023-01-02 23:18:12 +08:00
CreatorID: api.UnknownID,
Type: api.ActivityServerStart,
Level: api.ActivityInfo,
Payload: string(payloadStr),
})
2023-01-05 20:56:50 +08:00
s.Collector.Collect(ctx, &metric.Metric{
Name: string(activity.Type),
})
2023-01-02 23:18:12 +08:00
return err
}