예제 #1
0
파일: server.go 프로젝트: luizbafilho/fusis
func main() {
	e := echo.New()

	// Middleware
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	// Login route
	e.POST("/login", login)

	// Unauthenticated route
	e.GET("/", accessible)

	// Restricted group
	r := e.Group("/restricted")

	// Configure middleware with the custom claims type
	config := middleware.JWTConfig{
		Claims:     &jwtCustomClaims{},
		SigningKey: []byte("secret"),
	}
	r.Use(middleware.JWTWithConfig(config))
	r.GET("", restricted)

	e.Logger.Fatal(e.Start(":1323"))
}
예제 #2
0
파일: main.go 프로젝트: hexadecy/unaframe
func main() {
	// Echo instance
	e := echo.New()

	// Middleware echo
	e.Use(middleware.Logger())
	logg := e.Logger()

	e.Use(middleware.Recover())
	e.Use(middleware.Gzip())
	e.Use(middleware.Secure())
	e.Use(middleware.CORS())
	e.Use(middleware.BodyLimit("200M"))

	// gopkg.in/mgo.v2
	db := midd.NewMgo(viper.GetString("db.url"), viper.GetString("db.name"))
	defer db.Session.Close()
	e.Use(midd.Mgo(db))

	// github.com/thoas/stats
	middStat := stats.New()
	e.Use(standard.WrapMiddleware(middStat.Handler))
	// Route for stats
	e.Get("/stats", echo.HandlerFunc(func(c echo.Context) error {
		return c.JSON(http.StatusOK, middStat.Data())
	}))

	// API v1 Restricted group Token in header
	api := e.Group("/apiv1")
	api.Use(middleware.JWTWithConfig(middleware.JWTConfig{
		SigningKey:  []byte(midd.SigningKey),
		TokenLookup: "header:Authorization"}))
	api.Post("/user", handler.PostUser)
	api.Get("/user/:id", handler.GetUser)

	// API v1 Token as first param
	apit := e.Group("/apiv1token")
	apit.Use(middleware.JWTWithConfig(middleware.JWTConfig{
		SigningKey:  []byte(midd.SigningKey),
		TokenLookup: "query:token"}))
	apit.Post("/user", handler.PostUser)
	apit.Get("/user/:id", handler.GetUser)

	// Serve index file
	e.File("/", "public/index.html")
	// Serve favicon
	e.File("/favicon.ico", "public/favicon.ico")
	// Serve static files
	e.Static("/scripts", "public/scripts")

	midd.GenerateToken()

	// Get server info
	var svrHostname string

	if runtime.GOOS == "windows" {
		svrHostname = fmt.Sprintf("%s.%s", os.Getenv("COMPUTERNAME"), os.Getenv("USERDNSDOMAIN"))
	} else {
		svrHostname, _ = os.Hostname()
	}
	webInterfacePort := viper.GetString("web.interface")

	logg.Printf("%s for %s %s on %d cores\n%s%s\n",
		runtime.Version(), runtime.GOOS, runtime.GOARCH, runtime.NumCPU(), svrHostname, webInterfacePort)

	mgoInfo, _ := db.Session.BuildInfo()
	logg.Printf("MongoDB %s %d bits\n", mgoInfo.Version, mgoInfo.Bits)

	// github.com/tylerb/graceful
	std := standard.New(webInterfacePort)
	std.SetHandler(e)
	graceful.ListenAndServe(std.Server, 5*time.Second)
}