| 
									
										
										
										
											2021-06-01 10:51:35 +02:00
										 |  |  | package web | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"github.com/gin-contrib/static" | 
					
						
							|  |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							| 
									
										
										
										
											2021-09-29 13:08:43 +02:00
										 |  |  | 	"go.uber.org/zap" | 
					
						
							| 
									
										
										
										
											2021-06-01 10:51:35 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-19 17:59:47 +02:00
										 |  |  | // A ModuleRegisterFunc is a module.
 | 
					
						
							| 
									
										
										
										
											2021-06-01 10:51:35 +02:00
										 |  |  | type ModuleRegisterFunc func(*gin.Engine, *Service) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-19 17:59:47 +02:00
										 |  |  | // ModuleRegister adds f to ws's list of modules.
 | 
					
						
							| 
									
										
										
										
											2021-07-19 17:59:08 +02:00
										 |  |  | func (ws *Service) ModuleRegister(f ModuleRegisterFunc) { | 
					
						
							|  |  |  | 	ws.modules = append(ws.modules, f) | 
					
						
							| 
									
										
										
										
											2021-06-01 10:51:35 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-19 17:59:47 +02:00
										 |  |  | // Bind executes all of ws's modules with r.
 | 
					
						
							| 
									
										
										
										
											2021-06-01 10:51:35 +02:00
										 |  |  | func (ws *Service) Bind(r *gin.Engine) { | 
					
						
							| 
									
										
										
										
											2021-07-19 17:59:08 +02:00
										 |  |  | 	for _, f := range ws.modules { | 
					
						
							| 
									
										
										
										
											2021-06-01 10:51:35 +02:00
										 |  |  | 		f(r, ws) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 13:08:43 +02:00
										 |  |  | 	ws.log.Info("bind modules", zap.Int("count", len(ws.modules))) | 
					
						
							| 
									
										
										
										
											2021-10-25 12:56:47 +02:00
										 |  |  | 	if ws.Webroot != "" { | 
					
						
							| 
									
										
										
										
											2022-06-06 01:55:57 +02:00
										 |  |  | 		ws.WebrootFS = static.LocalFile(ws.Webroot, false) | 
					
						
							| 
									
										
										
										
											2021-10-25 12:56:47 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	r.Use(func(c *gin.Context) { | 
					
						
							|  |  |  | 		if !ws.WebrootIndexDisable { | 
					
						
							|  |  |  | 			_, err := ws.WebrootFS.Open(c.Request.URL.Path) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				c.FileFromFS("/", ws.WebrootFS) | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		c.FileFromFS(c.Request.URL.Path, ws.WebrootFS) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-06-01 10:51:35 +02:00
										 |  |  | } |