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-05-30 13:12:00 +02:00
|
|
|
package server
|
2021-03-04 14:38:18 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-05-08 14:25:55 +02:00
|
|
|
"net/http"
|
2021-03-04 14:38:18 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2021-11-13 12:29:08 +01:00
|
|
|
"codeberg.org/gruf/go-store/kv"
|
2021-03-04 14:38:18 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-12-07 13:31:39 +01:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"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-05-31 17:36:35 +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-05-31 17:36:35 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/filter"
|
2021-05-15 11:58:11 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/followrequest"
|
2021-05-09 14:06:06 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/instance"
|
2021-05-31 17:36:35 +02:00
|
|
|
"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-05-27 16:06:24 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/notification"
|
2021-05-29 19:36:54 +02:00
|
|
|
"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-19 11:18:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/streaming"
|
2021-05-21 23:04:59 +02:00
|
|
|
"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-05-09 20:34:27 +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"
|
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"
|
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"
|
2021-06-13 18:42:28 +02:00
|
|
|
timelineprocessing "github.com/superseriousbusiness/gotosocial/internal/timeline"
|
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 {
|
|
|
|
dbService, err := bundb.NewBunDBService(ctx)
|
2021-03-04 14:38:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating dbservice: %s", err)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-12-07 13:31:39 +01:00
|
|
|
federatingDB := federatingdb.New(dbService)
|
2021-05-21 15:48:26 +02:00
|
|
|
|
2021-12-07 13:31:39 +01:00
|
|
|
router, err := router.New(ctx, dbService)
|
2021-04-01 20:46:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating router: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-10-31 15:46:23 +01:00
|
|
|
// build converters and util
|
2021-12-07 13:31:39 +01:00
|
|
|
typeConverter := typeutils.NewConverter(dbService)
|
|
|
|
timelineManager := timelineprocessing.NewManager(dbService, typeConverter)
|
2021-10-31 15:46:23 +01:00
|
|
|
|
2021-09-11 21:18:06 +02:00
|
|
|
// Open the storage backend
|
2021-12-20 15:19:53 +01:00
|
|
|
storageBasePath := viper.GetString(config.Keys.StorageLocalBasePath)
|
2021-12-07 13:31:39 +01:00
|
|
|
storage, err := kv.OpenFile(storageBasePath, nil)
|
2021-04-01 20:46:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating storage backend: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// build backend handlers
|
2021-12-07 13:31:39 +01:00
|
|
|
mediaHandler := media.New(dbService, storage)
|
2021-10-11 14:37:33 +02:00
|
|
|
oauthServer := oauth.New(ctx, dbService)
|
2021-12-07 13:31:39 +01:00
|
|
|
transportController := transport.NewController(dbService, &federation.Clock{}, http.DefaultClient)
|
|
|
|
federator := federation.NewFederator(dbService, federatingDB, transportController, typeConverter, mediaHandler)
|
2021-10-31 15:46:23 +01:00
|
|
|
|
|
|
|
// decide whether to create a noop email sender (won't send emails) or a real one
|
|
|
|
var emailSender email.Sender
|
2021-12-07 13:31:39 +01:00
|
|
|
smtpHost := viper.GetString(config.Keys.SMTPHost)
|
|
|
|
if smtpHost != "" {
|
2021-10-31 15:46:23 +01:00
|
|
|
// host is defined so 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 {
|
|
|
|
// no host is defined so 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create and start the message processor using the other services we've created so far
|
2021-12-07 13:31:39 +01:00
|
|
|
processor := processing.NewProcessor(typeConverter, federator, oauthServer, mediaHandler, storage, timelineManager, dbService, emailSender)
|
2021-08-25 15:34:33 +02:00
|
|
|
if err := processor.Start(ctx); 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)
|
|
|
|
}
|
|
|
|
|
2021-04-01 20:46:45 +02:00
|
|
|
// build client api modules
|
2021-12-07 13:31:39 +01:00
|
|
|
authModule := auth.New(dbService, oauthServer, idp)
|
|
|
|
accountModule := account.New(processor)
|
|
|
|
instanceModule := instance.New(processor)
|
|
|
|
appsModule := app.New(processor)
|
|
|
|
followRequestsModule := followrequest.New(processor)
|
|
|
|
webfingerModule := webfinger.New(processor)
|
|
|
|
nodeInfoModule := nodeinfo.New(processor)
|
|
|
|
webBaseModule := web.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-01 20:46:45 +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,
|
|
|
|
|
|
|
|
// now everything else
|
2021-06-21 19:46:10 +02:00
|
|
|
webBaseModule,
|
2021-04-01 20:46:45 +02:00
|
|
|
accountModule,
|
2021-05-09 14:06:06 +02:00
|
|
|
instanceModule,
|
2021-04-01 20:46:45 +02:00
|
|
|
appsModule,
|
2021-05-15 11:58:11 +02:00
|
|
|
followRequestsModule,
|
2021-04-19 19:42:19 +02:00
|
|
|
mm,
|
|
|
|
fileServerModule,
|
|
|
|
adminModule,
|
|
|
|
statusModule,
|
2021-05-09 20:34:27 +02:00
|
|
|
webfingerModule,
|
2021-06-24 14:26:08 +02:00
|
|
|
nodeInfoModule,
|
2021-05-09 20:34:27 +02:00
|
|
|
usersModule,
|
2021-05-21 23:04:59 +02:00
|
|
|
timelineModule,
|
2021-05-27 16:06:24 +02:00
|
|
|
notificationModule,
|
2021-05-29 19:36:54 +02:00
|
|
|
searchModule,
|
2021-05-31 17:36:35 +02:00
|
|
|
filtersModule,
|
|
|
|
emojiModule,
|
|
|
|
listsModule,
|
2021-06-19 11:18:55 +02:00
|
|
|
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-01 20:46:45 +02:00
|
|
|
}
|
|
|
|
|
2021-05-08 14:25:55 +02:00
|
|
|
for _, m := range apis {
|
2021-04-01 20:46:45 +02:00
|
|
|
if err := m.Route(router); err != nil {
|
|
|
|
return fmt.Errorf("routing error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 13:31:39 +01:00
|
|
|
gts, err := gotosocial.NewServer(dbService, router, federator)
|
2021-04-01 20:46:45 +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)
|
|
|
|
}
|
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)
|
|
|
|
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
|
|
|
|
sig := <-sigs
|
2021-10-11 14:37:33 +02:00
|
|
|
logrus.Infof("received signal %s, shutting down", sig)
|
2021-03-04 14:38:18 +01:00
|
|
|
|
|
|
|
// close down all running services in order
|
2021-04-01 20:46:45 +02:00
|
|
|
if err := gts.Stop(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error closing gotosocial service: %s", err)
|
2021-03-04 14:38:18 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 14:37:33 +02:00
|
|
|
logrus.Info("done! exiting...")
|
2021-03-04 14:38:18 +01:00
|
|
|
return nil
|
|
|
|
}
|