示例#1
0
//dbUrl:"mongodb://*****:*****@localhost:27017/hamster"
//serverUrl:fmt.Sprintf("%s:%d", address, port)
//creates a new server, setups logging etc.
func NewServer() *Server {
	f, err := os.OpenFile("hamster.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		fmt.Println("hamster.log faied to open")

	}
	//log.SetOutput(f)
	//log.SetOutput(os.Stdout)
	//router
	r := routes.New()
	//toml config
	var cfg Config
	if _, err := toml.DecodeFile("hamster.toml", &cfg); err != nil {
		fmt.Println(err)
		return nil
	}
	//cookie store
	ck := sessions.NewCookieStore([]byte(cfg.Servers["local"].CookieSecret))

	//redis
	var getRedis = func() redis.Conn {

		c, err := redis.Dial("tcp", ":6379")
		if err != nil {
			panic(err)
		}

		return c

	}

	//initialize server
	s := &Server{
		httpServer: &http.Server{Addr: fmt.Sprintf(":%d", cfg.Servers["local"].Port), Handler: r},
		route:      r,
		logger:     log.New(f, "", log.LstdFlags),
		db:         &Db{Url: cfg.DB["mongo"].Host},
		config:     &cfg,
		cookie:     ck,
		redisConn:  getRedis,
	}

	s.logger.SetFlags(log.Lshortfile)
	s.addHandlers()

	return s

}
示例#2
0
//NewServer creates a new server
//dbUrl:"mongodb://*****:*****@localhost:27017/hamster"
//db.addUser( { user: "******",pwd: "pass",roles: [ "readWrite" ] } )
//serverUrl:fmt.Sprintf("%s:%d", address, port)
//creates a new server, setups logging etc.
func NewServer(configPath string) *Server {
	f, err := os.OpenFile("hamster.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		fmt.Println("hamster.log faied to open")

	}
	//log.SetOutput(f)
	//log.SetOutput(os.Stdout)
	//router
	r := routes.New()
	//toml config
	var cfg config
	if _, err := toml.DecodeFile("hamster.toml", &cfg); err != nil {
		fmt.Println(err)
		return nil
	}
	//cookie store
	ck := sessions.NewCookieStore([]byte(cfg.Servers["local"].CookieSecret))

	//redis
	var getRedis = func() redis.Conn {

		c, err := redis.Dial("tcp", os.Getenv("REDIS_URL"))
		if err != nil {
			panic(err)
		}

		return c

	}

	//initialize server
	s := &Server{
		httpServer: &http.Server{Addr: ":" + os.Getenv("SERVER_PORT"), Handler: r},
		route:      r,
		logger:     log.New(f, "", log.LstdFlags),
		db:         &db{URL: os.Getenv("MONGODB_URL")},
		config:     &cfg,
		cookie:     ck,
		redisConn:  getRedis,
	}

	s.logger.SetFlags(log.Lshortfile)
	s.addHandlers()

	return s

}