| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |    GoToSocial | 
					
						
							| 
									
										
										
										
											2023-01-05 12:43:00 +01:00
										 |  |  |    Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |    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 web | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-15 18:45:15 +00:00
										 |  |  | 	"codeberg.org/gruf/go-cache/v3" | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/config" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/log" | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/processing" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/router" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/uris" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	confirmEmailPath   = "/" + uris.ConfirmEmailPath | 
					
						
							|  |  |  | 	profilePath        = "/@:" + usernameKey | 
					
						
							|  |  |  | 	customCSSPath      = profilePath + "/custom.css" | 
					
						
							|  |  |  | 	rssFeedPath        = profilePath + "/feed.rss" | 
					
						
							|  |  |  | 	statusPath         = profilePath + "/statuses/:" + statusIDKey | 
					
						
							|  |  |  | 	assetsPathPrefix   = "/assets" | 
					
						
							|  |  |  | 	distPathPrefix     = assetsPathPrefix + "/dist" | 
					
						
							|  |  |  | 	settingsPathPrefix = "/settings" | 
					
						
							|  |  |  | 	settingsPanelGlob  = settingsPathPrefix + "/*panel" | 
					
						
							|  |  |  | 	userPanelPath      = settingsPathPrefix + "/user" | 
					
						
							|  |  |  | 	adminPanelPath     = settingsPathPrefix + "/admin" | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	tokenParam  = "token" | 
					
						
							|  |  |  | 	usernameKey = "username" | 
					
						
							|  |  |  | 	statusIDKey = "status" | 
					
						
							| 
									
										
										
										
											2022-10-08 14:00:39 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	cacheControlHeader    = "Cache-Control"     // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control | 
					
						
							|  |  |  | 	cacheControlNoCache   = "no-cache"          // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#response_directives | 
					
						
							|  |  |  | 	ifModifiedSinceHeader = "If-Modified-Since" // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since | 
					
						
							|  |  |  | 	ifNoneMatchHeader     = "If-None-Match"     // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match | 
					
						
							|  |  |  | 	eTagHeader            = "ETag"              // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag | 
					
						
							|  |  |  | 	lastModifiedHeader    = "Last-Modified"     // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Module struct { | 
					
						
							| 
									
										
										
										
											2022-10-08 14:00:39 +02:00
										 |  |  | 	processor processing.Processor | 
					
						
							|  |  |  | 	eTagCache cache.Cache[string, eTagCacheEntry] | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | func New(processor processing.Processor) *Module { | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 	return &Module{ | 
					
						
							| 
									
										
										
										
											2022-10-08 14:00:39 +02:00
										 |  |  | 		processor: processor, | 
					
						
							|  |  |  | 		eTagCache: newETagCache(), | 
					
						
							| 
									
										
										
										
											2022-09-04 14:41:42 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-03 10:50:59 +00:00
										 |  |  | func (m *Module) Route(r router.Router, mi ...gin.HandlerFunc) { | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 	// serve static files from assets dir at /assets | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	assetsGroup := r.AttachGroup(assetsPathPrefix) | 
					
						
							|  |  |  | 	webAssetsAbsFilePath, err := filepath.Abs(config.GetWebAssetBaseDir()) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Panicf("error getting absolute path of assets dir: %s", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	fs := fileSystem{http.Dir(webAssetsAbsFilePath)} | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	// use the cache middleware on all handlers in this group | 
					
						
							|  |  |  | 	assetsGroup.Use(m.assetsCacheControlMiddleware(fs)) | 
					
						
							| 
									
										
										
										
											2023-01-03 10:50:59 +00:00
										 |  |  | 	assetsGroup.Use(mi...) | 
					
						
							| 
									
										
										
										
											2022-09-12 13:14:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	// serve static file system in the root of this group, | 
					
						
							|  |  |  | 	// will end up being something like "/assets/" | 
					
						
							|  |  |  | 	assetsGroup.StaticFS("/", fs) | 
					
						
							| 
									
										
										
										
											2022-10-08 14:00:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	/* | 
					
						
							|  |  |  | 		Attach individual web handlers which require no specific middlewares | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	r.AttachHandler(http.MethodGet, "/", m.baseHandler) // front-page | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, settingsPathPrefix, m.SettingsPanelHandler) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, settingsPanelGlob, m.SettingsPanelHandler) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, profilePath, m.profileGETHandler) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, customCSSPath, m.customCSSGETHandler) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, rssFeedPath, m.rssFeedGETHandler) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, statusPath, m.threadGETHandler) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, confirmEmailPath, m.confirmEmailGETHandler) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, robotsPath, m.robotsGETHandler) | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-25 18:06:41 +01:00
										 |  |  | 	r.AttachHandler(http.MethodGet, domainBlockListPath, m.domainBlockListGETHandler) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	/* | 
					
						
							|  |  |  | 		Attach redirects from old endpoints to current ones for backwards compatibility | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	r.AttachHandler(http.MethodGet, "/auth/edit", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, userPanelPath) }) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, "/user", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, userPanelPath) }) | 
					
						
							|  |  |  | 	r.AttachHandler(http.MethodGet, "/admin", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, adminPanelPath) }) | 
					
						
							| 
									
										
										
										
											2022-07-18 12:55:06 +02:00
										 |  |  | } |