예제 #1
0
func main() {
	initConfig()

	defer initLogging(viper.GetString(ConfLogFilename))()
	defer initConfigWatcher()()

	e := echo.New()

	e.Use(middleware.Logger())
	e.Use(middleware.Recover())
	//e.Use(middleware.CORSWithConfig(viper.Get(ConfCorsConfig).(middleware.CORSConfig)))
	e.Use(middleware.Secure())
	g := e.Group("/api")
	NewCommandService(viper.GetString(ConfCommandsFolder), g)
	e.Static("/", viper.GetString(ConfStaticFolder))
	log.Println("Listening on:" + viper.GetString(ConfListen))
	e.Run(standard.New(viper.GetString(ConfListen)))
}
예제 #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)
}
예제 #3
0
파일: main.go 프로젝트: mewben/onix
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))
}