2021-03-04 13:38:18 +00:00
|
|
|
/*
|
|
|
|
GoToSocial
|
|
|
|
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
|
|
|
|
|
|
|
|
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-03-09 16:03:40 +00:00
|
|
|
package gotosocial
|
2021-03-04 13:38:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-05-08 12:25:55 +00:00
|
|
|
"net/http"
|
2021-03-04 13:38:18 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-04-01 18:46:45 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/action"
|
2021-05-08 12:25:55 +00: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"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/fileserver"
|
|
|
|
mediaModule "github.com/superseriousbusiness/gotosocial/internal/api/client/media"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/client/status"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/security"
|
2021-04-01 18:46:45 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/config"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/federation"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/media"
|
2021-05-08 12:25:55 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/message"
|
2021-04-01 18:46:45 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/oauth"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/router"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/storage"
|
2021-05-08 12:25:55 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/transport"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
|
2021-03-04 13:38:18 +00:00
|
|
|
)
|
|
|
|
|
2021-03-09 16:03:40 +00:00
|
|
|
// Run creates and starts a gotosocial server
|
2021-03-04 13:38:18 +00:00
|
|
|
var Run action.GTSAction = func(ctx context.Context, c *config.Config, log *logrus.Logger) error {
|
2021-05-08 12:25:55 +00:00
|
|
|
dbService, err := db.NewPostgresService(ctx, c, log)
|
2021-03-04 13:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating dbservice: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-04-01 18:46:45 +00:00
|
|
|
router, err := router.New(c, log)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating router: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:42:19 +00:00
|
|
|
storageBackend, err := storage.NewLocal(c, log)
|
2021-04-01 18:46:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating storage backend: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-05-08 12:25:55 +00:00
|
|
|
// build converters and util
|
|
|
|
typeConverter := typeutils.NewConverter(c, dbService)
|
|
|
|
|
2021-04-01 18:46:45 +00:00
|
|
|
// build backend handlers
|
|
|
|
mediaHandler := media.New(c, dbService, storageBackend, log)
|
|
|
|
oauthServer := oauth.New(dbService, log)
|
2021-05-08 12:25:55 +00:00
|
|
|
transportController := transport.NewController(c, &federation.Clock{}, http.DefaultClient, log)
|
|
|
|
federator := federation.NewFederator(dbService, transportController, c, log, typeConverter)
|
|
|
|
processor := message.NewProcessor(c, typeConverter, federator, oauthServer, mediaHandler, storageBackend, dbService, log)
|
|
|
|
if err := processor.Start(); err != nil {
|
|
|
|
return fmt.Errorf("error starting processor: %s", err)
|
2021-04-19 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 18:46:45 +00:00
|
|
|
// build client api modules
|
2021-05-08 12:25:55 +00:00
|
|
|
authModule := auth.New(c, dbService, oauthServer, log)
|
|
|
|
accountModule := account.New(c, processor, log)
|
|
|
|
appsModule := app.New(c, processor, log)
|
|
|
|
mm := mediaModule.New(c, processor, log)
|
|
|
|
fileServerModule := fileserver.New(c, processor, log)
|
|
|
|
adminModule := admin.New(c, processor, log)
|
|
|
|
statusModule := status.New(c, processor, log)
|
2021-04-19 17:42:19 +00:00
|
|
|
securityModule := security.New(c, log)
|
2021-04-01 18:46:45 +00:00
|
|
|
|
2021-05-08 12:25:55 +00:00
|
|
|
apis := []api.ClientModule{
|
2021-04-19 17:42:19 +00:00
|
|
|
// modules with middleware go first
|
|
|
|
securityModule,
|
|
|
|
authModule,
|
|
|
|
|
|
|
|
// now everything else
|
2021-04-01 18:46:45 +00:00
|
|
|
accountModule,
|
|
|
|
appsModule,
|
2021-04-19 17:42:19 +00:00
|
|
|
mm,
|
|
|
|
fileServerModule,
|
|
|
|
adminModule,
|
|
|
|
statusModule,
|
2021-04-01 18:46:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 12:25:55 +00:00
|
|
|
for _, m := range apis {
|
2021-04-01 18:46:45 +00:00
|
|
|
if err := m.Route(router); err != nil {
|
|
|
|
return fmt.Errorf("routing error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:42:19 +00:00
|
|
|
if err := dbService.CreateInstanceAccount(); err != nil {
|
|
|
|
return fmt.Errorf("error creating instance account: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-05-08 12:25:55 +00:00
|
|
|
gts, err := New(dbService, router, federator, c)
|
2021-04-01 18:46:45 +00: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 17:31:12 +00:00
|
|
|
|
2021-03-04 13:38:18 +00:00
|
|
|
// catch shutdown signals from the operating system
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, os.Interrupt, syscall.SIGTERM)
|
|
|
|
sig := <-sigs
|
|
|
|
log.Infof("received signal %s, shutting down", sig)
|
|
|
|
|
|
|
|
// close down all running services in order
|
2021-04-01 18:46:45 +00:00
|
|
|
if err := gts.Stop(ctx); err != nil {
|
|
|
|
return fmt.Errorf("error closing gotosocial service: %s", err)
|
2021-03-04 13:38:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("done! exiting...")
|
|
|
|
return nil
|
|
|
|
}
|