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"
|
2024-07-12 11:39:47 +02:00
|
|
|
"runtime"
|
2024-06-13 19:02:48 +02:00
|
|
|
"strings"
|
2021-03-04 14:38:18 +01:00
|
|
|
"syscall"
|
2023-08-03 11:34:35 +02:00
|
|
|
"time"
|
2021-03-04 14:38:18 +01:00
|
|
|
|
2024-06-13 19:02:48 +02:00
|
|
|
"github.com/KimMachineGun/automemlimit/memlimit"
|
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"
|
2024-07-24 13:27:42 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/filter/interaction"
|
2024-02-27 13:22:05 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/filter/spam"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/filter/visibility"
|
2023-01-02 13:10:50 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2024-07-12 11:39:47 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/media/ffmpeg"
|
2024-04-26 14:50:46 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/messages"
|
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-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"
|
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"
|
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 {
|
2024-07-12 11:39:47 +02:00
|
|
|
// Set GOMAXPROCS / GOMEMLIMIT
|
|
|
|
// to match container limits.
|
|
|
|
setLimits(ctx)
|
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
var (
|
|
|
|
// Define necessary core variables
|
|
|
|
// before anything so we can prepare
|
|
|
|
// defer function for safe shutdown
|
|
|
|
// depending on what services were
|
|
|
|
// managed to be started.
|
2024-07-30 13:58:31 +02:00
|
|
|
state = new(state.State)
|
|
|
|
route *router.Router
|
|
|
|
process *processing.Processor
|
2024-05-29 13:21:04 +02:00
|
|
|
)
|
2022-12-08 18:35:14 +01:00
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
defer func() {
|
|
|
|
// Stop caches with
|
|
|
|
// background tasks.
|
|
|
|
state.Caches.Stop()
|
|
|
|
|
|
|
|
if route != nil {
|
|
|
|
// We reached a point where the API router
|
|
|
|
// was created + setup. Ensure it gets stopped
|
|
|
|
// first to stop processing new information.
|
|
|
|
if err := route.Stop(); err != nil {
|
|
|
|
log.Errorf(ctx, "error stopping router: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop any currently running
|
|
|
|
// worker processes / scheduled
|
|
|
|
// tasks from being executed.
|
|
|
|
state.Workers.Stop()
|
|
|
|
|
|
|
|
if state.Timelines.Home != nil {
|
|
|
|
// Home timeline mgr was setup, ensure it gets stopped.
|
|
|
|
if err := state.Timelines.Home.Stop(); err != nil {
|
|
|
|
log.Errorf(ctx, "error stopping home timeline: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if state.Timelines.List != nil {
|
|
|
|
// List timeline mgr was setup, ensure it gets stopped.
|
|
|
|
if err := state.Timelines.List.Stop(); err != nil {
|
|
|
|
log.Errorf(ctx, "error stopping list timeline: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 13:58:31 +02:00
|
|
|
if process != nil {
|
|
|
|
const timeout = time.Minute
|
|
|
|
|
|
|
|
// Use a new timeout context to ensure
|
|
|
|
// persisting queued tasks does not fail!
|
|
|
|
// The main ctx is very likely canceled.
|
|
|
|
ctx := context.WithoutCancel(ctx)
|
|
|
|
ctx, cncl := context.WithTimeout(ctx, timeout)
|
|
|
|
defer cncl()
|
|
|
|
|
|
|
|
// Now that all the "moving" components have been stopped,
|
|
|
|
// persist any remaining queued worker tasks to the database.
|
|
|
|
if err := process.Admin().PersistWorkerQueues(ctx); err != nil {
|
|
|
|
log.Errorf(ctx, "error persisting worker queues: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
if state.DB != nil {
|
|
|
|
// Lastly, if database service was started,
|
|
|
|
// ensure it gets closed now all else stopped.
|
|
|
|
if err := state.DB.Close(); err != nil {
|
|
|
|
log.Errorf(ctx, "error stopping database: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 18:35:14 +01:00
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
// Finally reached end of shutdown.
|
|
|
|
log.Info(ctx, "done! exiting...")
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Initialize tracing (noop if not enabled).
|
2023-05-09 19:19:48 +02:00
|
|
|
if err := tracing.Initialize(); err != nil {
|
|
|
|
return fmt.Errorf("error initializing tracing: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
// Initialize caches
|
|
|
|
state.Caches.Init()
|
|
|
|
state.Caches.Start()
|
|
|
|
|
|
|
|
// Open connection to the database now caches started.
|
2024-05-30 11:55:57 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
// Set DB on state.
|
2022-12-08 18:35:14 +01:00
|
|
|
state.DB = dbService
|
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
// Ensure necessary database instance prerequisites exist.
|
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-04-11 11:45:53 +02:00
|
|
|
}
|
|
|
|
if err := dbService.CreateInstanceApplication(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error creating instance application: %s", err)
|
2021-06-13 18:42:28 +02:00
|
|
|
}
|
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
// Get the instance account (we'll need this later).
|
2024-01-22 16:17:04 +01:00
|
|
|
instanceAccount, err := dbService.GetInstanceAccount(ctx, "")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error retrieving instance account: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
// Open the storage backend according to config.
|
|
|
|
state.Storage, err = gtsstorage.AutoConfig()
|
2021-04-01 20:46:45 +02:00
|
|
|
if err != nil {
|
2024-05-29 13:21:04 +02:00
|
|
|
return fmt.Errorf("error opening storage backend: %w", err)
|
2021-04-01 20:46:45 +02:00
|
|
|
}
|
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
// Prepare wrapped httpclient with config.
|
2023-07-07 16:17:39 +02:00
|
|
|
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
|
|
|
|
2024-08-26 18:21:29 +02:00
|
|
|
// Compile WASM modules ahead of first use
|
|
|
|
// to prevent unexpected initial slowdowns.
|
|
|
|
//
|
|
|
|
// Note that this can take a bit of memory
|
|
|
|
// and processing so we perform this much
|
|
|
|
// later after any database migrations.
|
|
|
|
log.Info(ctx, "compiling WebAssembly")
|
|
|
|
if err := compileWASM(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-25 10:37:38 +02:00
|
|
|
// Build handlers used in later initializations.
|
2024-05-30 11:55:57 +02:00
|
|
|
mediaManager := media.NewManager(state)
|
2021-10-11 14:37:33 +02:00
|
|
|
oauthServer := oauth.New(ctx, dbService)
|
2024-05-30 11:55:57 +02:00
|
|
|
typeConverter := typeutils.NewConverter(state)
|
|
|
|
visFilter := visibility.NewFilter(state)
|
2024-07-24 13:27:42 +02:00
|
|
|
intFilter := interaction.NewFilter(state)
|
2024-05-30 11:55:57 +02:00
|
|
|
spamFilter := spam.NewFilter(state)
|
|
|
|
federatingDB := federatingdb.New(state, typeConverter, visFilter, spamFilter)
|
|
|
|
transportController := transport.NewController(state, federatingDB, &federation.Clock{}, client)
|
2024-07-24 13:27:42 +02:00
|
|
|
federator := federation.NewFederator(
|
|
|
|
state,
|
|
|
|
federatingDB,
|
|
|
|
transportController,
|
|
|
|
typeConverter,
|
|
|
|
visFilter,
|
|
|
|
intFilter,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
// Initialize both home / list timelines.
|
2023-05-25 10:37:38 +02:00
|
|
|
state.Timelines.Home = timeline.NewManager(
|
2024-05-30 11:55:57 +02:00
|
|
|
tlprocessor.HomeTimelineGrab(state),
|
|
|
|
tlprocessor.HomeTimelineFilter(state, visFilter),
|
|
|
|
tlprocessor.HomeTimelineStatusPrepare(state, typeConverter),
|
2023-05-25 10:37:38 +02:00
|
|
|
tlprocessor.SkipInsert(),
|
|
|
|
)
|
|
|
|
if err := state.Timelines.Home.Start(); err != nil {
|
|
|
|
return fmt.Errorf("error starting home timeline: %s", err)
|
|
|
|
}
|
|
|
|
state.Timelines.List = timeline.NewManager(
|
2024-05-30 11:55:57 +02:00
|
|
|
tlprocessor.ListTimelineGrab(state),
|
|
|
|
tlprocessor.ListTimelineFilter(state, visFilter),
|
|
|
|
tlprocessor.ListTimelineStatusPrepare(state, typeConverter),
|
2023-05-25 10:37:38 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-04-26 14:50:46 +02:00
|
|
|
// Start the job scheduler
|
|
|
|
// (this is required for cleaner).
|
|
|
|
state.Workers.StartScheduler()
|
|
|
|
|
|
|
|
// Add a task to the scheduler to sweep caches.
|
|
|
|
// Frequency = 1 * minute
|
|
|
|
// Threshold = 60% capacity
|
2024-05-30 11:55:57 +02:00
|
|
|
if !state.Workers.Scheduler.AddRecurring(
|
2024-04-26 14:50:46 +02:00
|
|
|
"@cachesweep", // id
|
|
|
|
time.Time{}, // start
|
|
|
|
time.Minute, // freq
|
|
|
|
func(context.Context, time.Time) {
|
|
|
|
state.Caches.Sweep(60)
|
|
|
|
},
|
2024-05-30 11:55:57 +02:00
|
|
|
) {
|
|
|
|
return fmt.Errorf("error scheduling cache sweep: %w", err)
|
|
|
|
}
|
2024-04-26 14:50:46 +02:00
|
|
|
|
|
|
|
// Create background cleaner.
|
2024-05-30 11:55:57 +02:00
|
|
|
cleaner := cleaner.New(state)
|
2023-10-30 18:35:11 +01:00
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
// Now schedule background cleaning tasks.
|
|
|
|
if err := cleaner.ScheduleJobs(); err != nil {
|
|
|
|
return fmt.Errorf("error scheduling cleaner jobs: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-04-26 14:50:46 +02:00
|
|
|
// Create the processor using all the
|
|
|
|
// other services we've created so far.
|
2024-07-30 13:58:31 +02:00
|
|
|
process = processing.NewProcessor(
|
2023-10-30 18:35:11 +01:00
|
|
|
cleaner,
|
|
|
|
typeConverter,
|
|
|
|
federator,
|
|
|
|
oauthServer,
|
|
|
|
mediaManager,
|
2024-05-30 11:55:57 +02:00
|
|
|
state,
|
2023-10-30 18:35:11 +01:00
|
|
|
emailSender,
|
2024-07-24 13:27:42 +02:00
|
|
|
visFilter,
|
|
|
|
intFilter,
|
2023-10-30 18:35:11 +01:00
|
|
|
)
|
2023-05-25 10:37:38 +02:00
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
// Initialize the specialized workers pools.
|
2024-04-26 14:50:46 +02:00
|
|
|
state.Workers.Client.Init(messages.ClientMsgIndices())
|
|
|
|
state.Workers.Federator.Init(messages.FederatorMsgIndices())
|
|
|
|
state.Workers.Delivery.Init(client)
|
2024-07-30 13:58:31 +02:00
|
|
|
state.Workers.Client.Process = process.Workers().ProcessFromClientAPI
|
|
|
|
state.Workers.Federator.Process = process.Workers().ProcessFromFediAPI
|
2023-03-01 19:26:53 +01:00
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
// Now start workers!
|
2024-04-26 14:50:46 +02:00
|
|
|
state.Workers.Start()
|
2023-09-04 15:55:17 +02:00
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
// Schedule notif tasks for all existing poll expiries.
|
2024-07-30 13:58:31 +02:00
|
|
|
if err := process.Polls().ScheduleAll(ctx); err != nil {
|
2023-11-08 15:32:17 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-07-23 21:44:31 +02:00
|
|
|
// Run advanced migrations.
|
2024-07-30 13:58:31 +02:00
|
|
|
if err := process.AdvancedMigrations().Migrate(ctx); err != nil {
|
2024-07-23 21:44:31 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-02 13:10:50 +01:00
|
|
|
/*
|
|
|
|
HTTP router initialization
|
|
|
|
*/
|
|
|
|
|
2024-05-29 13:21:04 +02:00
|
|
|
route, 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()),
|
2024-05-30 11:55:57 +02: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
|
2024-05-29 13:21:04 +02:00
|
|
|
route.AttachGlobalMiddleware(middlewares...)
|
2023-01-02 13:10:50 +01:00
|
|
|
|
|
|
|
// attach global no route / 404 handler to the router
|
2024-05-29 13:21:04 +02:00
|
|
|
route.AttachNoRouteHandler(func(c *gin.Context) {
|
2024-07-30 13:58:31 +02:00
|
|
|
apiutil.ErrorHandler(c, gtserror.NewErrorNotFound(errors.New(http.StatusText(http.StatusNotFound))), process.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 (
|
2024-07-30 13:58:31 +02:00
|
|
|
authModule = api.NewAuth(dbService, process, idp, routerSession, sessionName) // auth/oauth paths
|
|
|
|
clientModule = api.NewClient(state, process) // api client endpoints
|
|
|
|
metricsModule = api.NewMetrics() // Metrics endpoints
|
|
|
|
healthModule = api.NewHealth(dbService.Ready) // Health check endpoints
|
|
|
|
fileserverModule = api.NewFileserver(process) // fileserver endpoints
|
|
|
|
wellKnownModule = api.NewWellKnown(process) // .well-known endpoints
|
|
|
|
nodeInfoModule = api.NewNodeInfo(process) // nodeinfo endpoint
|
|
|
|
activityPubModule = api.NewActivityPub(dbService, process) // ActivityPub endpoints
|
|
|
|
webModule = web.New(dbService, process) // 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()
|
2024-07-30 13:58:31 +02:00
|
|
|
clThrottle := middleware.Throttle(cpuMultiplier, retryAfter) // client api
|
|
|
|
s2sThrottle := middleware.Throttle(cpuMultiplier, retryAfter)
|
|
|
|
// server-to-server (AP)
|
|
|
|
fsThrottle := middleware.Throttle(cpuMultiplier, retryAfter) // fileserver / web templates / emojis
|
|
|
|
pkThrottle := middleware.Throttle(cpuMultiplier, retryAfter) // throttle public key endpoint separately
|
2023-01-04 11:57:59 +01:00
|
|
|
|
2024-08-25 13:25:32 +02:00
|
|
|
// Gzip middleware is applied to all endpoints except
|
|
|
|
// fileserver (compression too expensive for those),
|
|
|
|
// health (which really doesn't need compression), and
|
|
|
|
// metrics (which does its own compression handling that
|
|
|
|
// is rather annoying to neatly override).
|
|
|
|
gzip := middleware.Gzip()
|
2023-01-04 11:57:59 +01:00
|
|
|
|
|
|
|
// these should be routed in order;
|
|
|
|
// apply throttling *after* rate limiting
|
2024-05-29 13:21:04 +02:00
|
|
|
authModule.Route(route, clLimit, clThrottle, gzip)
|
|
|
|
clientModule.Route(route, clLimit, clThrottle, gzip)
|
2024-08-25 13:25:32 +02:00
|
|
|
metricsModule.Route(route, clLimit, clThrottle)
|
2024-05-29 13:21:04 +02:00
|
|
|
healthModule.Route(route, clLimit, clThrottle)
|
|
|
|
fileserverModule.Route(route, fsMainLimit, fsThrottle)
|
|
|
|
fileserverModule.RouteEmojis(route, instanceAccount.ID, fsEmojiLimit, fsThrottle)
|
|
|
|
wellKnownModule.Route(route, gzip, s2sLimit, s2sThrottle)
|
|
|
|
nodeInfoModule.Route(route, s2sLimit, s2sThrottle, gzip)
|
|
|
|
activityPubModule.Route(route, s2sLimit, s2sThrottle, gzip)
|
|
|
|
activityPubModule.RoutePublicKey(route, s2sLimit, pkThrottle, gzip)
|
|
|
|
webModule.Route(route, fsMainLimit, fsThrottle, gzip)
|
|
|
|
|
|
|
|
// Finally start the main http server!
|
|
|
|
if err := route.Start(); err != nil {
|
|
|
|
return fmt.Errorf("error starting router: %w", err)
|
2021-04-01 20:46:45 +02:00
|
|
|
}
|
2021-03-05 18:31:12 +01:00
|
|
|
|
2024-07-30 13:58:31 +02:00
|
|
|
// Fill worker queues from persisted task data in database.
|
|
|
|
if err := process.Admin().FillWorkerQueues(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error filling worker queues: %w", err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-12 11:39:47 +02:00
|
|
|
|
|
|
|
func setLimits(ctx context.Context) {
|
|
|
|
if _, err := maxprocs.Set(maxprocs.Logger(nil)); err != nil {
|
|
|
|
log.Warnf(ctx, "could not set CPU limits from cgroup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := memlimit.SetGoMemLimitWithOpts(); err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "cgroup mountpoint does not exist") {
|
|
|
|
log.Warnf(ctx, "could not set Memory limits from cgroup: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-26 18:21:29 +02:00
|
|
|
func compileWASM(ctx context.Context) error {
|
2024-08-03 16:40:26 +02:00
|
|
|
// Use admin-set ffmpeg pool size, and fall
|
|
|
|
// back to GOMAXPROCS if number 0 or less.
|
|
|
|
ffPoolSize := config.GetMediaFfmpegPoolSize()
|
|
|
|
if ffPoolSize <= 0 {
|
|
|
|
ffPoolSize = runtime.GOMAXPROCS(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ffmpeg.InitFfmpeg(ctx, ffPoolSize); err != nil {
|
2024-07-12 11:39:47 +02:00
|
|
|
return gtserror.Newf("error compiling ffmpeg: %w", err)
|
|
|
|
}
|
2024-08-03 16:40:26 +02:00
|
|
|
|
|
|
|
if err := ffmpeg.InitFfprobe(ctx, ffPoolSize); err != nil {
|
2024-07-12 11:39:47 +02:00
|
|
|
return gtserror.Newf("error compiling ffprobe: %w", err)
|
|
|
|
}
|
2024-08-03 16:40:26 +02:00
|
|
|
|
2024-07-12 11:39:47 +02:00
|
|
|
return nil
|
|
|
|
}
|