2021-12-07 13:31:39 +01:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2021-12-20 18:42:19 +01:00
|
|
|
Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org
|
2021-12-07 13:31:39 +01:00
|
|
|
|
|
|
|
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-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"
|
|
|
|
"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"
|
|
|
|
|
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"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/account"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/admin"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/app"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/auth"
|
2021-07-11 16:22:21 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/blocks"
|
2021-06-21 12:27:23 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/emoji"
|
2021-07-09 18:32:48 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/favourites"
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/fileserver"
|
2021-06-21 12:27:23 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/filter"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/followrequest"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/instance"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/list"
|
2021-05-08 14:25:55 +02:00
|
|
|
mediaModule "github.com/superseriousbusiness/gotosocial/internal/api/client/media"
|
2021-06-21 12:27:23 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/notification"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/search"
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/status"
|
2021-06-21 12:27:23 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/streaming"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/timeline"
|
2021-11-22 09:04:16 +01:00
|
|
|
userClient "github.com/superseriousbusiness/gotosocial/internal/api/client/user"
|
2021-06-24 14:26:08 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/s2s/nodeinfo"
|
2021-06-21 12:27:23 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/s2s/user"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/s2s/webfinger"
|
2021-05-08 14:25:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/security"
|
2022-05-15 11:16:43 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/concurrency"
|
2021-04-19 19:42:19 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gotosocial"
|
2022-07-19 10:47:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2022-04-28 14:23:11 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/messages"
|
2021-07-23 10:36:28 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oidc"
|
2022-07-03 12:08:30 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/storage"
|
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 {
|
|
|
|
testrig.InitTestConfig()
|
2021-10-31 15:46:23 +01:00
|
|
|
testrig.InitTestLog()
|
|
|
|
|
2021-06-21 12:27:23 +02:00
|
|
|
dbService := testrig.NewTestDB()
|
2021-08-10 13:32:39 +02:00
|
|
|
testrig.StandardDBSetup(dbService, nil)
|
2021-07-07 15:46:42 +02:00
|
|
|
router := testrig.NewTestRouter(dbService)
|
2022-07-03 12:08:30 +02:00
|
|
|
var storageBackend storage.Driver
|
|
|
|
if os.Getenv("GTS_STORAGE_BACKEND") == "s3" {
|
|
|
|
storageBackend = testrig.NewS3Storage()
|
|
|
|
} else {
|
|
|
|
storageBackend = testrig.NewInMemoryStorage()
|
|
|
|
}
|
2021-06-21 12:27:23 +02:00
|
|
|
testrig.StandardStorageSetup(storageBackend, "./testrig/media")
|
|
|
|
|
2022-04-28 14:23:11 +02:00
|
|
|
// Create client API and federator worker pools
|
2022-05-15 11:16:43 +02:00
|
|
|
clientWorker := concurrency.NewWorkerPool[messages.FromClientAPI](-1, -1)
|
|
|
|
fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1)
|
2022-04-28 14:23:11 +02:00
|
|
|
|
2021-06-21 12:27:23 +02:00
|
|
|
// build backend handlers
|
|
|
|
oauthServer := testrig.NewTestOauthServer(dbService)
|
|
|
|
transportController := testrig.NewTestTransportController(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
|
2022-06-11 11:01:34 +02:00
|
|
|
}, ""), dbService, fedWorker)
|
2022-01-10 18:36:09 +01:00
|
|
|
mediaManager := testrig.NewTestMediaManager(dbService, storageBackend)
|
2022-04-28 14:23:11 +02:00
|
|
|
federator := testrig.NewTestFederator(dbService, transportController, storageBackend, mediaManager, fedWorker)
|
2021-06-21 12:27:23 +02:00
|
|
|
|
2021-10-31 15:46:23 +01:00
|
|
|
emailSender := testrig.NewEmailSender("./web/template/", nil)
|
|
|
|
|
2022-04-28 14:23:11 +02:00
|
|
|
processor := testrig.NewTestProcessor(dbService, storageBackend, federator, emailSender, mediaManager, clientWorker, fedWorker)
|
|
|
|
if err := processor.Start(); err != nil {
|
2021-05-08 14:25:55 +02:00
|
|
|
return fmt.Errorf("error starting processor: %s", err)
|
|
|
|
}
|
2021-04-19 19:42:19 +02:00
|
|
|
|
2021-12-07 13:31:39 +01:00
|
|
|
idp, err := oidc.NewIDP(ctx)
|
2021-07-23 10:36:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating oidc idp: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-04-15 14:33:01 +02:00
|
|
|
// build web module
|
2022-09-04 14:41:42 +02:00
|
|
|
webModule := web.New(processor)
|
2022-04-15 14:33:01 +02:00
|
|
|
|
2021-04-19 19:42:19 +02:00
|
|
|
// build client api modules
|
2022-06-11 10:39:39 +02:00
|
|
|
authModule := auth.New(dbService, idp, processor)
|
2021-12-07 13:31:39 +01:00
|
|
|
accountModule := account.New(processor)
|
|
|
|
instanceModule := instance.New(processor)
|
|
|
|
appsModule := app.New(processor)
|
|
|
|
followRequestsModule := followrequest.New(processor)
|
|
|
|
webfingerModule := webfinger.New(processor)
|
|
|
|
nodeInfoModule := nodeinfo.New(processor)
|
|
|
|
usersModule := user.New(processor)
|
|
|
|
timelineModule := timeline.New(processor)
|
|
|
|
notificationModule := notification.New(processor)
|
|
|
|
searchModule := search.New(processor)
|
|
|
|
filtersModule := filter.New(processor)
|
|
|
|
emojiModule := emoji.New(processor)
|
|
|
|
listsModule := list.New(processor)
|
|
|
|
mm := mediaModule.New(processor)
|
|
|
|
fileServerModule := fileserver.New(processor)
|
|
|
|
adminModule := admin.New(processor)
|
|
|
|
statusModule := status.New(processor)
|
|
|
|
securityModule := security.New(dbService, oauthServer)
|
|
|
|
streamingModule := streaming.New(processor)
|
|
|
|
favouritesModule := favourites.New(processor)
|
|
|
|
blocksModule := blocks.New(processor)
|
|
|
|
userClientModule := userClient.New(processor)
|
2021-04-19 19:42:19 +02:00
|
|
|
|
2021-05-08 14:25:55 +02:00
|
|
|
apis := []api.ClientModule{
|
2021-04-19 19:42:19 +02:00
|
|
|
// modules with middleware go first
|
|
|
|
securityModule,
|
|
|
|
authModule,
|
|
|
|
|
2022-04-15 14:33:01 +02:00
|
|
|
// now the web module
|
|
|
|
webModule,
|
|
|
|
|
2021-04-19 19:42:19 +02:00
|
|
|
// now everything else
|
|
|
|
accountModule,
|
2021-06-21 12:27:23 +02:00
|
|
|
instanceModule,
|
2021-04-19 19:42:19 +02:00
|
|
|
appsModule,
|
2021-06-21 12:27:23 +02:00
|
|
|
followRequestsModule,
|
2021-04-19 19:42:19 +02:00
|
|
|
mm,
|
|
|
|
fileServerModule,
|
|
|
|
adminModule,
|
|
|
|
statusModule,
|
2021-06-21 12:27:23 +02:00
|
|
|
webfingerModule,
|
2021-06-24 14:26:08 +02:00
|
|
|
nodeInfoModule,
|
2021-06-21 12:27:23 +02:00
|
|
|
usersModule,
|
|
|
|
timelineModule,
|
|
|
|
notificationModule,
|
|
|
|
searchModule,
|
|
|
|
filtersModule,
|
|
|
|
emojiModule,
|
|
|
|
listsModule,
|
|
|
|
streamingModule,
|
2021-07-09 18:32:48 +02:00
|
|
|
favouritesModule,
|
2021-07-11 16:22:21 +02:00
|
|
|
blocksModule,
|
2021-11-22 09:04:16 +01:00
|
|
|
userClientModule,
|
2021-04-19 19:42:19 +02:00
|
|
|
}
|
|
|
|
|
2021-05-08 14:25:55 +02:00
|
|
|
for _, m := range apis {
|
2021-04-19 19:42:19 +02:00
|
|
|
if err := m.Route(router); err != nil {
|
|
|
|
return fmt.Errorf("routing error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 18:36:09 +01:00
|
|
|
gts, err := gotosocial.NewServer(dbService, router, federator, mediaManager)
|
2021-04-19 19:42:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating gotosocial service: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Infof("received signal %s, shutting down", sig)
|
2021-04-19 19:42:19 +02:00
|
|
|
|
2021-06-21 12:27:23 +02:00
|
|
|
testrig.StandardDBTeardown(dbService)
|
|
|
|
testrig.StandardStorageTeardown(storageBackend)
|
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)
|
|
|
|
}
|
|
|
|
|
2022-07-19 10:47:55 +02:00
|
|
|
log.Info("done! exiting...")
|
2021-04-19 19:42:19 +02:00
|
|
|
return nil
|
|
|
|
}
|