server

package
v0.17.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2024 License: AGPL-3.0 Imports: 45 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Start action.GTSAction = func(ctx context.Context) error {

	setLimits(ctx)

	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.
		state   = new(state.State)
		route   *router.Router
		process *processing.Processor
	)

	defer func() {

		state.Caches.Stop()

		if route != nil {

			if err := route.Stop(); err != nil {
				log.Errorf(ctx, "error stopping router: %v", err)
			}
		}

		state.Workers.Stop()

		if state.Timelines.Home != nil {

			if err := state.Timelines.Home.Stop(); err != nil {
				log.Errorf(ctx, "error stopping home timeline: %v", err)
			}
		}

		if state.Timelines.List != nil {

			if err := state.Timelines.List.Stop(); err != nil {
				log.Errorf(ctx, "error stopping list timeline: %v", err)
			}
		}

		if process != nil {
			const timeout = time.Minute

			ctx := context.WithoutCancel(ctx)
			ctx, cncl := context.WithTimeout(ctx, timeout)
			defer cncl()

			if err := process.Admin().PersistWorkerQueues(ctx); err != nil {
				log.Errorf(ctx, "error persisting worker queues: %v", err)
			}
		}

		if state.DB != nil {

			if err := state.DB.Close(); err != nil {
				log.Errorf(ctx, "error stopping database: %v", err)
			}
		}

		log.Info(ctx, "done! exiting...")
	}()

	if err := tracing.Initialize(); err != nil {
		return fmt.Errorf("error initializing tracing: %w", err)
	}

	state.Caches.Init()
	state.Caches.Start()

	dbService, err := bundb.NewBunDBService(ctx, state)
	if err != nil {
		return fmt.Errorf("error creating dbservice: %s", err)
	}

	state.DB = dbService

	if err := dbService.CreateInstanceAccount(ctx); err != nil {
		return fmt.Errorf("error creating instance account: %s", err)
	}
	if err := dbService.CreateInstanceInstance(ctx); err != nil {
		return fmt.Errorf("error creating instance instance: %s", err)
	}
	if err := dbService.CreateInstanceApplication(ctx); err != nil {
		return fmt.Errorf("error creating instance application: %s", err)
	}

	instanceAccount, err := dbService.GetInstanceAccount(ctx, "")
	if err != nil {
		return fmt.Errorf("error retrieving instance account: %w", err)
	}

	state.Storage, err = gtsstorage.AutoConfig()
	if err != nil {
		return fmt.Errorf("error opening storage backend: %w", err)
	}

	client := httpclient.New(httpclient.Config{
		AllowRanges:           config.MustParseIPPrefixes(config.GetHTTPClientAllowIPs()),
		BlockRanges:           config.MustParseIPPrefixes(config.GetHTTPClientBlockIPs()),
		Timeout:               config.GetHTTPClientTimeout(),
		TLSInsecureSkipVerify: config.GetHTTPClientTLSInsecureSkipVerify(),
	})

	log.Info(ctx, "compiling WebAssembly")
	if err := compileWASM(ctx); err != nil {
		return err
	}

	mediaManager := media.NewManager(state)
	oauthServer := oauth.New(ctx, dbService)
	typeConverter := typeutils.NewConverter(state)
	visFilter := visibility.NewFilter(state)
	intFilter := interaction.NewFilter(state)
	spamFilter := spam.NewFilter(state)
	federatingDB := federatingdb.New(state, typeConverter, visFilter, intFilter, spamFilter)
	transportController := transport.NewController(state, federatingDB, &federation.Clock{}, client)
	federator := federation.NewFederator(
		state,
		federatingDB,
		transportController,
		typeConverter,
		visFilter,
		intFilter,
		mediaManager,
	)

	// Decide whether to create a noop email
	// sender (won't send emails) or a real one.
	var emailSender email.Sender
	if smtpHost := config.GetSMTPHost(); smtpHost != "" {

		emailSender, err = email.NewSender()
		if err != nil {
			return fmt.Errorf("error creating email sender: %s", err)
		}
	} else {

		emailSender, err = email.NewNoopSender(nil)
		if err != nil {
			return fmt.Errorf("error creating noop email sender: %s", err)
		}
	}

	state.Timelines.Home = timeline.NewManager(
		tlprocessor.HomeTimelineGrab(state),
		tlprocessor.HomeTimelineFilter(state, visFilter),
		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, visFilter),
		tlprocessor.ListTimelineStatusPrepare(state, typeConverter),
		tlprocessor.SkipInsert(),
	)
	if err := state.Timelines.List.Start(); err != nil {
		return fmt.Errorf("error starting list timeline: %s", err)
	}

	state.Workers.StartScheduler()

	if !state.Workers.Scheduler.AddRecurring(
		"@cachesweep",
		time.Time{},
		time.Minute,
		func(context.Context, time.Time) {
			state.Caches.Sweep(60)
		},
	) {
		return fmt.Errorf("error scheduling cache sweep: %w", err)
	}

	cleaner := cleaner.New(state)

	if err := cleaner.ScheduleJobs(); err != nil {
		return fmt.Errorf("error scheduling cleaner jobs: %w", err)
	}

	process = processing.NewProcessor(
		cleaner,
		typeConverter,
		federator,
		oauthServer,
		mediaManager,
		state,
		emailSender,
		visFilter,
		intFilter,
	)

	state.Workers.Client.Init(messages.ClientMsgIndices())
	state.Workers.Federator.Init(messages.FederatorMsgIndices())
	state.Workers.Delivery.Init(client)
	state.Workers.Client.Process = process.Workers().ProcessFromClientAPI
	state.Workers.Federator.Process = process.Workers().ProcessFromFediAPI

	state.Workers.Start()

	if err := process.Polls().ScheduleAll(ctx); err != nil {
		return fmt.Errorf("error scheduling poll expiries: %w", err)
	}

	if err := metrics.Initialize(state.DB); err != nil {
		return fmt.Errorf("error initializing metrics: %w", err)
	}

	if err := process.AdvancedMigrations().Migrate(ctx); err != nil {
		return err
	}

	route, err = router.New(ctx)
	if err != nil {
		return fmt.Errorf("error creating router: %s", err)
	}

	middlewares := make([]gin.HandlerFunc, 1)

	middlewares[0] = middleware.AddRequestID(config.GetRequestIDHeader())

	if config.GetTracingEnabled() {
		middlewares = append(middlewares, tracing.InstrumentGin())
	}

	if config.GetMetricsEnabled() {
		middlewares = append(middlewares, metrics.InstrumentGin())
	}

	middlewares = append(middlewares, []gin.HandlerFunc{

		middleware.Logger(config.GetLogClientIP()),
		middleware.HeaderFilter(state),
		middleware.UserAgent(),
		middleware.CORS(),
		middleware.ExtraHeaders(),
	}...)

	cspExtraURIs := make([]string, 0)

	storageCSPUri, err := state.Storage.ProbeCSPUri(ctx)
	if err != nil {
		return fmt.Errorf("error deriving Content-Security-Policy uri from storage: %w", err)
	}

	if storageCSPUri != "" {
		cspExtraURIs = append(cspExtraURIs, storageCSPUri)
	}

	cspExtraURIs = append(cspExtraURIs, config.GetAdvancedCSPExtraURIs()...)

	middlewares = append(middlewares, middleware.ContentSecurityPolicy(cspExtraURIs...))

	route.AttachGlobalMiddleware(middlewares...)

	route.AttachNoRouteHandler(func(c *gin.Context) {
		apiutil.ErrorHandler(c, gtserror.NewErrorNotFound(errors.New(http.StatusText(http.StatusNotFound))), process.InstanceGetV1)
	})

	// 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)
		}
	}

	routerSession, err := dbService.GetSession(ctx)
	if err != nil {
		return fmt.Errorf("error retrieving router session for session middleware: %w", err)
	}

	sessionName, err := middleware.SessionName()
	if err != nil {
		return fmt.Errorf("error generating session name for session middleware: %w", err)
	}

	var (
		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
	)

	rlLimit := config.GetAdvancedRateLimitRequests()
	rlExceptions := config.GetAdvancedRateLimitExceptions()
	clLimit := middleware.RateLimit(rlLimit, rlExceptions)
	s2sLimit := middleware.RateLimit(rlLimit, rlExceptions)
	fsMainLimit := middleware.RateLimit(rlLimit, rlExceptions)
	fsEmojiLimit := middleware.RateLimit(rlLimit*2, rlExceptions)

	cpuMultiplier := config.GetAdvancedThrottlingMultiplier()
	retryAfter := config.GetAdvancedThrottlingRetryAfter()
	clThrottle := middleware.Throttle(cpuMultiplier, retryAfter)
	s2sThrottle := middleware.Throttle(cpuMultiplier, retryAfter)

	fsThrottle := middleware.Throttle(cpuMultiplier, retryAfter)
	pkThrottle := middleware.Throttle(cpuMultiplier, retryAfter)

	gzip := middleware.Gzip()

	authModule.Route(route, clLimit, clThrottle, gzip)
	clientModule.Route(route, clLimit, clThrottle, gzip)
	metricsModule.Route(route, clLimit, clThrottle)
	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)

	if err := route.Start(); err != nil {
		return fmt.Errorf("error starting router: %w", err)
	}

	if err := process.Admin().FillWorkerQueues(ctx); err != nil {
		return fmt.Errorf("error filling worker queues: %w", err)
	}

	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
	sig := <-sigs
	log.Infof(ctx, "received signal %s, shutting down", sig)

	return nil
}

Start creates and starts a gotosocial server

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL