Properly load the full config file and allow overrides from the flags.
authorRobert Sesek <rsesek@bluestatic.org>
Sun, 14 Nov 2010 17:12:45 +0000 (12:12 -0500)
committerRobert Sesek <rsesek@bluestatic.org>
Sun, 14 Nov 2010 17:12:45 +0000 (12:12 -0500)
src/config.go
src/main.go
src/paths.go
src/server.go

index 8a48d78a554142c687e2058c210462e903c0c9b7..8b0a6085f80cf3c1f74ef5e93864390449934759 100644 (file)
@@ -15,9 +15,18 @@ import (
 )
 
 type Configuration struct {
+  // The path to which all file operations are restricted.
   JailRoot string
+
+  // The port on which the server back end runs.
   Port int
+
+  // An array of URLs that the /proxy service will for which the back-end will
+  // forward GET requests and return the result.
   ProxyURLs []string
+
+  // A map of usernames to MD5-encoded passwords that will be allowed to log in
+  // via a .htaccess style realm.
   Users map [string] string
 }
 
index 6c63faf2d5ba793618085f96599bd4c629832444..2671e1310f929cee6b051ef4873e239148cafa89 100644 (file)
@@ -18,11 +18,13 @@ import (
 )
 
 func main() {
+  // Set up the basic flags.
   var configPath *string = flag.String("config", "~/.armadillo", "Path to the configuration file")
-  flag.StringVar(&paths.JailRoot, "jail", "/", "Restrict file operations to this directory root")
-  var port *int = flag.Int("port", 8080, "Port to run the server on")
+  var jailRoot *string = flag.String("jail", "", "Restrict file operations to this directory root")
+  var port *int = flag.Int("port", 0, "Port to run the server on")
   flag.Parse()
 
+  // Load the configuration file, if it is present.
   var configuration = new(config.Configuration)
   fmt.Printf("Reading configuration from %v\n", *configPath)
   if len(*configPath) > 0 {
@@ -32,9 +34,17 @@ func main() {
     }
   }
 
-  configuration.JailRoot = paths.JailRoot
-  configuration.Port = *port
+  // Override configuration values with command line arguments.
+  if *jailRoot != "" {
+    configuration.JailRoot = *jailRoot
+  }
+  if *port != 0 {
+    configuration.Port = *port
+  }
 
-  fmt.Printf("Starting Armadillo on port %d with root:\n  %v\n", *port, paths.JailRoot)
+  // Run the server.
+  fmt.Printf("Starting Armadillo on port %d with root:\n  %v\n",
+      configuration.Port, configuration.JailRoot)
+  paths.SetConfig(configuration)
   server.RunBackEnd(configuration)
 }
index 8125bf3e5dc739536c3f77f1d7d0d2208f353038..a589bb6fbcaf93a713bb7ceba8c609b0e5e871b2 100644 (file)
@@ -14,20 +14,25 @@ import (
   "os"
   "path"
   "strings"
+  "./config"
 )
 
-var JailRoot string;
+var gJailRoot string;
+
+func SetConfig(aConfig *config.Configuration) {
+  gJailRoot = aConfig.JailRoot
+}
 
 func canonicalizePath(raw_path string) string {
-  raw_path = path.Join(JailRoot, raw_path)
+  raw_path = path.Join(gJailRoot, raw_path)
   return path.Clean(raw_path)
 }
 
 func checkInJail(the_path string) bool {
-  if len(the_path) < len(JailRoot) {
+  if len(the_path) < len(gJailRoot) {
     return false
   }
-  if the_path[0:len(JailRoot)] != JailRoot {
+  if the_path[0:len(gJailRoot)] != gJailRoot {
     return false
   }
   if strings.Index(the_path, "../") != -1 {
index 4f500d00febc7652ff3c89e6d713a1aebbeb997d..6080914c9d629e2f196bbbe5266c76473b706fa3 100644 (file)
@@ -128,7 +128,7 @@ func performProxy(url *http.URL, response *http.Conn, origRequest *http.Request)
 }
 
 func errorResponse(connection *http.Conn, message string) {
-  message = strings.Replace(message, paths.JailRoot, "/", -1)
+  message = strings.Replace(message, gConfig.JailRoot, "/", -1)
   response := map[string] string {
     "error"   : "-1",
     "message" : message,