Beispiel #1
0
// Serve sets up and starts the server
func (s *APIServer) Serve() error {
	e := echo.New()
	e.Debug = true
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())
	e.Use(middleware.CORS())
	e.Use(middleware.BodyLimit("1024K"))
	e.Use(middleware.Gzip())

	// Serve the ember app
	e.File("/", "assets/index.html")
	e.Static("/assets", "assets/assets")

	//	e.OPTIONS("/api/login/", s.updateUser)
	e.POST("/api/login/", s.login)

	// Restricted group
	r := e.Group("/api/v1")
	r.Use(middleware.JWT([]byte("secret")))
	r.Use(s.getDBUser)
	r.GET("/feeds", s.getFeeds)
	r.POST("/feeds", s.addFeed)
	r.GET("/feeds/:id", s.getFeed)
	r.PATCH("/feeds/:id", s.updateFeed)
	r.PUT("/feeds/:id/subscribe", s.subFeed)
	r.PUT("/feeds/:id/unsubscribe", s.unsubFeed)

	customServer := &http.Server{
		Addr:           fmt.Sprintf(":%d", s.Port),
		ReadTimeout:    20 * time.Second,
		WriteTimeout:   20 * time.Second,
		MaxHeaderBytes: 2048,
	}

	err := e.StartServer(customServer)
	if err != nil {
		return fmt.Errorf("Error starting server: %s", err)
	}
	return nil
}
Beispiel #2
0
// 设置Body大小
func BodyLimit(str string) {

	Echo.Use(middleware.BodyLimit(str))
}
Beispiel #3
0
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)
}
Beispiel #4
0
func main() {
	app := echo.New()
	// create new Studio
	utils.ST = badactor.NewStudio(1024) // studio capacity... RAM?

	// add the rule to the stack
	utils.ST.AddRule(mw.LoginRule)
	err := utils.ST.CreateDirectors(1024)
	if err != nil {
		log.Fatal(err)
	}

	//poll duration
	dur := time.Minute * time.Duration(60)
	// Start the reaper
	utils.ST.StartReaper(dur)

	app.Use(middleware.Recover())
	app.Use(middleware.Gzip())
	app.Use(middleware.Secure())
	app.Use(middleware.BodyLimit("100K"))

	api := app.Group("/api")

	if config.Mode == "dev" {
		// Enable Debug
		app.Use(middleware.Logger())
		app.SetDebug(true)
		corsEnabled := middleware.CORSWithConfig(middleware.CORSConfig{
			AllowHeaders: []string{
				echo.HeaderOrigin,
				echo.HeaderContentType,
				echo.HeaderAcceptEncoding,
				echo.HeaderAuthorization,
			},
		})
		// Enable CORS /
		app.Use(corsEnabled)
		// Enable CORS /api
		api.Use(corsEnabled)
	}

	// admin routing
	// app.Static("/admin", "public/admin")
	app.Static("/admin/assets", "public/admin/assets")
	app.Static("/tinymce", "public/tinymce")
	app.File("/admin*", "public/admin/index.html")
	app.File("/admin/*", "public/admin/index.html")

	// Public
	// Setup Theme
	theme.Setup(app, config.Mode)

	users := controllers.UsersController{}
	app.POST("/auth/login", users.Login, mw.Jailer)
	app.POST("/auth/delegation", users.Delegate)

	// Get jwt signingkey
	signingKey, err := models.GetSettingString("admin_signingkey")
	if err != nil {
		panic(err)
	}
	// get api routes
	api.Use(middleware.JWT([]byte(signingKey)))

	APIRoutes(api)

	// ======= SITES =====
	site := controllers.SiteController{}
	app.GET("/", site.Home)
	app.GET("/:slug", site.Single)
	app.GET("/sitemap.xml", site.Sitemap)

	app.Run(fasthttp.New(config.Port))
}