-
-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clean up some weirdness in the router (#80)
- Loading branch information
1 parent
3568579
commit c71e55e
Showing
11 changed files
with
336 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
GoToSocial | ||
Copyright (C) 2021 GoToSocial Authors [email protected] | ||
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 gtsmodel | ||
|
||
// RouterSession is used to store and retrieve settings for a router session. | ||
type RouterSession struct { | ||
ID string `pg:"type:CHAR(26),pk,notnull"` | ||
Auth []byte `pg:",notnull"` | ||
Crypt []byte `pg:",notnull"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
GoToSocial | ||
Copyright (C) 2021 GoToSocial Authors [email protected] | ||
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 router | ||
|
||
import "github.com/gin-gonic/gin" | ||
|
||
// AttachHandler attaches the given gin.HandlerFunc to the router with the specified method and path. | ||
// If the path is set to ANY, then the handlerfunc will be used for ALL methods at its given path. | ||
func (r *router) AttachHandler(method string, path string, handler gin.HandlerFunc) { | ||
if method == "ANY" { | ||
r.engine.Any(path, handler) | ||
} else { | ||
r.engine.Handle(method, path, handler) | ||
} | ||
} | ||
|
||
// AttachMiddleware attaches a gin middleware to the router that will be used globally | ||
func (r *router) AttachMiddleware(middleware gin.HandlerFunc) { | ||
r.engine.Use(middleware) | ||
} | ||
|
||
// AttachNoRouteHandler attaches a gin.HandlerFunc to NoRoute to handle 404's | ||
func (r *router) AttachNoRouteHandler(handler gin.HandlerFunc) { | ||
r.engine.NoRoute(handler) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
GoToSocial | ||
Copyright (C) 2021 GoToSocial Authors [email protected] | ||
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 router | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/gin-contrib/cors" | ||
"github.com/gin-gonic/gin" | ||
"github.com/superseriousbusiness/gotosocial/internal/config" | ||
) | ||
|
||
var corsConfig = cors.Config{ | ||
// TODO: make this customizable so instance admins can specify an origin for CORS requests | ||
AllowAllOrigins: true, | ||
|
||
// adds the following: | ||
// "chrome-extension://" | ||
// "safari-extension://" | ||
// "moz-extension://" | ||
// "ms-browser-extension://" | ||
AllowBrowserExtensions: true, | ||
AllowMethods: []string{ | ||
"POST", | ||
"PUT", | ||
"DELETE", | ||
"GET", | ||
"PATCH", | ||
"OPTIONS", | ||
}, | ||
AllowHeaders: []string{ | ||
// basic cors stuff | ||
"Origin", | ||
"Content-Length", | ||
"Content-Type", | ||
|
||
// needed to pass oauth bearer tokens | ||
"Authorization", | ||
|
||
// needed for websocket upgrade requests | ||
"Upgrade", | ||
"Sec-WebSocket-Extensions", | ||
"Sec-WebSocket-Key", | ||
"Sec-WebSocket-Protocol", | ||
"Sec-WebSocket-Version", | ||
"Connection", | ||
}, | ||
AllowWebSockets: true, | ||
ExposeHeaders: []string{ | ||
// needed for accessing next/prev links when making GET timeline requests | ||
"Link", | ||
|
||
// needed so clients can handle rate limits | ||
"X-RateLimit-Reset", | ||
"X-RateLimit-Limit", | ||
"X-RateLimit-Remaining", | ||
"X-Request-Id", | ||
|
||
// websocket stuff | ||
"Connection", | ||
"Sec-WebSocket-Accept", | ||
"Upgrade", | ||
}, | ||
MaxAge: 2 * time.Minute, | ||
} | ||
|
||
// useCors attaches the corsConfig above to the given gin engine | ||
func useCors(cfg *config.Config, engine *gin.Engine) error { | ||
c := cors.New(corsConfig) | ||
engine.Use(c) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.