2021-05-30 14:12:00 +03:00
|
|
|
package server
|
2021-03-04 16:38:18 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-05-08 15:25:55 +03:00
|
|
|
"net/http"
|
2021-03-04 16:38:18 +03:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2021-11-13 14:29:08 +03:00
|
|
|
"codeberg.org/gruf/go-store/kv"
|
2021-03-04 16:38:18 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-05-08 15:25:55 +03: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 17:22:21 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/blocks"
|
2021-05-31 18:36:35 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/emoji"
|
2021-07-09 19:32:48 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/favourites"
|
2021-05-08 15:25:55 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/fileserver"
|
2021-05-31 18:36:35 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/filter"
|
2021-05-15 12:58:11 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/followrequest"
|
2021-05-09 15:06:06 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/instance"
|
2021-05-31 18:36:35 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/list"
|
2021-05-08 15:25:55 +03:00
|
|
|
mediaModule "github.com/superseriousbusiness/gotosocial/internal/api/client/media"
|
2021-05-27 17:06:24 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/notification"
|
2021-05-29 20:36:54 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/search"
|
2021-05-08 15:25:55 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/status"
|
2021-06-19 12:18:55 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/streaming"
|
2021-05-22 00:04:59 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/timeline"
|
2021-11-22 11:04:16 +03:00
|
|
|
userClient "github.com/superseriousbusiness/gotosocial/internal/api/client/user"
|
2021-06-24 15:26:08 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/s2s/nodeinfo"
|
2021-05-09 21:34:27 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/s2s/user"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/s2s/webfinger"
|
2021-05-08 15:25:55 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/security"
|
2021-05-30 14:12:00 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/cliactions"
|
2021-04-01 21:46:45 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
2021-08-25 16:34:33 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db/bundb"
|
2021-10-31 17:46:23 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/email"
|
2021-04-01 21:46:45 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/federation"
|
2021-05-27 17:06:24 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/federation/federatingdb"
|
2021-05-30 14:12:00 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gotosocial"
|
2021-04-01 21:46:45 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/media"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
2021-07-23 11:36:28 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oidc"
|
2021-05-30 14:12:00 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/processing"
|
2021-04-01 21:46:45 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/router"
|
2021-06-13 19:42:28 +03:00
|
|
|
timelineprocessing "github.com/superseriousbusiness/gotosocial/internal/timeline"
|
2021-05-08 15:25:55 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/transport"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
|
2021-06-21 20:46:10 +03:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/web"
|
2021-03-04 16:38:18 +03:00
|
|
|
)
|
|
|
|
|
2021-05-30 14:12:00 +03:00
|
|
|
// Start creates and starts a gotosocial server
|
2021-10-11 15:37:33 +03:00
|
|
|
var Start cliactions.GTSAction = func(ctx context.Context, c *config.Config) error {
|
|
|
|
dbService, err := bundb.NewBunDBService(ctx, c)
|
2021-03-04 16:38:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating dbservice: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := dbService.CreateInstanceAccount(ctx); err != nil {
|
2021-06-13 19:42:28 +03:00
|
|
|
return fmt.Errorf("error creating instance account: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := dbService.CreateInstanceInstance(ctx); err != nil {
|
2021-06-13 19:42:28 +03:00
|
|
|
return fmt.Errorf("error creating instance instance: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:37:33 +03:00
|
|
|
federatingDB := federatingdb.New(dbService, c)
|
2021-05-21 16:48:26 +03:00
|
|
|
|
2021-10-11 15:37:33 +03:00
|
|
|
router, err := router.New(ctx, c, dbService)
|
2021-04-01 21:46:45 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating router: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-10-31 17:46:23 +03:00
|
|
|
// build converters and util
|
|
|
|
typeConverter := typeutils.NewConverter(c, dbService)
|
|
|
|
timelineManager := timelineprocessing.NewManager(dbService, typeConverter, c)
|
|
|
|
|
2021-09-11 22:18:06 +03:00
|
|
|
// Open the storage backend
|
|
|
|
storage, err := kv.OpenFile(c.StorageConfig.BasePath, nil)
|
2021-04-01 21:46:45 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating storage backend: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// build backend handlers
|
2021-10-11 15:37:33 +03:00
|
|
|
mediaHandler := media.New(c, dbService, storage)
|
|
|
|
oauthServer := oauth.New(ctx, dbService)
|
|
|
|
transportController := transport.NewController(c, dbService, &federation.Clock{}, http.DefaultClient)
|
|
|
|
federator := federation.NewFederator(dbService, federatingDB, transportController, c, typeConverter, mediaHandler)
|
2021-10-31 17:46:23 +03:00
|
|
|
|
|
|
|
// decide whether to create a noop email sender (won't send emails) or a real one
|
|
|
|
var emailSender email.Sender
|
|
|
|
if c.SMTPConfig.Host != "" {
|
|
|
|
// host is defined so create a proper sender
|
|
|
|
emailSender, err = email.NewSender(c)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating email sender: %s", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no host is defined so create a noop sender
|
|
|
|
emailSender, err = email.NewNoopSender(c.TemplateConfig.BaseDir, nil)
|
|
|
|
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
|
|
|
|
processor := processing.NewProcessor(c, typeConverter, federator, oauthServer, mediaHandler, storage, timelineManager, dbService, emailSender)
|
2021-08-25 16:34:33 +03:00
|
|
|
if err := processor.Start(ctx); err != nil {
|
2021-05-08 15:25:55 +03:00
|
|
|
return fmt.Errorf("error starting processor: %s", err)
|
2021-04-19 20:42:19 +03:00
|
|
|
}
|
|
|
|
|
2021-10-11 15:37:33 +03:00
|
|
|
idp, err := oidc.NewIDP(ctx, c)
|
2021-07-23 11:36:28 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating oidc idp: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-04-01 21:46:45 +03:00
|
|
|
// build client api modules
|
2021-10-11 15:37:33 +03:00
|
|
|
authModule := auth.New(c, dbService, oauthServer, idp)
|
|
|
|
accountModule := account.New(c, processor)
|
|
|
|
instanceModule := instance.New(c, processor)
|
|
|
|
appsModule := app.New(c, processor)
|
|
|
|
followRequestsModule := followrequest.New(c, processor)
|
|
|
|
webfingerModule := webfinger.New(c, processor)
|
|
|
|
nodeInfoModule := nodeinfo.New(c, processor)
|
|
|
|
webBaseModule := web.New(c, processor)
|
|
|
|
usersModule := user.New(c, processor)
|
|
|
|
timelineModule := timeline.New(c, processor)
|
|
|
|
notificationModule := notification.New(c, processor)
|
|
|
|
searchModule := search.New(c, processor)
|
|
|
|
filtersModule := filter.New(c, processor)
|
|
|
|
emojiModule := emoji.New(c, processor)
|
|
|
|
listsModule := list.New(c, processor)
|
|
|
|
mm := mediaModule.New(c, processor)
|
|
|
|
fileServerModule := fileserver.New(c, processor)
|
|
|
|
adminModule := admin.New(c, processor)
|
|
|
|
statusModule := status.New(c, processor)
|
|
|
|
securityModule := security.New(c, dbService)
|
|
|
|
streamingModule := streaming.New(c, processor)
|
|
|
|
favouritesModule := favourites.New(c, processor)
|
|
|
|
blocksModule := blocks.New(c, processor)
|
2021-11-22 11:04:16 +03:00
|
|
|
userClientModule := userClient.New(c, processor)
|
2021-04-01 21:46:45 +03:00
|
|
|
|
2021-05-08 15:25:55 +03:00
|
|
|
apis := []api.ClientModule{
|
2021-04-19 20:42:19 +03:00
|
|
|
// modules with middleware go first
|
|
|
|
securityModule,
|
|
|
|
authModule,
|
|
|
|
|
|
|
|
// now everything else
|
2021-06-21 20:46:10 +03:00
|
|
|
webBaseModule,
|
2021-04-01 21:46:45 +03:00
|
|
|
accountModule,
|
2021-05-09 15:06:06 +03:00
|
|
|
instanceModule,
|
2021-04-01 21:46:45 +03:00
|
|
|
appsModule,
|
2021-05-15 12:58:11 +03:00
|
|
|
followRequestsModule,
|
2021-04-19 20:42:19 +03:00
|
|
|
mm,
|
|
|
|
fileServerModule,
|
|
|
|
adminModule,
|
|
|
|
statusModule,
|
2021-05-09 21:34:27 +03:00
|
|
|
webfingerModule,
|
2021-06-24 15:26:08 +03:00
|
|
|
nodeInfoModule,
|
2021-05-09 21:34:27 +03:00
|
|
|
usersModule,
|
2021-05-22 00:04:59 +03:00
|
|
|
timelineModule,
|
2021-05-27 17:06:24 +03:00
|
|
|
notificationModule,
|
2021-05-29 20:36:54 +03:00
|
|
|
searchModule,
|
2021-05-31 18:36:35 +03:00
|
|
|
filtersModule,
|
|
|
|
emojiModule,
|
|
|
|
listsModule,
|
2021-06-19 12:18:55 +03:00
|
|
|
streamingModule,
|
2021-07-09 19:32:48 +03:00
|
|
|
favouritesModule,
|
2021-07-11 17:22:21 +03:00
|
|
|
blocksModule,
|
2021-11-22 11:04:16 +03:00
|
|
|
userClientModule,
|
2021-04-01 21:46:45 +03:00
|
|
|
}
|
|
|
|
|
2021-05-08 15:25:55 +03:00
|
|
|
for _, m := range apis {
|
2021-04-01 21:46:45 +03:00
|
|
|
if err := m.Route(router); err != nil {
|
|
|
|
return fmt.Errorf("routing error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-30 14:12:00 +03:00
|
|
|
gts, err := gotosocial.NewServer(dbService, router, federator, c)
|
2021-04-01 21:46:45 +03: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 20:31:12 +03:00
|
|
|
|
2021-03-04 16:38:18 +03: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 15:37:33 +03:00
|
|
|
logrus.Infof("received signal %s, shutting down", sig)
|
2021-03-04 16:38:18 +03:00
|
|
|
|
|
|
|
// close down all running services in order
|
2021-04-01 21:46:45 +03:00
|
|
|
if err := gts.Stop(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error closing gotosocial service: %s", err)
|
2021-03-04 16:38:18 +03:00
|
|
|
}
|
|
|
|
|
2021-10-11 15:37:33 +03:00
|
|
|
logrus.Info("done! exiting...")
|
2021-03-04 16:38:18 +03:00
|
|
|
return nil
|
|
|
|
}
|