From 9a79d176c91de59a409d06efc8837c1d8f38b5c0 Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Tue, 9 Mar 2021 17:03:40 +0100 Subject: moving stuff around, stubbing interfaces --- internal/api/client.go | 27 ------------------ internal/api/route_statuses.go | 31 -------------------- internal/api/router.go | 49 -------------------------------- internal/cache/cache.go | 6 ++++ internal/client/client.go | 27 ++++++++++++++++++ internal/client/route_statuses.go | 19 +++++++++++++ internal/client/router.go | 19 +++++++++++++ internal/config/db.go | 18 ++++++++++++ internal/db/postgres.go | 2 +- internal/federation/federation.go | 34 +++++++++++----------- internal/gotosocial/actions.go | 58 +++++++++++++++++++++++++++++++++++++ internal/gotosocial/gotosocial.go | 60 +++++++++++++++++++++++++++++++++++++++ internal/media/media.go | 24 ++++++++++++++++ internal/server/actions.go | 58 ------------------------------------- internal/server/server.go | 19 ------------- 15 files changed, 249 insertions(+), 202 deletions(-) delete mode 100644 internal/api/client.go delete mode 100644 internal/api/route_statuses.go delete mode 100644 internal/api/router.go create mode 100644 internal/client/client.go create mode 100644 internal/client/route_statuses.go create mode 100644 internal/client/router.go create mode 100644 internal/gotosocial/actions.go create mode 100644 internal/gotosocial/gotosocial.go create mode 100644 internal/media/media.go delete mode 100644 internal/server/actions.go delete mode 100644 internal/server/server.go (limited to 'internal') diff --git a/internal/api/client.go b/internal/api/client.go deleted file mode 100644 index 5d8a709b0..000000000 --- a/internal/api/client.go +++ /dev/null @@ -1,27 +0,0 @@ -/* - 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 . -*/ - -package api - -// API is the client API exposed to the outside world for access by front-ends; this is distinct from the federation API -type API interface { -} - -// api implements Api interface -type api struct { -} diff --git a/internal/api/route_statuses.go b/internal/api/route_statuses.go deleted file mode 100644 index 1cdc5f9c2..000000000 --- a/internal/api/route_statuses.go +++ /dev/null @@ -1,31 +0,0 @@ -/* - 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 . -*/ - -package api - -import ( - "net/http" - - "github.com/gin-gonic/gin" -) - -func statusGet(c *gin.Context) { - c.HTML(http.StatusOK, "index.tmpl", gin.H{ - "title": "Posts", - }) -} diff --git a/internal/api/router.go b/internal/api/router.go deleted file mode 100644 index 95e784016..000000000 --- a/internal/api/router.go +++ /dev/null @@ -1,49 +0,0 @@ -/* - 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 . -*/ - -package api - -import "github.com/gin-gonic/gin" - -// Router provides the http routes used by the API -type Router interface { - Route() error -} - -// NewRouter returns a new router -func NewRouter() Router { - return &router{} -} - -// router implements the router interface -type router struct { -} - -func (r *router) Route() error { - ginRouter := gin.Default() - ginRouter.LoadHTMLGlob("web/template/*") - - apiGroup := ginRouter.Group("/api") - - v1 := apiGroup.Group("/v1") - - statusesGroup := v1.Group("/statuses") - statusesGroup.GET(":id", statusGet) - err := ginRouter.Run() - return err -} diff --git a/internal/cache/cache.go b/internal/cache/cache.go index 4bef60642..ed13aded4 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -17,3 +17,9 @@ */ package cache + +// Cache defines an in-memory cache that is safe to be wiped when the application is restarted +type Cache interface { + Store(k string, v interface{}) error + Fetch(k string) (interface{}, error) +} diff --git a/internal/client/client.go b/internal/client/client.go new file mode 100644 index 000000000..ce0f3e015 --- /dev/null +++ b/internal/client/client.go @@ -0,0 +1,27 @@ +/* + 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 . +*/ + +package client + +// API is the client API exposed to the outside world for access by front-ends; this is distinct from the federation API +type API interface { +} + +// api implements ClientAPI interface +type api struct { +} diff --git a/internal/client/route_statuses.go b/internal/client/route_statuses.go new file mode 100644 index 000000000..47907b9a9 --- /dev/null +++ b/internal/client/route_statuses.go @@ -0,0 +1,19 @@ +/* + 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 . +*/ + +package client diff --git a/internal/client/router.go b/internal/client/router.go new file mode 100644 index 000000000..47907b9a9 --- /dev/null +++ b/internal/client/router.go @@ -0,0 +1,19 @@ +/* + 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 . +*/ + +package client diff --git a/internal/config/db.go b/internal/config/db.go index 9cbc1746e..fbde6fe82 100644 --- a/internal/config/db.go +++ b/internal/config/db.go @@ -1,3 +1,21 @@ +/* + 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 . +*/ + package config // DBConfig provides configuration options for the database connection diff --git a/internal/db/postgres.go b/internal/db/postgres.go index a66b1364a..568d384e4 100644 --- a/internal/db/postgres.go +++ b/internal/db/postgres.go @@ -114,7 +114,7 @@ func newPostgresService(ctx context.Context, c *config.Config, log *logrus.Entry CreatedAt: time.Now(), UpdatedAt: time.Now(), } - if _, err := conn.Model(note).Returning("id").Insert(); err != nil { + if _, err := conn.WithContext(ctx).Model(note).Returning("id").Insert(); err != nil { cancel() return nil, fmt.Errorf("db insert error: %s", err) } diff --git a/internal/federation/federation.go b/internal/federation/federation.go index dd75fa506..ebc9102b0 100644 --- a/internal/federation/federation.go +++ b/internal/federation/federation.go @@ -31,82 +31,82 @@ import ( ) func New(db db.DB) pub.FederatingActor { - fs := &FederationService{} - return pub.NewFederatingActor(fs, fs, db, fs) + fa := &API{} + return pub.NewFederatingActor(fa, fa, db, fa) } -type FederationService struct { +type API struct { } // AuthenticateGetInbox determines whether the request is for a GET call to the Actor's Inbox. -func (fs *FederationService) AuthenticateGetInbox(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, bool, error) { +func (fa *API) AuthenticateGetInbox(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, bool, error) { // TODO return nil, false, nil } // AuthenticateGetOutbox determines whether the request is for a GET call to the Actor's Outbox. -func (fs *FederationService) AuthenticateGetOutbox(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, bool, error) { +func (fa *API) AuthenticateGetOutbox(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, bool, error) { // TODO return nil, false, nil } // GetOutbox returns a proper paginated view of the Outbox for serving in a response. -func (fs *FederationService) GetOutbox(ctx context.Context, r *http.Request) (vocab.ActivityStreamsOrderedCollectionPage, error) { +func (fa *API) GetOutbox(ctx context.Context, r *http.Request) (vocab.ActivityStreamsOrderedCollectionPage, error) { // TODO return nil, nil } // NewTransport returns a new pub.Transport for federating with peer software. -func (fs *FederationService) NewTransport(ctx context.Context, actorBoxIRI *url.URL, gofedAgent string) (pub.Transport, error) { +func (fa *API) NewTransport(ctx context.Context, actorBoxIRI *url.URL, gofedAgent string) (pub.Transport, error) { // TODO return nil, nil } -func (fs *FederationService) PostInboxRequestBodyHook(ctx context.Context, r *http.Request, activity pub.Activity) (context.Context, error) { +func (fa *API) PostInboxRequestBodyHook(ctx context.Context, r *http.Request, activity pub.Activity) (context.Context, error) { // TODO return nil, nil } -func (fs *FederationService) AuthenticatePostInbox(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, bool, error) { +func (fa *API) AuthenticatePostInbox(ctx context.Context, w http.ResponseWriter, r *http.Request) (context.Context, bool, error) { // TODO return nil, false, nil } -func (fs *FederationService) Blocked(ctx context.Context, actorIRIs []*url.URL) (bool, error) { +func (fa *API) Blocked(ctx context.Context, actorIRIs []*url.URL) (bool, error) { // TODO return false, nil } -func (fs *FederationService) FederatingCallbacks(ctx context.Context) (pub.FederatingWrappedCallbacks, []interface{}, error) { +func (fa *API) FederatingCallbacks(ctx context.Context) (pub.FederatingWrappedCallbacks, []interface{}, error) { // TODO return pub.FederatingWrappedCallbacks{}, nil, nil } -func (fs *FederationService) DefaultCallback(ctx context.Context, activity pub.Activity) error { +func (fa *API) DefaultCallback(ctx context.Context, activity pub.Activity) error { // TODO return nil } -func (fs *FederationService) MaxInboxForwardingRecursionDepth(ctx context.Context) int { +func (fa *API) MaxInboxForwardingRecursionDepth(ctx context.Context) int { // TODO return 0 } -func (fs *FederationService) MaxDeliveryRecursionDepth(ctx context.Context) int { +func (fa *API) MaxDeliveryRecursionDepth(ctx context.Context) int { // TODO return 0 } -func (fs *FederationService) FilterForwarding(ctx context.Context, potentialRecipients []*url.URL, a pub.Activity) ([]*url.URL, error) { +func (fa *API) FilterForwarding(ctx context.Context, potentialRecipients []*url.URL, a pub.Activity) ([]*url.URL, error) { // TODO return nil, nil } -func (fs *FederationService) GetInbox(ctx context.Context, r *http.Request) (vocab.ActivityStreamsOrderedCollectionPage, error) { +func (fa *API) GetInbox(ctx context.Context, r *http.Request) (vocab.ActivityStreamsOrderedCollectionPage, error) { // TODO return nil, nil } -func (fs *FederationService) Now() time.Time { +func (fa *API) Now() time.Time { return time.Now() } diff --git a/internal/gotosocial/actions.go b/internal/gotosocial/actions.go new file mode 100644 index 000000000..3d3fdc366 --- /dev/null +++ b/internal/gotosocial/actions.go @@ -0,0 +1,58 @@ +/* + 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 . +*/ + +package gotosocial + +import ( + "context" + "fmt" + "os" + "os/signal" + "syscall" + + "github.com/gotosocial/gotosocial/internal/action" + "github.com/gotosocial/gotosocial/internal/config" + "github.com/gotosocial/gotosocial/internal/db" + "github.com/sirupsen/logrus" +) + +// Run creates and starts a gotosocial server +var Run action.GTSAction = func(ctx context.Context, c *config.Config, log *logrus.Logger) error { + dbService, err := db.New(ctx, c, log) + if err != nil { + return fmt.Errorf("error creating dbservice: %s", err) + } + + if err := dbService.CreateSchema(ctx); err != nil { + return fmt.Errorf("error creating dbschema: %s", err) + } + + // 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 + if err := dbService.Stop(ctx); err != nil { + return fmt.Errorf("error closing dbservice: %s", err) + } + + log.Info("done! exiting...") + return nil +} diff --git a/internal/gotosocial/gotosocial.go b/internal/gotosocial/gotosocial.go new file mode 100644 index 000000000..8ad79e09d --- /dev/null +++ b/internal/gotosocial/gotosocial.go @@ -0,0 +1,60 @@ +/* + 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 . +*/ + +package gotosocial + +import ( + "context" + + "github.com/go-fed/activity/pub" + "github.com/gotosocial/gotosocial/internal/cache" + "github.com/gotosocial/gotosocial/internal/client" + "github.com/gotosocial/gotosocial/internal/config" + "github.com/gotosocial/gotosocial/internal/db" +) + +type Gotosocial interface { + Start(context.Context) error + Stop(context.Context) error +} + +func New(db db.DB, cache cache.Cache, clientAPI client.API, federationAPI pub.FederatingActor, config *config.Config) (Gotosocial, error) { + return &gotosocial{ + db: db, + cache: cache, + clientAPI: clientAPI, + federationAPI: federationAPI, + config: config, + }, nil +} + +type gotosocial struct { + db db.DB + cache cache.Cache + clientAPI client.API + federationAPI pub.FederatingActor + config *config.Config +} + +func (gts *gotosocial) Start(ctx context.Context) error { + return nil +} + +func (gts *gotosocial) Stop(ctx context.Context) error { + return nil +} diff --git a/internal/media/media.go b/internal/media/media.go new file mode 100644 index 000000000..8ff8693f4 --- /dev/null +++ b/internal/media/media.go @@ -0,0 +1,24 @@ +/* + 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 . +*/ + +package media + +// API provides an interface for parsing, storing, and retrieving media objects like photos and videos +type API interface { + +} diff --git a/internal/server/actions.go b/internal/server/actions.go deleted file mode 100644 index 6ff3045b5..000000000 --- a/internal/server/actions.go +++ /dev/null @@ -1,58 +0,0 @@ -/* - 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 . -*/ - -package server - -import ( - "context" - "fmt" - "os" - "os/signal" - "syscall" - - "github.com/gotosocial/gotosocial/internal/action" - "github.com/gotosocial/gotosocial/internal/config" - "github.com/gotosocial/gotosocial/internal/db" - "github.com/sirupsen/logrus" -) - -// Run starts the gotosocial server -var Run action.GTSAction = func(ctx context.Context, c *config.Config, log *logrus.Logger) error { - dbService, err := db.New(ctx, c, log) - if err != nil { - return fmt.Errorf("error creating dbservice: %s", err) - } - - if err := dbService.CreateSchema(ctx); err != nil { - return fmt.Errorf("error creating dbschema: %s", err) - } - - // 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 - if err := dbService.Stop(ctx); err != nil { - return fmt.Errorf("error closing dbservice: %s", err) - } - - log.Info("done! exiting...") - return nil -} diff --git a/internal/server/server.go b/internal/server/server.go deleted file mode 100644 index 5fef83948..000000000 --- a/internal/server/server.go +++ /dev/null @@ -1,19 +0,0 @@ -/* - 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 . -*/ - -package server -- cgit v1.2.3