コード例 #1
0
ファイル: main.go プロジェクト: xiwenc/cf-fastpush-controller
func main() {

	viper.SetConfigName("cf-fastpush-controller")
	viper.AddConfigPath("/etc/")
	viper.AddConfigPath("$HOME/.config/")
	viper.AddConfigPath(".")
	viper.ReadInConfig()
	viper.AutomaticEnv()

	viper.SetDefault(lib.CONFIG_BIND_ADDRESS, "0.0.0.0")
	viper.SetDefault(lib.CONFIG_PORT, "9000")
	viper.SetDefault(lib.CONFIG_BACKEND_DIRS, "./")
	viper.SetDefault(lib.CONFIG_RESTART_REGEX, "^*.py$")
	viper.SetDefault(lib.CONFIG_IGNORE_REGEX, "")
	viper.SetDefault(lib.CONFIG_BACKEND_COMMAND, "python -m http.server")
	viper.SetDefault(lib.CONFIG_BACKEND_PORT, "8080")
	viper.SetDefault(lib.CONFIG_BASE_PATH, "/_fastpush")

	appCmd = viper.GetString(lib.CONFIG_BACKEND_COMMAND)
	listenOn = viper.GetString(lib.CONFIG_BIND_ADDRESS) + ":" + viper.GetString(lib.CONFIG_PORT)
	backendOn = "127.0.0.1:" + viper.GetString(lib.CONFIG_BACKEND_PORT)
	basePath := viper.GetString(lib.CONFIG_BASE_PATH)
	localAuthToken := GetLocalToken()

	log.Println("Controller listening to: " + listenOn)

	http.HandleFunc(basePath+"/files", func(w http.ResponseWriter, r *http.Request) {
		SetJsonContentType(w)
		if !IsAuthenticated(r, localAuthToken) {
			http.Error(w, "Invalid authentication token", http.StatusUnauthorized)
			return
		}
		if r.Method == "GET" {
			ListFiles(w, r)
		} else if r.Method == "PUT" {
			UploadFiles(w, r)
		} else {
			http.Error(w, "Invalid request method.", http.StatusMethodNotAllowed)
		}
	})
	http.HandleFunc(basePath+"/restart", func(w http.ResponseWriter, r *http.Request) {
		SetJsonContentType(w)
		if !IsAuthenticated(r, localAuthToken) {
			http.Error(w, "Invalid authentication token", http.StatusUnauthorized)
			return
		}
		if r.Method == "POST" {
			RestartApp(w, r)
		} else {
			http.Error(w, "Invalid request method.", http.StatusMethodNotAllowed)
		}
	})
	http.HandleFunc(basePath+"/status", func(w http.ResponseWriter, r *http.Request) {
		SetJsonContentType(w)
		if !IsAuthenticated(r, localAuthToken) {
			http.Error(w, "Invalid authentication token", http.StatusUnauthorized)
			return
		}
		if r.Method == "GET" {
			GetStatus(w, r)
		} else {
			http.Error(w, "Invalid request method.", http.StatusMethodNotAllowed)
		}
	})
	reverseProxy := httputil.NewSingleHostReverseProxy(&url.URL{
		Scheme: "http",
		Host:   backendOn,
	})
	http.HandleFunc("/", ReverseProxyHandler(reverseProxy))

	go lib.RestartApp(appCmd)
	go lib.ListFiles()
	http.ListenAndServe(listenOn, nil)
}
コード例 #2
0
ファイル: main.go プロジェクト: xiwenc/cf-fastpush-controller
func RestartApp(w http.ResponseWriter, r *http.Request) {
	result := lib.RestartApp(appCmd)
	json.NewEncoder(w).Encode(result)
}