diff options
author | 2021-03-15 16:15:14 +0100 | |
---|---|---|
committer | 2021-03-15 16:15:14 +0100 | |
commit | 1eecc2688c5eef14b8834c8f58a46b2f8e6bc845 (patch) | |
tree | 9e9f34745028cb0ef2330b421fef57e68db8d4d4 /internal | |
parent | move mastotypes (diff) | |
download | gotosocial-1eecc2688c5eef14b8834c8f58a46b2f8e6bc845.tar.xz |
bit of experimenting and tidying
Diffstat (limited to 'internal')
-rw-r--r-- | internal/api/route_statuses.go (renamed from internal/client/route_statuses.go) | 2 | ||||
-rw-r--r-- | internal/api/server.go | 72 | ||||
-rw-r--r-- | internal/client/client.go | 27 | ||||
-rw-r--r-- | internal/client/router.go | 19 | ||||
-rw-r--r-- | internal/db/postgres.go | 6 | ||||
-rw-r--r-- | internal/gotosocial/gotosocial.go | 6 | ||||
-rw-r--r-- | internal/gtsmodel/README.md | 5 | ||||
-rw-r--r-- | internal/gtsmodel/account.go (renamed from internal/model/account.go) | 27 | ||||
-rw-r--r-- | internal/gtsmodel/status.go (renamed from internal/model/note.go) | 4 | ||||
-rw-r--r-- | internal/oauth/README.md | 3 | ||||
-rw-r--r-- | internal/oauth/oauth.go | 80 |
11 files changed, 141 insertions, 110 deletions
diff --git a/internal/client/route_statuses.go b/internal/api/route_statuses.go index 47907b9a9..fe8aa9699 100644 --- a/internal/client/route_statuses.go +++ b/internal/api/route_statuses.go @@ -16,4 +16,4 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package client +package api diff --git a/internal/api/server.go b/internal/api/server.go new file mode 100644 index 000000000..a27178855 --- /dev/null +++ b/internal/api/server.go @@ -0,0 +1,72 @@ +/* + 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/>. +*/ + +package api + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/gotosocial/gotosocial/internal/config" + "github.com/sirupsen/logrus" +) + +type Server interface { + AttachHTTPHandler(method string, path string, handler http.HandlerFunc) + AttachGinHandler(method string, path string, handler gin.HandlerFunc) + // AttachMiddleware(handler gin.HandlerFunc) + GetAPIGroup() *gin.RouterGroup + Start() + Stop() +} + +type server struct { + APIGroup *gin.RouterGroup + logger *logrus.Logger + engine *gin.Engine +} + +func (s *server) GetAPIGroup() *gin.RouterGroup { + return s.APIGroup +} + +func (s *server) Start() { + // todo: start gracefully + s.engine.Run() +} + +func (s *server) Stop() { + // todo: shut down gracefully +} + +func (s *server) AttachHTTPHandler(method string, path string, handler http.HandlerFunc) { + s.engine.Handle(method, path, gin.WrapH(handler)) +} + +func (s *server) AttachGinHandler(method string, path string, handler gin.HandlerFunc) { + s.engine.Handle(method, path, handler) +} + +func New(config *config.Config, logger *logrus.Logger) Server { + engine := gin.New() + return &server{ + APIGroup: engine.Group("/api").Group("/v1"), + logger: logger, + engine: engine, + } +} diff --git a/internal/client/client.go b/internal/client/client.go deleted file mode 100644 index ce0f3e015..000000000 --- a/internal/client/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 <http://www.gnu.org/licenses/>. -*/ - -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/router.go b/internal/client/router.go deleted file mode 100644 index 47907b9a9..000000000 --- a/internal/client/router.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 <http://www.gnu.org/licenses/>. -*/ - -package client diff --git a/internal/db/postgres.go b/internal/db/postgres.go index 61f894a86..0453b207b 100644 --- a/internal/db/postgres.go +++ b/internal/db/postgres.go @@ -35,7 +35,7 @@ import ( "github.com/go-pg/pg/v10" "github.com/go-pg/pg/v10/orm" "github.com/gotosocial/gotosocial/internal/config" - "github.com/gotosocial/gotosocial/internal/model" + "github.com/gotosocial/gotosocial/internal/gtsmodel" "github.com/gotosocial/gotosocial/internal/oauth" "github.com/sirupsen/logrus" ) @@ -316,8 +316,8 @@ func (ps *postgresService) Stop(ctx context.Context) error { func (ps *postgresService) CreateSchema(ctx context.Context) error { models := []interface{}{ - (*model.Account)(nil), - (*model.Note)(nil), + (*gtsmodel.GTSAccount)(nil), + (*gtsmodel.GTSStatus)(nil), } ps.log.Info("creating db schema") diff --git a/internal/gotosocial/gotosocial.go b/internal/gotosocial/gotosocial.go index cf4305ee2..a43af65f9 100644 --- a/internal/gotosocial/gotosocial.go +++ b/internal/gotosocial/gotosocial.go @@ -23,7 +23,7 @@ import ( "github.com/go-fed/activity/pub" "github.com/gotosocial/gotosocial/internal/cache" - "github.com/gotosocial/gotosocial/internal/client" + "github.com/gotosocial/gotosocial/internal/api" "github.com/gotosocial/gotosocial/internal/config" "github.com/gotosocial/gotosocial/internal/db" ) @@ -33,7 +33,7 @@ type Gotosocial interface { Stop(context.Context) error } -func New(db db.DB, cache cache.Cache, clientAPI client.API, federationAPI pub.FederatingActor, config *config.Config) (Gotosocial, error) { +func New(db db.DB, cache cache.Cache, clientAPI api.Server, federationAPI pub.FederatingActor, config *config.Config) (Gotosocial, error) { return &gotosocial{ db: db, cache: cache, @@ -46,7 +46,7 @@ func New(db db.DB, cache cache.Cache, clientAPI client.API, federationAPI pub.Fe type gotosocial struct { db db.DB cache cache.Cache - clientAPI client.API + clientAPI api.Server federationAPI pub.FederatingActor config *config.Config } diff --git a/internal/gtsmodel/README.md b/internal/gtsmodel/README.md new file mode 100644 index 000000000..12a05ddec --- /dev/null +++ b/internal/gtsmodel/README.md @@ -0,0 +1,5 @@ +# gtsmodel + +This package contains types used *internally* by GoToSocial and added/removed/selected from the database. As such, they contain sensitive fields which should **never** be serialized or reach the API level. Use the [mastotypes](../../pkg/mastotypes) package for that. + +The annotation used on these structs is for handling them via the go-pg ORM. See [here](https://pg.uptrace.dev/models/). diff --git a/internal/model/account.go b/internal/gtsmodel/account.go index 6d8d04954..7cd942ee8 100644 --- a/internal/model/account.go +++ b/internal/gtsmodel/account.go @@ -16,17 +16,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package model +// package gtsmodel contains types used *internally* by GoToSocial and added/removed/selected from the database. +// These types should never be serialized and/or sent out via public APIs, as they contain sensitive information. +// The annotation used on these structs is for handling them via the go-pg ORM. See here: https://pg.uptrace.dev/models/ +package gtsmodel import ( "net/url" "time" ) -// Account represents a user account -type Account struct { - Avatar - Header +// GTSAccount represents a GoToSocial user account +type GTSAccount struct { + GTSAvatar + GTSHeader URI string URL string ID string `pg:"type:uuid,default:gen_random_uuid(),pk,notnull"` @@ -63,7 +66,7 @@ type Account struct { SuspensionOrigin int } -type Avatar struct { +type GTSAvatar struct { AvatarFileName string AvatarContentType string AvatarFileSize int @@ -72,7 +75,7 @@ type Avatar struct { AvatarStorageSchemaVersion int } -type Header struct { +type GTSHeader struct { HeaderFileName string HeaderContentType string HeaderFileSize int @@ -80,13 +83,3 @@ type Header struct { HeaderRemoteURL *url.URL `pg:"type:text"` HeaderStorageSchemaVersion int } - -func StubAccount() *Account { - return &Account{ - Username: "some_user", - Domain: "example.org", - RemoteURL: "https://example.org/@someuser", - CreatedAt: time.Now(), - UpdatedAt: time.Now(), - } -} diff --git a/internal/model/note.go b/internal/gtsmodel/status.go index 69242cfa7..cba8a6ba8 100644 --- a/internal/model/note.go +++ b/internal/gtsmodel/status.go @@ -16,11 +16,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ -package model +package gtsmodel import "time" -type Note struct { +type GTSStatus struct { ID string `pg:"type:uuid,default:gen_random_uuid(),pk,notnull"` URI string URL string diff --git a/internal/oauth/README.md b/internal/oauth/README.md new file mode 100644 index 000000000..5eaef673f --- /dev/null +++ b/internal/oauth/README.md @@ -0,0 +1,3 @@ +# oauth + +This package provides uses [go-oauth2](https://github.com/go-oauth2/oauth2) to provide [oauth2](https://www.oauth.com/) server functionality to the GoToSocial APIs. diff --git a/internal/oauth/oauth.go b/internal/oauth/oauth.go index 11b1b57fd..ac833d1fc 100644 --- a/internal/oauth/oauth.go +++ b/internal/oauth/oauth.go @@ -19,42 +19,46 @@ package oauth type Server struct { - manager := manage.NewDefaultManager() - // token memory store - manager.MustTokenStorage(store.NewMemoryTokenStore()) - - // client memory store - clientStore := store.NewClientStore() - clientStore.Set("000000", &models.Client{ - ID: "000000", - Secret: "999999", - Domain: "http://localhost", - }) - manager.MapClientStorage(clientStore) - - srv := server.NewDefaultServer(manager) - srv.SetAllowGetAccessRequest(true) - srv.SetClientInfoHandler(server.ClientFormHandler) - - srv.SetInternalErrorHandler(func(err error) (re *errors.Response) { - log.Println("Internal Error:", err.Error()) - return - }) - - srv.SetResponseErrorHandler(func(re *errors.Response) { - log.Println("Response Error:", re.Error.Error()) - }) - - http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) { - err := srv.HandleAuthorizeRequest(w, r) - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - } - }) - - http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) { - srv.HandleTokenRequest(w, r) - }) - - log.Fatal(http.ListenAndServe(":9096", nil)) + +} + +func main() { +// manager := manage.NewDefaultManager() +// // token memory store +// manager.MustTokenStorage(store.NewMemoryTokenStore()) + +// // client memory store +// clientStore := store.NewClientStore() +// clientStore.Set("000000", &models.Client{ +// ID: "000000", +// Secret: "999999", +// Domain: "http://localhost", +// }) +// manager.MapClientStorage(clientStore) + +// srv := server.NewDefaultServer(manager) +// srv.SetAllowGetAccessRequest(true) +// srv.SetClientInfoHandler(server.ClientFormHandler) + +// srv.SetInternalErrorHandler(func(err error) (re *errors.Response) { +// log.Println("Internal Error:", err.Error()) +// return +// }) + +// srv.SetResponseErrorHandler(func(re *errors.Response) { +// log.Println("Response Error:", re.Error.Error()) +// }) + +// http.HandleFunc("/authorize", func(w http.ResponseWriter, r *http.Request) { +// err := srv.HandleAuthorizeRequest(w, r) +// if err != nil { +// http.Error(w, err.Error(), http.StatusBadRequest) +// } +// }) + +// http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) { +// srv.HandleTokenRequest(w, r) +// }) + +// log.Fatal(http.ListenAndServe(":9096", nil)) } |