Example #1
0
File: ctpd.go Project: ehengzh/ctpd
func main() {
	var ok bool
	var conf ctp.Configuration

	flag.Parse()

	if versionFlag {
		fmt.Println("ctpd version %f.", CTPD_VERSION)
		fmt.Println(" Copyright 2015 Cloud Security Alliance EMEA (cloudsecurityalliance.org).")
		fmt.Println(" ctpd is licensed under the Apache License, Version 2.0.")
		fmt.Println(" see http://www.apache.org/licenses/LICENSE-2.0")
		fmt.Println("")
		return
	}

	if helpFlag {
		fmt.Fprintf(os.Stderr, "Usage: %s [flags]\n", os.Args[0])
		flag.PrintDefaults()
		return
	}

	if configFileFlag == "/path/to/file" {
		conf, ok = ctp.SearchAndLoadConfigurationFile()
	} else {
		conf, ok = ctp.LoadConfigurationFromFile(configFileFlag)
	}

	if !ok {
		log.Printf("No configuration file was loaded, using defaults.")
		conf = ctp.ConfigurationDefaults
	}

	if conf["client"] != "" {
		http.Handle("/", http.FileServer(http.Dir(conf["client"])))
	}

	http.Handle(conf["basepath"], server.NewCtpApiHandlerMux(conf))
	if conf["tls_use"] != "" && conf["tls_use"] != "no" {
		if conf["tls_use"] != "yes" {
			log.Fatal("Configuration: tls_use must be either 'yes' or 'no'")
		}
		if conf["tls_key_file"] == "" || conf["tls_cert_file"] == "" {
			log.Fatal("Missing tls_key_file or tls_cert_file in configuration.")
		}
		log.Printf("Starting ctpd with TLS enabled at %s", conf["listen"])
		log.Fatal(http.ListenAndServeTLS(conf["listen"], conf["tls_cert_file"], conf["tls_key_file"], nil))
	} else {
		log.Printf("Starting ctpd at %s", conf["listen"])
		log.Fatal(http.ListenAndServe(conf["listen"], nil))
	}
}
Example #2
0
func main() {
	var ok bool
	var conf ctp.Configuration

	flag.Parse()

	if versionFlag {
		fmt.Println("ctpd version", CTPD_VERSION)
		fmt.Println(" Copyright 2015 Cloud Security Alliance EMEA (cloudsecurityalliance.org).")
		fmt.Println(" ctpd is licensed under the Apache License, Version 2.0.")
		fmt.Println(" see http://www.apache.org/licenses/LICENSE-2.0")
		fmt.Println("")
		return
	}

	if helpFlag {
		fmt.Fprintf(os.Stderr, "Usage: %s [flags]\n", path.Base(os.Args[0]))
		flag.PrintDefaults()
		return
	}

	if configFileFlag == "/path/to/file" {
		conf, ok = ctp.SearchAndLoadConfigurationFile()
	} else {
		conf, ok = ctp.LoadConfigurationFromFile(configFileFlag)
	}

	if !ok {
		ctp.Log(nil, ctp.INFO, "No configuration file was loaded, using defaults.")
		conf = ctp.ConfigurationDefaults
	}

	if logfileFlag != "" {
		conf["log-file"] = logfileFlag
	}

	if conf["log-file"] != "" {
		file, err := os.Create(conf["log-file"])
		if err != nil {
			log.Fatalf("Could not open %s, %s", conf["log-file"], err.Error())
		}
		defer file.Close()
		log.SetOutput(file)
	}

	if colorFlag {
		conf["color-logs"] = "true"
	}

	if clientFlag != "" {
		conf["client"] = clientFlag
	}

	if debugVMFlag {
		conf["debug-vm"] = "true"
	}

	if conf["client"] != "" {
		http.Handle("/", http.FileServer(http.Dir(conf["client"])))
	}

	if !ctp.IsMongoRunning(conf) {
		log.Fatal("Missing mongodb.")
	}

	http.Handle(conf["basepath"], server.NewCtpApiHandlerMux(conf))
	if conf["tls_use"] != "" && conf["tls_use"] != "no" {
		if conf["tls_use"] != "yes" {
			log.Fatal("Configuration: tls_use must be either 'yes' or 'no'")
		}
		if conf["tls_key_file"] == "" || conf["tls_cert_file"] == "" {
			log.Fatal("Missing tls_key_file or tls_cert_file in configuration.")
		}
		ctp.Log(nil, ctp.INFO, "Starting ctpd with TLS enabled at %s", conf["listen"])
		log.Fatal(http.ListenAndServeTLS(conf["listen"], conf["tls_cert_file"], conf["tls_key_file"], nil))
	} else {
		ctp.Log(nil, ctp.INFO, "Starting ctpd at %s", conf["listen"])
		log.Fatal(http.ListenAndServe(conf["listen"], nil))
	}
}