bit of experimenting and tidying

This commit is contained in:
tsmethurst
2021-03-15 16:15:14 +01:00
parent 7590eb9cc2
commit 1eecc2688c
13 changed files with 167 additions and 109 deletions

View File

@ -16,4 +16,4 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package client
package api

72
internal/api/server.go Normal file
View File

@ -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,
}
}

View File

@ -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 {
}

View File

@ -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

View File

@ -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")

View File

@ -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
}

View File

@ -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/).

View File

@ -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(),
}
}

View File

@ -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

3
internal/oauth/README.md Normal file
View File

@ -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.

View File

@ -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))
}