Example #1
0
func setup(t *testing.T) *gocql.Session {
	flag.Set("config", "../config.toml")
	core.NewConfig()
	//	if err != nil {
	//		t.Fatal(err.Error())
	//	}
	session := core.NewCassandraWConn()
	//create table
	/*if err := session.Query(`CREATE TABLE IF NOT EXISTS message (owner varchar, sender varchar, id timeuuid, payload varchar, PRIMARY KEY ((owner, sender),id)) WITH CLUSTERING ORDER BY (id DESC)`).Exec(); err != nil {
		t.Log("can not create table message")
		t.Fatal(err.Error())
	}*/
	return session
}
Example #2
0
func NewServer(sh chan bool) {
	serverOnce.Do(func() {
		shutdownCh = sh
		flag.Parse()
		go hub.run()
		r := mux.NewRouter()

		var buffer bytes.Buffer
		buffer.WriteString(":")
		buffer.WriteString(core.NewConfig().HttpConfig.Port)

		jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options{
			ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
				return []byte(core.NewConfig().JWTConfig.Secret), nil
			},
			Debug: true,
			Extractor: jwtmiddleware.FromFirst(jwtmiddleware.FromAuthHeader,
				func(r *http.Request) (string, error) {
					c, err := r.Cookie("Authentication")
					if err != nil {
						return "", err
					}
					authHeaderParts := strings.Split(c.Value, " ")
					if len(authHeaderParts) != 2 || strings.ToLower(authHeaderParts[0]) != "bearer" {
						return "", fmt.Errorf("Authorization header format must be Bearer {token}")
					}
					return authHeaderParts[1], nil
				}),

			SigningMethod: jwt.SigningMethodHS256,
		})

		n := negroni.Classic()
		for _, v := range routes {
			if v.auth {
				r.Handle(v.path, negroni.New(
					negroni.HandlerFunc(jwtMiddleware.HandlerWithNext),
					negroni.Wrap(http.HandlerFunc(v.handleFunc)),
				)).Methods(v.method)
			} else {
				r.HandleFunc(v.path, v.handleFunc).Methods(v.method)
			}
		}

		n.UseHandler(r)
		server = &graceful.Server{
			Timeout: 10 * time.Second,
			Server: &http.Server{
				Addr:    buffer.String(),
				Handler: n,
			},
			BeforeShutdown: func() bool {
				log.Println("bye")
				return true
			},
		}

		err := server.ListenAndServe()
		if err != nil {
			log.Fatal("ListenAndServe: ", err)
		}
	})
}