2023-03-12 16:00:57 +01:00
|
|
|
// GoToSocial
|
|
|
|
// Copyright (C) GoToSocial Authors admin@gotosocial.org
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
//
|
|
|
|
// 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, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
2021-12-07 13:31:39 +01:00
|
|
|
|
2021-05-30 13:12:00 +02:00
|
|
|
package server
|
2021-03-04 14:38:18 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-02 13:10:50 +01:00
|
|
|
"errors"
|
2021-03-04 14:38:18 +01:00
|
|
|
"fmt"
|
2023-01-02 13:10:50 +01:00
|
|
|
"net/http"
|
2021-03-04 14:38:18 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2023-08-03 11:34:35 +02:00
|
|
|
"time"
|
2021-03-04 14:38:18 +01:00
|
|
|
|
2023-01-02 13:10:50 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-12-07 13:31:39 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/cmd/gotosocial/action"
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api"
|
2023-01-02 13:10:50 +01:00
|
|
|
apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
|
2023-10-30 18:35:11 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/cleaner"
|
2023-01-02 13:10:50 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2023-11-20 16:43:55 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/metrics"
|
2023-01-02 13:10:50 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/middleware"
|
2023-05-25 10:37:38 +02:00
|
|
|
tlprocessor "github.com/superseriousbusiness/gotosocial/internal/processing/timeline"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/timeline"
|
2023-05-09 19:19:48 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/tracing"
|
2023-05-25 10:37:38 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/visibility"
|
2023-01-17 21:59:04 +01:00
|
|
|
"go.uber.org/automaxprocs/maxprocs"
|
2023-01-02 13:10:50 +01:00
|
|
|
|
2021-04-01 20:46:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2021-08-25 15:34:33 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
|
2021-10-31 15:46:23 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/email"
|
2021-04-01 20:46:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/federation"
|
2021-05-27 16:06:24 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/federation/federatingdb"
|
2021-05-30 13:12:00 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gotosocial"
|
2022-05-15 11:16:43 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/httpclient"
|
2022-07-19 10:47:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-04-01 20:46:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/media"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
2021-07-23 10:36:28 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oidc"
|
2021-05-30 13:12:00 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/processing"
|
2021-04-01 20:46:45 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/router"
|
2022-12-08 18:35:14 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/state"
|
2022-07-03 12:08:30 +02:00
|
|
|
gtsstorage "github.com/superseriousbusiness/gotosocial/internal/storage"
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/transport"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
|
2021-06-21 19:46:10 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/web"
|
2023-01-17 21:59:04 +01:00
|
|
|
|
|
|
|
// Inherit memory limit if set from cgroup
|
|
|
|
_ "github.com/KimMachineGun/automemlimit"
|
2021-03-04 14:38:18 +01:00
|
|
|
)
|
|
|
|
|
2021-05-30 13:12:00 +02:00
|
|
|
// Start creates and starts a gotosocial server
|
2021-12-07 13:31:39 +01:00
|
|
|
var Start action.GTSAction = func(ctx context.Context) error {
|
2023-04-08 11:51:24 +02:00
|
|
|
if _, err := maxprocs.Set(maxprocs.Logger(nil)); err != nil {
|
|
|
|
log.Infof(ctx, "could not set CPU limits from cgroup: %s", err)
|
2023-01-17 21:59:04 +01:00
|
|
|
}
|
|
|
|
|
2022-12-08 18:35:14 +01:00
|
|
|
var state state.State
|
|
|
|
|
|
|
|
// Initialize caches
|
|
|
|
state.Caches.Init()
|
2023-02-03 21:03:05 +01:00
|
|
|
state.Caches.Start()
|
|
|
|
defer state.Caches.Stop()
|
2022-12-08 18:35:14 +01:00
|
|
|
|
2023-05-09 19:19:48 +02:00
|
|
|
// Initialize Tracing
|
|
|
|
if err := tracing.Initialize(); err != nil {
|
|
|
|
return fmt.Errorf("error initializing tracing: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-12-08 18:35:14 +01:00
|
|
|
// Open connection to the database
|
|
|
|
dbService, err := bundb.NewBunDBService(ctx, &state)
|
2021-03-04 14:38:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating dbservice: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-12-08 18:35:14 +01:00
|
|
|
// Set the state DB connection
|
|
|
|
state.DB = dbService
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := dbService.CreateInstanceAccount(ctx); err != nil {
|
2021-06-13 18:42:28 +02:00
|
|
|
return fmt.Errorf("error creating instance account: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := dbService.CreateInstanceInstance(ctx); err != nil {
|
2021-06-13 18:42:28 +02:00
|
|
|
return fmt.Errorf("error creating instance instance: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-01-22 16:17:04 +01:00
|
|
|
// Get the instance account
|
|
|
|
// (we'll need this later).
|
|
|
|
instanceAccount, err := dbService.GetInstanceAccount(ctx, "")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error retrieving instance account: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-09-11 21:18:06 +02:00
|
|
|
// Open the storage backend
|
2022-07-03 12:08:30 +02:00
|
|
|
storage, err := gtsstorage.AutoConfig()
|
2021-04-01 20:46:45 +02:00
|
|
|
if err != nil {
|
2022-07-03 12:08:30 +02:00
|
|
|
return fmt.Errorf("error creating storage backend: %w", err)
|
2021-04-01 20:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-02-13 19:40:48 +01:00
|
|
|
// Set the state storage driver
|
|
|
|
state.Storage = storage
|
|
|
|
|
2023-07-07 16:17:39 +02:00
|
|
|
// Build HTTP client
|
|
|
|
client := httpclient.New(httpclient.Config{
|
2023-08-01 19:50:17 +02:00
|
|
|
AllowRanges: config.MustParseIPPrefixes(config.GetHTTPClientAllowIPs()),
|
|
|
|
BlockRanges: config.MustParseIPPrefixes(config.GetHTTPClientBlockIPs()),
|
|
|
|
Timeout: config.GetHTTPClientTimeout(),
|
|
|
|
TLSInsecureSkipVerify: config.GetHTTPClientTLSInsecureSkipVerify(),
|
2023-07-07 16:17:39 +02:00
|
|
|
})
|
2022-05-15 11:16:43 +02:00
|
|
|
|
2023-02-13 19:40:48 +01:00
|
|
|
// Initialize workers.
|
|
|
|
state.Workers.Start()
|
|
|
|
defer state.Workers.Stop()
|
|
|
|
|
2023-08-03 11:34:35 +02:00
|
|
|
// Add a task to the scheduler to sweep caches.
|
|
|
|
// Frequency = 1 * minute
|
|
|
|
// Threshold = 80% capacity
|
2023-11-04 21:21:20 +01:00
|
|
|
_ = state.Workers.Scheduler.AddRecurring(
|
|
|
|
"@cachesweep", // id
|
|
|
|
time.Time{}, // start
|
|
|
|
time.Minute, // freq
|
|
|
|
func(context.Context, time.Time) {
|
2024-01-19 13:57:29 +01:00
|
|
|
state.Caches.Sweep(60)
|
2023-11-04 21:21:20 +01:00
|
|
|
},
|
|
|
|
)
|
2023-08-03 11:34:35 +02:00
|
|
|
|
2023-05-25 10:37:38 +02:00
|
|
|
// Build handlers used in later initializations.
|
2023-02-13 19:40:48 +01:00
|
|
|
mediaManager := media.NewManager(&state)
|
2021-10-11 14:37:33 +02:00
|
|
|
oauthServer := oauth.New(ctx, dbService)
|
2023-09-23 18:44:11 +02:00
|
|
|
typeConverter := typeutils.NewConverter(&state)
|
2023-05-25 10:37:38 +02:00
|
|
|
filter := visibility.NewFilter(&state)
|
2023-11-04 21:21:20 +01:00
|
|
|
federatingDB := federatingdb.New(&state, typeConverter, filter)
|
2023-03-08 13:57:41 +01:00
|
|
|
transportController := transport.NewController(&state, federatingDB, &federation.Clock{}, client)
|
2023-05-12 11:15:54 +02:00
|
|
|
federator := federation.NewFederator(&state, federatingDB, transportController, typeConverter, mediaManager)
|
2021-10-31 15:46:23 +01:00
|
|
|
|
2023-05-25 10:37:38 +02:00
|
|
|
// Decide whether to create a noop email
|
|
|
|
// sender (won't send emails) or a real one.
|
2021-10-31 15:46:23 +01:00
|
|
|
var emailSender email.Sender
|
2022-05-30 14:41:24 +02:00
|
|
|
if smtpHost := config.GetSMTPHost(); smtpHost != "" {
|
2023-05-25 10:37:38 +02:00
|
|
|
// Host is defined; create a proper sender.
|
2021-12-07 13:31:39 +01:00
|
|
|
emailSender, err = email.NewSender()
|
2021-10-31 15:46:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating email sender: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
2023-05-25 10:37:38 +02:00
|
|
|
// No host is defined; create a noop sender.
|
2021-12-07 13:31:39 +01:00
|
|
|
emailSender, err = email.NewNoopSender(nil)
|
2021-10-31 15:46:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating noop email sender: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 10:37:38 +02:00
|
|
|
// Initialize timelines.
|
|
|
|
state.Timelines.Home = timeline.NewManager(
|
|
|
|
tlprocessor.HomeTimelineGrab(&state),
|
|
|
|
tlprocessor.HomeTimelineFilter(&state, filter),
|
|
|
|
tlprocessor.HomeTimelineStatusPrepare(&state, typeConverter),
|
|
|
|
tlprocessor.SkipInsert(),
|
|
|
|
)
|
|
|
|
if err := state.Timelines.Home.Start(); err != nil {
|
|
|
|
return fmt.Errorf("error starting home timeline: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Timelines.List = timeline.NewManager(
|
|
|
|
tlprocessor.ListTimelineGrab(&state),
|
|
|
|
tlprocessor.ListTimelineFilter(&state, filter),
|
|
|
|
tlprocessor.ListTimelineStatusPrepare(&state, typeConverter),
|
|
|
|
tlprocessor.SkipInsert(),
|
|
|
|
)
|
|
|
|
if err := state.Timelines.List.Start(); err != nil {
|
|
|
|
return fmt.Errorf("error starting list timeline: %s", err)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
2023-10-30 18:35:11 +01:00
|
|
|
// Create a media cleaner using the given state.
|
|
|
|
cleaner := cleaner.New(&state)
|
|
|
|
|
2023-05-25 10:37:38 +02:00
|
|
|
// Create the processor using all the other services we've created so far.
|
2023-10-30 18:35:11 +01:00
|
|
|
processor := processing.NewProcessor(
|
|
|
|
cleaner,
|
|
|
|
typeConverter,
|
|
|
|
federator,
|
|
|
|
oauthServer,
|
|
|
|
mediaManager,
|
|
|
|
&state,
|
|
|
|
emailSender,
|
|
|
|
)
|
2023-05-25 10:37:38 +02:00
|
|
|
|
2023-09-04 15:55:17 +02:00
|
|
|
// Set state client / federator asynchronous worker enqueue functions
|
2023-08-09 19:14:33 +02:00
|
|
|
state.Workers.EnqueueClientAPI = processor.Workers().EnqueueClientAPI
|
|
|
|
state.Workers.EnqueueFediAPI = processor.Workers().EnqueueFediAPI
|
2023-03-01 19:26:53 +01:00
|
|
|
|
2023-09-04 15:55:17 +02:00
|
|
|
// Set state client / federator synchronous processing functions.
|
|
|
|
state.Workers.ProcessFromClientAPI = processor.Workers().ProcessFromClientAPI
|
|
|
|
state.Workers.ProcessFromFediAPI = processor.Workers().ProcessFromFediAPI
|
|
|
|
|
2023-11-08 15:32:17 +01:00
|
|
|
// Schedule tasks for all existing poll expiries.
|
|
|
|
if err := processor.Polls().ScheduleAll(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error scheduling poll expiries: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-14 11:58:55 +01:00
|
|
|
// Initialize metrics.
|
|
|
|
if err := metrics.Initialize(state.DB); err != nil {
|
|
|
|
return fmt.Errorf("error initializing metrics: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-01-02 13:10:50 +01:00
|
|
|
/*
|
|
|
|
HTTP router initialization
|
|
|
|
*/
|
|
|
|
|
|
|
|
router, err := router.New(ctx)
|
2021-07-23 10:36:28 +02:00
|
|
|
if err != nil {
|
2023-01-02 13:10:50 +01:00
|
|
|
return fmt.Errorf("error creating router: %s", err)
|
2021-07-23 10:36:28 +02:00
|
|
|
}
|
2023-11-20 16:43:55 +01:00
|
|
|
|
2023-12-18 15:18:25 +01:00
|
|
|
// Start preparing middleware stack.
|
|
|
|
middlewares := make([]gin.HandlerFunc, 1)
|
|
|
|
|
|
|
|
// RequestID middleware must run before tracing!
|
|
|
|
middlewares[0] = middleware.AddRequestID(config.GetRequestIDHeader())
|
|
|
|
|
|
|
|
// Add tracing middleware if enabled.
|
2023-05-09 19:19:48 +02:00
|
|
|
if config.GetTracingEnabled() {
|
|
|
|
middlewares = append(middlewares, tracing.InstrumentGin())
|
|
|
|
}
|
2023-11-20 16:43:55 +01:00
|
|
|
|
2023-12-18 15:18:25 +01:00
|
|
|
// Add metrics middleware if enabled.
|
2023-11-20 16:43:55 +01:00
|
|
|
if config.GetMetricsEnabled() {
|
|
|
|
middlewares = append(middlewares, metrics.InstrumentGin())
|
|
|
|
}
|
|
|
|
|
2023-05-09 19:19:48 +02:00
|
|
|
middlewares = append(middlewares, []gin.HandlerFunc{
|
2023-02-17 12:02:29 +01:00
|
|
|
// note: hooks adding ctx fields must be ABOVE
|
|
|
|
// the logger, otherwise won't be accessible.
|
2023-05-21 17:12:47 +02:00
|
|
|
middleware.Logger(config.GetLogClientIP()),
|
2023-12-18 15:18:25 +01:00
|
|
|
middleware.HeaderFilter(&state),
|
2023-01-02 13:10:50 +01:00
|
|
|
middleware.UserAgent(),
|
|
|
|
middleware.CORS(),
|
|
|
|
middleware.ExtraHeaders(),
|
2023-05-09 19:19:48 +02:00
|
|
|
}...)
|
|
|
|
|
2023-08-20 13:35:55 +02:00
|
|
|
// Instantiate Content-Security-Policy
|
|
|
|
// middleware, with extra URIs.
|
|
|
|
cspExtraURIs := make([]string, 0)
|
|
|
|
|
|
|
|
// Probe storage to check if extra URI is needed in CSP.
|
|
|
|
// Error here means something is wrong with storage.
|
|
|
|
storageCSPUri, err := state.Storage.ProbeCSPUri(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error deriving Content-Security-Policy uri from storage: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// storageCSPUri may be empty string if
|
|
|
|
// not S3-backed storage; check for this.
|
|
|
|
if storageCSPUri != "" {
|
|
|
|
cspExtraURIs = append(cspExtraURIs, storageCSPUri)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add any extra CSP URIs from config.
|
|
|
|
cspExtraURIs = append(cspExtraURIs, config.GetAdvancedCSPExtraURIs()...)
|
|
|
|
|
|
|
|
// Add CSP to middlewares.
|
|
|
|
middlewares = append(middlewares, middleware.ContentSecurityPolicy(cspExtraURIs...))
|
|
|
|
|
2023-05-09 19:19:48 +02:00
|
|
|
// attach global middlewares which are used for every request
|
|
|
|
router.AttachGlobalMiddleware(middlewares...)
|
2023-01-02 13:10:50 +01:00
|
|
|
|
|
|
|
// attach global no route / 404 handler to the router
|
|
|
|
router.AttachNoRouteHandler(func(c *gin.Context) {
|
2023-02-02 14:08:13 +01:00
|
|
|
apiutil.ErrorHandler(c, gtserror.NewErrorNotFound(errors.New(http.StatusText(http.StatusNotFound))), processor.InstanceGetV1)
|
2023-01-02 13:10:50 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
// build router modules
|
|
|
|
var idp oidc.IDP
|
|
|
|
if config.GetOIDCEnabled() {
|
|
|
|
idp, err = oidc.NewIDP(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating oidc idp: %w", err)
|
|
|
|
}
|
2021-04-01 20:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 13:10:50 +01:00
|
|
|
routerSession, err := dbService.GetSession(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error retrieving router session for session middleware: %w", err)
|
2021-04-01 20:46:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 13:10:50 +01:00
|
|
|
sessionName, err := middleware.SessionName()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error generating session name for session middleware: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
authModule = api.NewAuth(dbService, processor, idp, routerSession, sessionName) // auth/oauth paths
|
|
|
|
clientModule = api.NewClient(dbService, processor) // api client endpoints
|
2023-11-23 19:10:51 +01:00
|
|
|
metricsModule = api.NewMetrics() // Metrics endpoints
|
2023-01-02 13:10:50 +01:00
|
|
|
fileserverModule = api.NewFileserver(processor) // fileserver endpoints
|
|
|
|
wellKnownModule = api.NewWellKnown(processor) // .well-known endpoints
|
|
|
|
nodeInfoModule = api.NewNodeInfo(processor) // nodeinfo endpoint
|
|
|
|
activityPubModule = api.NewActivityPub(dbService, processor) // ActivityPub endpoints
|
2023-02-07 14:57:09 +01:00
|
|
|
webModule = web.New(dbService, processor) // web pages + user profiles + settings panels etc
|
2023-01-02 13:10:50 +01:00
|
|
|
)
|
|
|
|
|
2023-01-03 11:50:59 +01:00
|
|
|
// create required middleware
|
2023-01-04 11:57:59 +01:00
|
|
|
// rate limiting
|
2023-08-23 14:32:27 +02:00
|
|
|
rlLimit := config.GetAdvancedRateLimitRequests()
|
|
|
|
rlExceptions := config.GetAdvancedRateLimitExceptions()
|
2024-01-22 16:17:04 +01:00
|
|
|
clLimit := middleware.RateLimit(rlLimit, rlExceptions) // client api
|
|
|
|
s2sLimit := middleware.RateLimit(rlLimit, rlExceptions) // server-to-server (AP)
|
|
|
|
fsMainLimit := middleware.RateLimit(rlLimit, rlExceptions) // fileserver / web templates
|
|
|
|
fsEmojiLimit := middleware.RateLimit(rlLimit*2, rlExceptions) // fileserver (emojis only, use high limit)
|
2023-01-03 11:50:59 +01:00
|
|
|
|
2023-01-04 11:57:59 +01:00
|
|
|
// throttling
|
|
|
|
cpuMultiplier := config.GetAdvancedThrottlingMultiplier()
|
2023-02-10 21:16:01 +01:00
|
|
|
retryAfter := config.GetAdvancedThrottlingRetryAfter()
|
|
|
|
clThrottle := middleware.Throttle(cpuMultiplier, retryAfter) // client api
|
|
|
|
s2sThrottle := middleware.Throttle(cpuMultiplier, retryAfter) // server-to-server (AP)
|
2024-01-22 16:17:04 +01:00
|
|
|
fsThrottle := middleware.Throttle(cpuMultiplier, retryAfter) // fileserver / web templates / emojis
|
2023-02-10 21:16:01 +01:00
|
|
|
pkThrottle := middleware.Throttle(cpuMultiplier, retryAfter) // throttle public key endpoint separately
|
2023-01-04 11:57:59 +01:00
|
|
|
|
|
|
|
gzip := middleware.Gzip() // applied to all except fileserver
|
|
|
|
|
|
|
|
// these should be routed in order;
|
|
|
|
// apply throttling *after* rate limiting
|
|
|
|
authModule.Route(router, clLimit, clThrottle, gzip)
|
|
|
|
clientModule.Route(router, clLimit, clThrottle, gzip)
|
2023-11-23 19:10:51 +01:00
|
|
|
metricsModule.Route(router, clLimit, clThrottle, gzip)
|
2024-01-22 16:17:04 +01:00
|
|
|
fileserverModule.Route(router, fsMainLimit, fsThrottle)
|
|
|
|
fileserverModule.RouteEmojis(router, instanceAccount.ID, fsEmojiLimit, fsThrottle)
|
2023-01-04 11:57:59 +01:00
|
|
|
wellKnownModule.Route(router, gzip, s2sLimit, s2sThrottle)
|
|
|
|
nodeInfoModule.Route(router, s2sLimit, s2sThrottle, gzip)
|
|
|
|
activityPubModule.Route(router, s2sLimit, s2sThrottle, gzip)
|
2023-02-08 15:10:56 +01:00
|
|
|
activityPubModule.RoutePublicKey(router, s2sLimit, pkThrottle, gzip)
|
2024-01-22 16:17:04 +01:00
|
|
|
webModule.Route(router, fsMainLimit, fsThrottle, gzip)
|
2023-01-02 13:10:50 +01:00
|
|
|
|
2023-10-30 18:35:11 +01:00
|
|
|
// Start the GoToSocial server.
|
|
|
|
server := gotosocial.NewServer(dbService, router, cleaner)
|
|
|
|
if err := server.Start(ctx); err != nil {
|
2021-04-01 20:46:45 +02:00
|
|
|
return fmt.Errorf("error starting gotosocial service: %s", err)
|
|
|
|
}
|
2021-03-05 18:31:12 +01:00
|
|
|
|
2021-03-04 14:38:18 +01:00
|
|
|
// catch shutdown signals from the operating system
|
|
|
|
sigs := make(chan os.Signal, 1)
|
2023-01-03 11:50:59 +01:00
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
sig := <-sigs // block until signal received
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Infof(ctx, "received signal %s, shutting down", sig)
|
2021-03-04 14:38:18 +01:00
|
|
|
|
|
|
|
// close down all running services in order
|
2023-10-30 18:35:11 +01:00
|
|
|
if err := server.Stop(ctx); err != nil {
|
2021-04-01 20:46:45 +02:00
|
|
|
return fmt.Errorf("error closing gotosocial service: %s", err)
|
2021-03-04 14:38:18 +01:00
|
|
|
}
|
|
|
|
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Info(ctx, "done! exiting...")
|
2021-03-04 14:38:18 +01:00
|
|
|
return nil
|
|
|
|
}
|