| 
									
										
										
										
											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" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/sirupsen/logrus" | 
					
						
							| 
									
										
										
										
											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-05-31 17:36:35 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/emoji" | 
					
						
							| 
									
										
										
										
											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-05-21 23:04:59 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/timeline" | 
					
						
							| 
									
										
										
										
											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-05-30 13:12:00 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/blob" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/cliactions" | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/config" | 
					
						
							| 
									
										
										
										
											2021-05-15 11:58:11 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db/pg" | 
					
						
							| 
									
										
										
										
											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-05-09 11:25:13 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/media" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/oauth" | 
					
						
							| 
									
										
										
										
											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-03-04 14:38:18 +01:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 11:25:13 +02:00
										 |  |  | var models []interface{} = []interface{}{ | 
					
						
							|  |  |  | 	>smodel.Account{}, | 
					
						
							|  |  |  | 	>smodel.Application{}, | 
					
						
							|  |  |  | 	>smodel.Block{}, | 
					
						
							|  |  |  | 	>smodel.DomainBlock{}, | 
					
						
							|  |  |  | 	>smodel.EmailDomainBlock{}, | 
					
						
							|  |  |  | 	>smodel.Follow{}, | 
					
						
							|  |  |  | 	>smodel.FollowRequest{}, | 
					
						
							|  |  |  | 	>smodel.MediaAttachment{}, | 
					
						
							|  |  |  | 	>smodel.Mention{}, | 
					
						
							|  |  |  | 	>smodel.Status{}, | 
					
						
							|  |  |  | 	>smodel.StatusFave{}, | 
					
						
							|  |  |  | 	>smodel.StatusBookmark{}, | 
					
						
							|  |  |  | 	>smodel.StatusMute{}, | 
					
						
							|  |  |  | 	>smodel.Tag{}, | 
					
						
							|  |  |  | 	>smodel.User{}, | 
					
						
							|  |  |  | 	>smodel.Emoji{}, | 
					
						
							| 
									
										
										
										
											2021-05-09 14:06:06 +02:00
										 |  |  | 	>smodel.Instance{}, | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	>smodel.Notification{}, | 
					
						
							| 
									
										
										
										
											2021-05-09 11:25:13 +02:00
										 |  |  | 	&oauth.Token{}, | 
					
						
							|  |  |  | 	&oauth.Client{}, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-30 13:12:00 +02:00
										 |  |  | // Start creates and starts a gotosocial server | 
					
						
							|  |  |  | var Start cliactions.GTSAction = func(ctx context.Context, c *config.Config, log *logrus.Logger) error { | 
					
						
							| 
									
										
										
										
											2021-05-15 11:58:11 +02:00
										 |  |  | 	dbService, err := pg.NewPostgresService(ctx, c, log) | 
					
						
							| 
									
										
										
										
											2021-03-04 14:38:18 +01:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return fmt.Errorf("error creating dbservice: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	for _, m := range models { | 
					
						
							|  |  |  | 		if err := dbService.CreateTable(m); err != nil { | 
					
						
							|  |  |  | 			return fmt.Errorf("table creation error: %s", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := dbService.CreateInstanceAccount(); err != nil { | 
					
						
							|  |  |  | 		return fmt.Errorf("error creating instance account: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := dbService.CreateInstanceInstance(); err != nil { | 
					
						
							|  |  |  | 		return fmt.Errorf("error creating instance instance: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	federatingDB := federatingdb.New(dbService, c, log) | 
					
						
							| 
									
										
										
										
											2021-05-21 15:48:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	router, err := router.New(c, log) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return fmt.Errorf("error creating router: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-30 13:12:00 +02:00
										 |  |  | 	storageBackend, err := blob.NewLocal(c, log) | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return fmt.Errorf("error creating storage backend: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-08 14:25:55 +02:00
										 |  |  | 	// build converters and util | 
					
						
							|  |  |  | 	typeConverter := typeutils.NewConverter(c, dbService) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	timelineManager := timelineprocessing.NewManager(dbService, typeConverter, c, log) | 
					
						
							| 
									
										
										
										
											2021-05-08 14:25:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	// build backend handlers | 
					
						
							|  |  |  | 	mediaHandler := media.New(c, dbService, storageBackend, log) | 
					
						
							|  |  |  | 	oauthServer := oauth.New(dbService, log) | 
					
						
							| 
									
										
										
										
											2021-05-08 14:25:55 +02:00
										 |  |  | 	transportController := transport.NewController(c, &federation.Clock{}, http.DefaultClient, log) | 
					
						
							| 
									
										
										
										
											2021-05-21 15:48:26 +02:00
										 |  |  | 	federator := federation.NewFederator(dbService, federatingDB, transportController, c, log, typeConverter) | 
					
						
							| 
									
										
										
										
											2021-06-13 18:42:28 +02:00
										 |  |  | 	processor := processing.NewProcessor(c, typeConverter, federator, oauthServer, mediaHandler, storageBackend, timelineManager, dbService, log) | 
					
						
							| 
									
										
										
										
											2021-05-08 14:25:55 +02:00
										 |  |  | 	if err := processor.Start(); err != nil { | 
					
						
							|  |  |  | 		return fmt.Errorf("error starting processor: %s", err) | 
					
						
							| 
									
										
										
										
											2021-04-19 19:42:19 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-01 20:46:45 +02:00
										 |  |  | 	// build client api modules | 
					
						
							| 
									
										
										
										
											2021-05-08 14:25:55 +02:00
										 |  |  | 	authModule := auth.New(c, dbService, oauthServer, log) | 
					
						
							|  |  |  | 	accountModule := account.New(c, processor, log) | 
					
						
							| 
									
										
										
										
											2021-05-09 14:06:06 +02:00
										 |  |  | 	instanceModule := instance.New(c, processor, log) | 
					
						
							| 
									
										
										
										
											2021-05-08 14:25:55 +02:00
										 |  |  | 	appsModule := app.New(c, processor, log) | 
					
						
							| 
									
										
										
										
											2021-05-15 11:58:11 +02:00
										 |  |  | 	followRequestsModule := followrequest.New(c, processor, log) | 
					
						
							| 
									
										
										
										
											2021-05-09 20:34:27 +02:00
										 |  |  | 	webfingerModule := webfinger.New(c, processor, log) | 
					
						
							|  |  |  | 	usersModule := user.New(c, processor, log) | 
					
						
							| 
									
										
										
										
											2021-05-21 23:04:59 +02:00
										 |  |  | 	timelineModule := timeline.New(c, processor, log) | 
					
						
							| 
									
										
										
										
											2021-05-27 16:06:24 +02:00
										 |  |  | 	notificationModule := notification.New(c, processor, log) | 
					
						
							| 
									
										
										
										
											2021-05-29 19:36:54 +02:00
										 |  |  | 	searchModule := search.New(c, processor, log) | 
					
						
							| 
									
										
										
										
											2021-05-31 17:36:35 +02:00
										 |  |  | 	filtersModule := filter.New(c, processor, log) | 
					
						
							|  |  |  | 	emojiModule := emoji.New(c, processor, log) | 
					
						
							|  |  |  | 	listsModule := list.New(c, processor, log) | 
					
						
							| 
									
										
										
										
											2021-05-08 14:25:55 +02:00
										 |  |  | 	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 19:42:19 +02:00
										 |  |  | 	securityModule := security.New(c, log) | 
					
						
							| 
									
										
										
										
											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-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, | 
					
						
							|  |  |  | 		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-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-05-30 13:12:00 +02:00
										 |  |  | 	gts, err := gotosocial.NewServer(dbService, router, federator, c) | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 	log.Infof("received signal %s, shutting down", sig) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	log.Info("done! exiting...") | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |