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-04-19 19:42:19 +02:00
|
|
|
package testrig
|
|
|
|
|
|
|
|
import (
|
2021-05-08 14:25:55 +02:00
|
|
|
"bytes"
|
2021-04-19 19:42:19 +02:00
|
|
|
"context"
|
2023-01-02 13:10:50 +01:00
|
|
|
"errors"
|
2021-04-19 19:42:19 +02:00
|
|
|
"fmt"
|
2022-09-28 19:30:40 +02:00
|
|
|
"io"
|
2021-05-08 14:25:55 +02:00
|
|
|
"net/http"
|
2021-04-19 19:42:19 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
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/config"
|
2021-04-19 19:42:19 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gotosocial"
|
2023-01-02 13:10:50 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
2023-11-17 11:35:28 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/language"
|
2022-07-19 10:47:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
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"
|
2021-07-23 10:36:28 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oidc"
|
2023-05-25 10:37:38 +02:00
|
|
|
tlprocessor "github.com/superseriousbusiness/gotosocial/internal/processing/timeline"
|
2023-03-01 19:26:53 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/state"
|
2022-07-03 12:08:30 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/storage"
|
2023-05-25 10:37:38 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/timeline"
|
2023-05-09 19:19:48 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/tracing"
|
2023-09-23 18:44:11 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
|
2023-05-25 10:37:38 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/visibility"
|
2021-06-21 20:40:56 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/web"
|
2021-06-21 12:27:23 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/testrig"
|
2021-04-19 19:42:19 +02:00
|
|
|
)
|
|
|
|
|
2021-06-21 12:27:23 +02:00
|
|
|
// Start creates and starts a gotosocial testrig server
|
2021-12-07 13:31:39 +01:00
|
|
|
var Start action.GTSAction = func(ctx context.Context) error {
|
2023-03-01 19:26:53 +01:00
|
|
|
var state state.State
|
|
|
|
|
2021-12-07 13:31:39 +01:00
|
|
|
testrig.InitTestConfig()
|
2021-10-31 15:46:23 +01:00
|
|
|
testrig.InitTestLog()
|
|
|
|
|
2023-11-17 11:35:28 +01:00
|
|
|
parsedLangs, err := language.InitLangs(config.GetInstanceLanguages().TagStrs())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error initializing languages: %w", err)
|
|
|
|
}
|
|
|
|
config.SetInstanceLanguages(parsedLangs)
|
|
|
|
|
2023-05-09 19:19:48 +02:00
|
|
|
if err := tracing.Initialize(); err != nil {
|
|
|
|
return fmt.Errorf("error initializing tracing: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-11-20 16:43:55 +01:00
|
|
|
if err := metrics.Initialize(); err != nil {
|
|
|
|
return fmt.Errorf("error initializing metrics: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-05-21 14:44:10 +02:00
|
|
|
// Initialize caches and database
|
|
|
|
state.DB = testrig.NewTestDB(&state)
|
|
|
|
|
|
|
|
// New test db inits caches so we don't need to do
|
|
|
|
// that twice, we can just start the initialized caches.
|
2023-03-01 19:26:53 +01:00
|
|
|
state.Caches.Start()
|
|
|
|
defer state.Caches.Stop()
|
|
|
|
|
|
|
|
testrig.StandardDBSetup(state.DB, nil)
|
|
|
|
|
2022-07-03 12:08:30 +02:00
|
|
|
if os.Getenv("GTS_STORAGE_BACKEND") == "s3" {
|
2023-08-20 13:35:55 +02:00
|
|
|
var err error
|
|
|
|
state.Storage, err = storage.NewS3Storage()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error initializing storage: %w", err)
|
|
|
|
}
|
2022-07-03 12:08:30 +02:00
|
|
|
} else {
|
2023-03-01 19:26:53 +01:00
|
|
|
state.Storage = testrig.NewInMemoryStorage()
|
2022-07-03 12:08:30 +02:00
|
|
|
}
|
2023-03-01 19:26:53 +01:00
|
|
|
testrig.StandardStorageSetup(state.Storage, "./testrig/media")
|
2021-06-21 12:27:23 +02:00
|
|
|
|
2023-03-01 19:26:53 +01:00
|
|
|
// Initialize workers.
|
|
|
|
state.Workers.Start()
|
|
|
|
defer state.Workers.Stop()
|
2022-04-28 14:23:11 +02:00
|
|
|
|
2021-06-21 12:27:23 +02:00
|
|
|
// build backend handlers
|
2023-03-01 19:26:53 +01:00
|
|
|
transportController := testrig.NewTestTransportController(&state, testrig.NewMockHTTPClient(func(req *http.Request) (*http.Response, error) {
|
2022-09-28 19:30:40 +02:00
|
|
|
r := io.NopCloser(bytes.NewReader([]byte{}))
|
2021-05-08 14:25:55 +02:00
|
|
|
return &http.Response{
|
|
|
|
StatusCode: 200,
|
|
|
|
Body: r,
|
|
|
|
}, nil
|
2023-03-01 19:26:53 +01:00
|
|
|
}, ""))
|
|
|
|
mediaManager := testrig.NewTestMediaManager(&state)
|
|
|
|
federator := testrig.NewTestFederator(&state, transportController, mediaManager)
|
2021-06-21 12:27:23 +02:00
|
|
|
|
2021-10-31 15:46:23 +01:00
|
|
|
emailSender := testrig.NewEmailSender("./web/template/", nil)
|
2023-09-23 18:44:11 +02:00
|
|
|
typeConverter := typeutils.NewConverter(&state)
|
2023-05-25 10:37:38 +02:00
|
|
|
filter := visibility.NewFilter(&state)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2021-10-31 15:46:23 +01:00
|
|
|
|
2023-05-25 10:37:38 +02:00
|
|
|
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-05-08 14:25:55 +02:00
|
|
|
}
|
2021-04-19 19:42:19 +02:00
|
|
|
|
2023-05-25 10:37:38 +02:00
|
|
|
processor := testrig.NewTestProcessor(&state, federator, emailSender, mediaManager)
|
|
|
|
|
2023-01-02 13:10:50 +01:00
|
|
|
/*
|
|
|
|
HTTP router initialization
|
|
|
|
*/
|
|
|
|
|
2023-03-01 19:26:53 +01:00
|
|
|
router := testrig.NewTestRouter(state.DB)
|
2023-05-09 19:19:48 +02:00
|
|
|
middlewares := []gin.HandlerFunc{
|
|
|
|
middleware.AddRequestID(config.GetRequestIDHeader()), // requestID middleware must run before tracing
|
|
|
|
}
|
|
|
|
if config.GetTracingEnabled() {
|
|
|
|
middlewares = append(middlewares, tracing.InstrumentGin())
|
|
|
|
}
|
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-05-21 17:12:47 +02:00
|
|
|
middleware.Logger(config.GetLogClientIP()),
|
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-07-23 10:36:28 +02:00
|
|
|
}
|
|
|
|
|
2023-03-01 19:26:53 +01:00
|
|
|
routerSession, err := state.DB.GetSession(ctx)
|
2023-01-02 13:10:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error retrieving router session for session middleware: %w", err)
|
2021-04-19 19:42:19 +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)
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 13:10:50 +01:00
|
|
|
var (
|
2023-03-01 19:26:53 +01:00
|
|
|
authModule = api.NewAuth(state.DB, processor, idp, routerSession, sessionName) // auth/oauth paths
|
|
|
|
clientModule = api.NewClient(state.DB, processor) // api client endpoints
|
2023-11-23 19:10:51 +01:00
|
|
|
metricsModule = api.NewMetrics() // Metrics endpoints
|
2023-03-01 19:26:53 +01:00
|
|
|
fileserverModule = api.NewFileserver(processor) // fileserver endpoints
|
|
|
|
wellKnownModule = api.NewWellKnown(processor) // .well-known endpoints
|
|
|
|
nodeInfoModule = api.NewNodeInfo(processor) // nodeinfo endpoint
|
|
|
|
activityPubModule = api.NewActivityPub(state.DB, processor) // ActivityPub endpoints
|
|
|
|
webModule = web.New(state.DB, processor) // web pages + user profiles + settings panels etc
|
2023-01-02 13:10:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// these should be routed in order
|
|
|
|
authModule.Route(router)
|
|
|
|
clientModule.Route(router)
|
2023-11-23 19:10:51 +01:00
|
|
|
metricsModule.Route(router)
|
2023-01-02 13:10:50 +01:00
|
|
|
fileserverModule.Route(router)
|
|
|
|
wellKnownModule.Route(router)
|
|
|
|
nodeInfoModule.Route(router)
|
|
|
|
activityPubModule.Route(router)
|
2023-02-08 15:10:56 +01:00
|
|
|
activityPubModule.RoutePublicKey(router)
|
2023-01-02 13:10:50 +01:00
|
|
|
webModule.Route(router)
|
|
|
|
|
2023-10-30 18:35:11 +01:00
|
|
|
cleaner := cleaner.New(&state)
|
2021-04-19 19:42:19 +02:00
|
|
|
|
2023-10-30 18:35:11 +01:00
|
|
|
gts := gotosocial.NewServer(state.DB, router, cleaner)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err := gts.Start(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error starting gotosocial service: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// catch shutdown signals from the operating system
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
|
|
|
|
sig := <-sigs
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Infof(ctx, "received signal %s, shutting down", sig)
|
2021-04-19 19:42:19 +02:00
|
|
|
|
2023-03-01 19:26:53 +01:00
|
|
|
testrig.StandardDBTeardown(state.DB)
|
|
|
|
testrig.StandardStorageTeardown(state.Storage)
|
2021-04-19 19:42:19 +02:00
|
|
|
|
|
|
|
// close down all running services in order
|
|
|
|
if err := gts.Stop(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error closing gotosocial service: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:02:29 +01:00
|
|
|
log.Info(ctx, "done! exiting...")
|
2021-04-19 19:42:19 +02:00
|
|
|
return nil
|
|
|
|
}
|