gotosocial.go (2899B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package gotosocial 19 20 import ( 21 "context" 22 23 "github.com/superseriousbusiness/gotosocial/internal/db" 24 "github.com/superseriousbusiness/gotosocial/internal/federation" 25 "github.com/superseriousbusiness/gotosocial/internal/media" 26 "github.com/superseriousbusiness/gotosocial/internal/router" 27 ) 28 29 // Server is the 'main' function of the gotosocial server, and the place where everything hangs together. 30 // The logic of stopping and starting the entire server is contained here. 31 type Server interface { 32 // Start starts up the gotosocial server. If something goes wrong 33 // while starting the server, then an error will be returned. 34 Start(context.Context) error 35 // Stop closes down the gotosocial server, first closing the router 36 // then the database. If something goes wrong while stopping, an 37 // error will be returned. 38 Stop(context.Context) error 39 } 40 41 // NewServer returns a new gotosocial server, initialized with the given configuration. 42 // An error will be returned the caller if something goes wrong during initialization 43 // eg., no db or storage connection, port for router already in use, etc. 44 func NewServer(db db.DB, apiRouter router.Router, federator federation.Federator, mediaManager *media.Manager) (Server, error) { 45 return &gotosocial{ 46 db: db, 47 apiRouter: apiRouter, 48 federator: federator, 49 mediaManager: mediaManager, 50 }, nil 51 } 52 53 // gotosocial fulfils the gotosocial interface. 54 type gotosocial struct { 55 db db.DB 56 apiRouter router.Router 57 federator federation.Federator 58 mediaManager *media.Manager 59 } 60 61 // Start starts up the gotosocial server. If something goes wrong 62 // while starting the server, then an error will be returned. 63 func (gts *gotosocial) Start(ctx context.Context) error { 64 gts.apiRouter.Start() 65 return nil 66 } 67 68 // Stop closes down the gotosocial server, first closing the router, 69 // then the media manager, then the database. 70 // If something goes wrong while stopping, an error will be returned. 71 func (gts *gotosocial) Stop(ctx context.Context) error { 72 if err := gts.apiRouter.Stop(ctx); err != nil { 73 return err 74 } 75 76 return gts.db.Stop(ctx) 77 }