示例#1
0
func main() {
	var cfgfile *string = flag.String("config", "", "configuration file")
	flag.Parse()

	if *cfgfile == "" {
		flag.Usage()
		os.Exit(1)
	}

	log.Printf("this is activities, revision %s", gitRevision)
	log.Printf("GOMAXPROCS = %d", runtime.GOMAXPROCS(0))
	log.Printf("NumCPU = %d", runtime.NumCPU())

	cfg, err := goconf.ReadConfigFile(*cfgfile)

	// TODO: add error handling
	driver, _ := cfg.GetString("database", "driver")
	dsn, _ := cfg.GetString("database", "dsn")
	auth_key, _ := cfg.GetString("sessions", "authkey")
	enc_key, _ := cfg.GetString("sessions", "enckey")

	db_handle, err := sql.Open(driver, dsn)
	if err != nil {
		log.Fatalf("sql.Open: %v", err)
	}
	defer db_handle.Close()

	db := NewDatabase(db_handle)

	store := sessions.NewCookieStore([]byte(auth_key), []byte(enc_key))

	r := pat.New()

	r.Add("POST", "/auth/try", &TryAuthenticateHandler{Db: db, Store: store})
	r.Add("POST", "/auth/signup", &SignupHandler{Db: db})
	r.Add("POST", "/auth/logout", &LogoutHandler{Store: store})
	r.Add("POST", "/auth", &AuthenticateHandler{Db: db, Store: store})
	r.Add("POST", "/activity/add", &AddActivityHandler{Store: store, Db: db})
	r.Add("GET", "/activity/list/{page:[0-9]+}", &ListActivitiesHandler{Db: db, Store: store})
	r.Add("POST", "/activity/type/add", &AddActivityTypeHandler{Db: db, Store: store})
	r.Add("POST", "/activity/type/edit", &EditActivityTypeHandler{Db: db, Store: store})
	r.Add("POST", "/activity/type/del", &DeleteActivityTypeHandler{Db: db, Store: store})
	r.Add("GET", "/activity/type/list", &ListActivityTypesHandler{Db: db, Store: store})
	r.Add("GET", "/activity/latest", &LatestActivitiesHandler{Db: db, Store: store})
	r.Add("GET", "/", http.FileServer(http.Dir("htdocs")))

	httpsrv := &http.Server{Handler: Logger(r), Addr: ":8000"}
	if err := httpsrv.ListenAndServe(); err != nil {
		log.Fatalf("ListenAndServe: %v", err)
	}
}
示例#2
0
func main() {
	var cfgfile *string = flag.String("config", "", "configuration file")
	flag.Parse()

	if *cfgfile == "" {
		flag.Usage()
		os.Exit(1)
	}

	cfg, err := goconf.ReadConfigFile(*cfgfile)

	// TODO: add error handling
	driver, _ := cfg.GetString("database", "driver")
	dsn, _ := cfg.GetString("database", "dsn")
	auth_key, _ := cfg.GetString("sessions", "authkey")
	enc_key, _ := cfg.GetString("sessions", "enckey")

	db, err = sql.Open(driver, dsn)
	if err != nil {
		log.Fatalf("sql.Open: %v", err)
	}
	defer db.Close()

	store = sessions.NewCookieStore([]byte(auth_key), []byte(enc_key))

	r := pat.New()

	r.Post("/auth/try", http.HandlerFunc(TryAuthenticate))
	r.Post("/auth/signup", http.HandlerFunc(Signup))
	r.Post("/auth/logout", http.HandlerFunc(Logout))
	r.Post("/auth", http.HandlerFunc(Authenticate))
	r.Post("/activity/add", http.HandlerFunc(AddActivity))
	r.Get("/activity/list/{page:[0-9]+}", http.HandlerFunc(ListActivities))
	r.Post("/activity/type/add", http.HandlerFunc(AddActivityType))
	r.Post("/activity/type/edit", http.HandlerFunc(EditActivityType))
	r.Post("/activity/type/del", http.HandlerFunc(DeleteActivityType))
	r.Get("/activity/type/list", http.HandlerFunc(ListActivityTypes))

	r.Get("/activity/latest", http.HandlerFunc(LatestActivities))
	r.Add("GET", "/", http.FileServer(http.Dir("htdocs")))

	httpsrv := &http.Server{Handler: r, Addr: ":8000"}
	if err := httpsrv.ListenAndServe(); err != nil {
		log.Fatalf("ListenAndServe: %v", err)
	}
}
示例#3
0
文件: mpm.go 项目: ZhuoRoger/mymon
func (conf *Cfg) readConf(file string) error {
	c, err := goconf.ReadConfigFile(file)
	if err != nil {
		return err
	}

	conf.LogFile, err = c.GetString("default", "log_file")
	if err != nil {
		return err
	}

	conf.LogLevel, err = c.GetInt("default", "log_level")
	if err != nil {
		return err
	}

	conf.FalconClient, err = c.GetString("default", "falcon_client")
	if err != nil {
		return err
	}

	conf.Endpoint, err = c.GetString("default", "endpoint")
	if err != nil {
		return err
	}

	conf.User, err = c.GetString("mysql", "user")
	if err != nil {
		return err
	}

	conf.Pass, err = c.GetString("mysql", "password")
	if err != nil {
		return err
	}

	conf.Host, err = c.GetString("mysql", "host")
	if err != nil {
		return err
	}

	conf.Port, err = c.GetInt("mysql", "port")
	return err
}
示例#4
0
func main() {
	var cfgfile *string = flag.String("config", "", "configuration file")

	backends := make(map[string]*Backend)
	hosts := make(map[string][]*Backend)
	frontends := make(map[string]*Frontend)

	flag.Parse()

	if *cfgfile == "" {
		usage()
	}

	cfg, err := goconf.ReadConfigFile(*cfgfile)
	if err != nil {
		log.Printf("opening %s failed: %v", *cfgfile, err)
		os.Exit(1)
	}

	var access_f io.WriteCloser
	accesslog_file, err := cfg.GetString("global", "accesslog")
	if err == nil {
		access_f, err = os.OpenFile(accesslog_file, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
		if err == nil {
			defer access_f.Close()
		} else {
			log.Printf("Opening access log %s failed: %v", accesslog_file, err)
		}
	}

	// first, extract backends
	for _, section := range cfg.GetSections() {
		if strings.HasPrefix(section, "backend ") {
			tokens := strings.Split(section, " ")
			if len(tokens) < 2 {
				log.Printf("backend section has no name, ignoring.")
				continue
			}
			connect_str, _ := cfg.GetString(section, "connect")
			if connect_str == "" {
				log.Printf("empty connect string for backend %s, ignoring.", tokens[1])
				continue
			}
			b := &Backend{Name: tokens[1], ConnectString: connect_str}
			backends[b.Name] = b
		}
	}

	// then extract hosts
	for _, section := range cfg.GetSections() {
		if strings.HasPrefix(section, "host ") {
			tokens := strings.Split(section, " ")
			if len(tokens) < 2 {
				log.Printf("host section has no name, ignoring.")
				continue
			}
			backends_str, _ := cfg.GetString(section, "backends")
			backends_list := strings.Split(backends_str, " ")
			if len(backends_list) == 0 {
				log.Printf("host %s has no backends, ignoring.", tokens[1])
				continue
			}
			for _, host := range tokens[1:] {
				backends_for_host := []*Backend{}
				for _, backend := range backends_list {
					b := backends[backend]
					if b == nil {
						log.Printf("backend %s doesn't exist, ignoring.", backend)
					}
					backends_for_host = append(backends_for_host, b)
				}
				hosts[host] = backends_for_host
			}
		}
	}

	// and finally, extract frontends
	for _, section := range cfg.GetSections() {
		if strings.HasPrefix(section, "frontend ") {
			tokens := strings.Split(section, " ")
			if len(tokens) < 2 {
				log.Printf("frontend section has no name, ignoring.")
				continue
			}

			frontend_name := tokens[1]

			frontend := &Frontend{}
			frontend.Name = frontend_name
			frontend.BindString, err = cfg.GetString(section, "bind")
			if err != nil {
				log.Printf("error while getting [%s]bind: %v, ignoring.", section, err)
				continue
			}
			if frontend.BindString == "" {
				log.Printf("frontend %s has no bind argument, ignoring.", frontend_name)
				continue
			}

			frontend.HTTPS, err = cfg.GetBool(section, "https")
			if err != nil {
				frontend.HTTPS = false
			}

			if frontend.HTTPS {
				frontend.KeyFile, err = cfg.GetString(section, "keyfile")
				if err != nil {
					log.Printf("error while getting[%s]keyfile: %v, ignoring.", section, err)
					continue
				}
				if frontend.KeyFile == "" {
					log.Printf("frontend %s has HTTPS enabled but no keyfile, ignoring.", frontend_name)
					continue
				}

				frontend.CertFile, err = cfg.GetString(section, "certfile")
				if err != nil {
					log.Printf("error while getting[%s]certfile: %v, ignoring.", section, err)
					continue
				}
				if frontend.CertFile == "" {
					log.Printf("frontend %s has HTTPS enabled but no certfile, ignoring.", frontend_name)
					continue
				}
			}

			frontend_hosts, err := cfg.GetString(section, "hosts")
			if err == nil && frontend_hosts != "" {
				frontend.Hosts = strings.Split(frontend_hosts, " ")
			}

			frontend_backends, err := cfg.GetString(section, "backends")
			if err == nil && frontend_backends != "" {
				frontend.Backends = strings.Split(frontend_backends, " ")
			}

			frontend.AddForwarded, _ = cfg.GetBool(section, "add-x-forwarded-for")

			if len(frontend.Backends) == 0 && len(frontend.Hosts) == 0 {
				log.Printf("frontend %s has neither backends nor hosts configured, ignoring.", frontend_name)
				continue
			}

			frontends[frontend_name] = frontend
		}
	}

	count := 0
	exit_chan := make(chan int)
	for name, frontend := range frontends {
		log.Printf("Starting frontend %s...", name)
		go func(fe *Frontend, name string) {
			var accesslogger *log.Logger
			if access_f != nil {
				accesslogger = log.New(access_f, "frontend:"+name+" ", log.Ldate|log.Ltime|log.Lmicroseconds)
			} else {
				log.Printf("Not creating logger for frontend %s", name)
			}
			fe.Start(hosts, backends, accesslogger)
			exit_chan <- 1
		}(frontend, name)
		count++
	}

	// this shouldn't return
	for i := 0; i < count; i++ {
		<-exit_chan
	}
}
示例#5
0
func main() {
	homedir := getHomeDir()
	if homedir == "" {
		fmt.Printf("Error: unable to determine home directory!\n")
		return
	}

	cfgdir := homedir + "/.gockel"
	os.Mkdir(cfgdir, 0700)

	cfgfilename := cfgdir + "/gockelrc"

	var logfile *string = flag.String("log", "", "logfile")
	var cfgfile *string = flag.String("config", cfgfilename, "configuration file")
	var add *bool = flag.Bool("add", false, "add a new user")

	flag.Parse()

	log.SetFlags(log.LstdFlags | log.Lshortfile)
	devnull := new(DevNullWriter)
	log.SetOutput(devnull)

	if *logfile != "" {
		if f, err := os.OpenFile(*logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600); err == nil {
			defer f.Close()
			log.SetOutput(f)
		} else {
			fmt.Fprintf(os.Stderr, "Warning: couldn't open logfile for writing: %v\n", err)
		}
	}

	cfg, err := goconf.ReadConfigFile(*cfgfile)
	if err != nil {
		log.Printf("reading configuration file failed: %v", err)
	}

	tapi := NewTwitterAPI(CONSUMER_KEY, CONSUMER_SECRET, cfg)

	if *add {
		if err := AddUser(tapi, cfgdir); err != nil {
			log.Printf("Error while adding user: %v", err)
			fmt.Fprintf(os.Stderr, "Error while adding user: %v\n", err)
		}
		return
	}

	fmt.Printf("Starting %s %s...\n", PROGRAM_NAME, PROGRAM_VERSION)

	var users []UserTwitterAPITuple

	fmt.Printf("Loading user information...")
	for {
		users, err = LoadAccessTokens(cfgdir, cfg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Loading users failed: %v\n", err)
			return
		}
		if len(users) > 0 {
			break
		}
		err = AddUser(tapi, cfgdir)
		if err != nil {
			log.Printf("Error while adding user: %v", err)
			fmt.Fprintf(os.Stderr, "Error while adding user: %v\n", err)
			return
		}
	}

	fmt.Println(" done.")

	log.Printf("loaded %d users:", len(users))
	for _, u := range users {
		log.Printf("user: %s", u.User)
	}

	cmdchan := make(chan interface{}, 1)
	newtweetchan := make(chan []*Tweet, 10)
	lookupchan := make(chan TweetRequest, 1)
	uiactionchan := make(chan interface{}, 10)

	model := NewModel(users, cmdchan, newtweetchan, lookupchan, uiactionchan, cfg)
	go model.Run()

	ui := NewUserInterface(cmdchan, newtweetchan, lookupchan, uiactionchan, cfg)
	go ui.Run()

	ui.InputLoop()
}