Refactor environment sensor URL path generation
This commit is contained in:
		
					parent
					
						
							
								8015ed2be5
							
						
					
				
			
			
				commit
				
					
						7218177625
					
				
			
		
					 2 changed files with 28 additions and 9 deletions
				
			
		
							
								
								
									
										17
									
								
								main.go
									
										
									
									
									
								
							
							
						
						
									
										17
									
								
								main.go
									
										
									
									
									
								
							|  | @ -2,18 +2,17 @@ package main | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"flag" | 	"flag" | ||||||
| 	"fmt" |  | ||||||
| 	"log" | 	"log" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"os" | 	"os" | ||||||
| 	"os/signal" | 	"os/signal" | ||||||
| 	"strings" |  | ||||||
| 	"syscall" | 	"syscall" | ||||||
| 
 | 
 | ||||||
| 	"git.hamburg.ccc.de/ccchh/spaceapid/config" | 	"git.hamburg.ccc.de/ccchh/spaceapid/config" | ||||||
| 	"git.hamburg.ccc.de/ccchh/spaceapid/handlers" | 	"git.hamburg.ccc.de/ccchh/spaceapid/handlers" | ||||||
| 	"git.hamburg.ccc.de/ccchh/spaceapid/persistence" | 	"git.hamburg.ccc.de/ccchh/spaceapid/persistence" | ||||||
| 	"git.hamburg.ccc.de/ccchh/spaceapid/types" | 	"git.hamburg.ccc.de/ccchh/spaceapid/types" | ||||||
|  | 	"git.hamburg.ccc.de/ccchh/spaceapid/util" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| var ( | var ( | ||||||
|  | @ -59,15 +58,15 @@ func main() { | ||||||
| 		handlers.StateOpen(conf.Credentials, conf.Dynamic.State.Open.AllowedCredentials, &conf.Response.State), | 		handlers.StateOpen(conf.Credentials, conf.Dynamic.State.Open.AllowedCredentials, &conf.Response.State), | ||||||
| 	) | 	) | ||||||
| 	// Register handlers for Environmental Sensors | 	// Register handlers for Environmental Sensors | ||||||
| 	for key, envSensorConfigs := range conf.Dynamic.Sensors { | 	for sensorType, envSensorConfigs := range conf.Dynamic.Sensors { | ||||||
| 		for i, envSensorConfig := range envSensorConfigs { | 		for i, envSensorConfig := range envSensorConfigs { | ||||||
| 			pattern := fmt.Sprintf("/sensors/%s/%s", key, envSensorConfig.SensorData.Location) | 			urlPath := util.GetSensorURLPath( | ||||||
| 			if envSensorConfig.SensorData.Name != "" { | 				sensorType, envSensorConfig.SensorData.Location, envSensorConfig.SensorData.Name, | ||||||
| 				pattern += "/" + envSensorConfig.SensorData.Name | 			) | ||||||
| 			} | 			http.HandleFunc( | ||||||
| 			http.HandleFunc(strings.ToLower(pattern), | 				urlPath, | ||||||
| 				handlers.EnvironmentSensor( | 				handlers.EnvironmentSensor( | ||||||
| 					conf.Credentials, envSensorConfig.AllowedCredentials, &conf.Response.Sensors[key][i], | 					conf.Credentials, envSensorConfig.AllowedCredentials, &conf.Response.Sensors[sensorType][i], | ||||||
| 				), | 				), | ||||||
| 			) | 			) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
							
								
								
									
										20
									
								
								util/util.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								util/util.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,20 @@ | ||||||
|  | package util | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"strings" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | // GetSensorURLPath generates the URL path for the given sensor details. | ||||||
|  | // location and name may be optional depending on sensorType, see the schema definition for details. | ||||||
|  | // The path is always all-lowercase. | ||||||
|  | func GetSensorURLPath(sensorType, location, name string) string { | ||||||
|  | 	path := fmt.Sprintf("/sensors/%s", sensorType) | ||||||
|  | 	if location != "" { | ||||||
|  | 		path += "/" + location | ||||||
|  | 	} | ||||||
|  | 	if name != "" { | ||||||
|  | 		path += "/" + name | ||||||
|  | 	} | ||||||
|  | 	return strings.ToLower(path) | ||||||
|  | } | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue