mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 10:13:14 -04:00 
			
		
		
		
	Cleanup: put middleware helper functions in pkgs (#245)
Move all (almost all) Go files in middleware into their own packages. This makes for better naming and discoverability. Lot of changes elsewhere to make this change. The middleware.State was renamed to request.Request which is better, but still does not cover all use-cases. It was also moved out middleware because it is used by `dnsserver` as well. A pkg/dnsutil packages was added for shared, handy, dns util functions. All normalize functions are now put in normalize.go
This commit is contained in:
		
							
								
								
									
										59
									
								
								middleware/pkg/storage/fs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								middleware/pkg/storage/fs.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,59 @@ | ||||
| package storage | ||||
|  | ||||
| import ( | ||||
| 	"net/http" | ||||
| 	"os" | ||||
| 	"path" | ||||
| 	"path/filepath" | ||||
| 	"runtime" | ||||
| ) | ||||
|  | ||||
| // dir wraps an http.Dir that restrict file access to a specific directory tree, see http.Dir's documentation | ||||
| // for methods for accessing files. | ||||
| type dir http.Dir | ||||
|  | ||||
| // CoreDir is the directory where middleware can store assets, like zone files after a zone transfer | ||||
| // or public and private keys or anything else a middleware might need. The convention is to place | ||||
| // assets in a subdirectory named after the zone prefixed with "D", to prevent the root zone become a hidden directory. | ||||
| // | ||||
| // Dexample.org/Kexample.org<something>.key | ||||
| // | ||||
| // Note that subzone(s) under example.org are places in the own directory under CoreDir: | ||||
| // | ||||
| // Dexample.org/... | ||||
| // Db.example.org/... | ||||
| // | ||||
| // CoreDir will default to "$HOME/.coredns" on Unix, but it's location can be overriden with the COREDNSPATH | ||||
| // environment variable. | ||||
| var CoreDir dir = dir(fsPath()) | ||||
|  | ||||
| func (d dir) Zone(z string) dir { | ||||
| 	if z != "." && z[len(z)-2] == '.' { | ||||
| 		return dir(path.Join(string(d), "D"+z[:len(z)-1])) | ||||
| 	} | ||||
| 	return dir(path.Join(string(d), "D"+z)) | ||||
| } | ||||
|  | ||||
| // fsPath returns the path to the directory where the application may store data. | ||||
| // If COREDNSPATH env variable. is set, that value is used. Otherwise, the path is | ||||
| // the result of evaluating "$HOME/.coredns". | ||||
| func fsPath() string { | ||||
| 	if corePath := os.Getenv("COREDNSPATH"); corePath != "" { | ||||
| 		return corePath | ||||
| 	} | ||||
| 	return filepath.Join(userHomeDir(), ".coredns") | ||||
| } | ||||
|  | ||||
| // userHomeDir returns the user's home directory according to environment variables. | ||||
| // | ||||
| // Credit: http://stackoverflow.com/a/7922977/1048862 | ||||
| func userHomeDir() string { | ||||
| 	if runtime.GOOS == "windows" { | ||||
| 		home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") | ||||
| 		if home == "" { | ||||
| 			home = os.Getenv("USERPROFILE") | ||||
| 		} | ||||
| 		return home | ||||
| 	} | ||||
| 	return os.Getenv("HOME") | ||||
| } | ||||
							
								
								
									
										42
									
								
								middleware/pkg/storage/fs_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								middleware/pkg/storage/fs_test.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,42 @@ | ||||
| package storage | ||||
|  | ||||
| import ( | ||||
| 	"os" | ||||
| 	"path" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestfsPath(t *testing.T) { | ||||
| 	if actual := fsPath(); !strings.HasSuffix(actual, ".coredns") { | ||||
| 		t.Errorf("Expected path to be a .coredns folder, got: %v", actual) | ||||
| 	} | ||||
|  | ||||
| 	os.Setenv("COREDNSPATH", "testpath") | ||||
| 	defer os.Setenv("COREDNSPATH", "") | ||||
| 	if actual, expected := fsPath(), "testpath"; actual != expected { | ||||
| 		t.Errorf("Expected path to be %v, got: %v", expected, actual) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestZone(t *testing.T) { | ||||
| 	for _, ts := range []string{"example.org.", "example.org"} { | ||||
| 		d := CoreDir.Zone(ts) | ||||
| 		actual := path.Base(string(d)) | ||||
| 		expected := "D" + ts | ||||
| 		if actual != expected { | ||||
| 			t.Errorf("Expected path to be %v, got %v", actual, expected) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func TestZoneRoot(t *testing.T) { | ||||
| 	for _, ts := range []string{"."} { | ||||
| 		d := CoreDir.Zone(ts) | ||||
| 		actual := path.Base(string(d)) | ||||
| 		expected := "D" + ts | ||||
| 		if actual != expected { | ||||
| 			t.Errorf("Expected path to be %v, got %v", actual, expected) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user