Ejemplo n.º 1
0
func Init() *echo.Echo {

	e := echo.New()

	e.Debug()

	// Set Bundle MiddleWare
	e.Use(echoMw.Logger())
	e.Use(echoMw.Gzip())
	e.Use(echoMw.CORSWithConfig(echoMw.CORSConfig{
		AllowOrigins: []string{"*"},
		AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAcceptEncoding},
	}))
	e.SetHTTPErrorHandler(handler.JSONHTTPErrorHandler)

	// Set Custom MiddleWare
	e.Use(myMw.TransactionHandler(db.Init()))

	// Routes
	v1 := e.Group("/api/v1")
	{
		v1.POST("/members", api.PostMember())
		v1.GET("/members", api.GetMembers())
		v1.GET("/members/:id", api.GetMember())
	}
	return e
}
Ejemplo n.º 2
0
func main() {
	e := echo.New()
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	// CORS default
	// Allows requests from any origin wth GET, HEAD, PUT, POST or DELETE method.
	// e.Use(middleware.CORS())

	// CORS restricted
	// Allows requests from any `https://labstack.com` or `https://labstack.net` origin
	// wth GET, PUT, POST or DELETE method.
	e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
		AllowOrigins: []string{"https://labstack.com", "https://labstack.net"},
		AllowMethods: []string{echo.GET, echo.PUT, echo.POST, echo.DELETE},
	}))

	e.GET("/api/users", getUsers)
	e.Run(standard.New(":1323"))
}
Ejemplo n.º 3
0
// NewRouter creates echo router with all application endpoints
func (a *App) NewRouter() *echo.Echo {
	// Create router with default middleware
	router := echo.New()

	// Implementing C.O.R.S. headers enable pages within a modern web browser
	// to consume resources (such as REST APIs) from servers that are on a different domain.
	router.Use(middleware.CORSWithConfig(middleware.CORSConfig{
		AllowOrigins: []string{"*"},
		AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE},
	}))

	// Logger middleware
	router.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
		Format: `${time_rfc3339} | ${method} | ${uri} | ${status} | ${latency_human}` + "\n",
	}))

	// Handle static files
	router.Static("/", "public")

	return router
}
Ejemplo n.º 4
0
func Init() *echo.Echo {
	t := &Template{
		templates: template.Must(template.ParseGlob("templates/*.html")),
	}
	e := echo.New()
	e.Static("/assets", "public")

	e.Debug()

	e.Use(mw.Logger())
	e.Use(mw.Recover())
	e.Use(mw.Gzip())
	e.Use(mw.CORSWithConfig(mw.CORSConfig{
		AllowOrigins: []string{"*"},
		AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAcceptEncoding},
	}))
	e.SetHTTPErrorHandler(handler.JSONHTTPErrorHandler)

	e.SetRenderer(t)

	// set custome middleware
	e.Use(sckMw.TransactionHandler(db.Init()))

	// view
	e.GET("/", handler.Home)
	e.GET("/snippet", handler.Snippet)
	e.GET("/create", handler.SnippetCreate)

	// api
	v1 := e.Group("/api/v1")
	{
		v1.POST("/snippet/create", api.Create())
		v1.POST("/preview", api.Preview())
	}
	return e
}
Ejemplo n.º 5
0
Archivo: main.go Proyecto: 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))
}
Ejemplo n.º 6
0
func main() {

	e := echo.New()
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())
	e.GET("/", func(context echo.Context) error {
		return context.String(http.StatusOK, "Hello Blue")
	})
	e.GET("/search/address", func(context echo.Context) error {
		postalcode := context.QueryParam("postalcode")
		var result = common.SearchAddress(postalcode)
		pp.Printf("SearchAddress result: %s", result)
		return context.JSON(http.StatusOK, result)
	})
	e.GET("/customer", func(context echo.Context) error {
		var customers = customer.GetCustomer()
		return context.JSON(http.StatusOK, customers)
	})
	e.POST("/customer/save", func(context echo.Context) error {
		c := new(customer.Customer)
		// リクエストをCustomerにバインドします。
		if err := context.Bind(c); err != nil {
			return err
		}

		savedCustomer, err := customer.SaveCustomer(c)
		if err != nil {
			return err
		}

		return context.JSON(http.StatusOK, savedCustomer)
	})
	e.POST("/customer/delete", func(context echo.Context) error {
		c := new(customer.Customer)
		if err := context.Bind(c); err != nil {
			return err
		}

		deletedCustomer, err := customer.DeleteCustomer(c)
		if err != nil {
			return err
		}

		return context.JSON(http.StatusOK, deletedCustomer)
	})
	e.GET("/tax", func(context echo.Context) error {
		var taxResult = master.GetTax()
		return context.JSON(http.StatusOK, taxResult)
	})
	e.POST("/tax/save", func(context echo.Context) error {
		t := new(master.Tax)
		// リクエストをTaxにバインドします。
		if err := context.Bind(t); err != nil {
			return err
		}

		savedTax, err := master.SaveTax(t)
		if err != nil {
			return err
		}

		return context.JSON(http.StatusOK, savedTax)
	})
	// CORS (Cross-Origin Resource Sharing)を許可します。
	e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
		AllowOrigins: []string{"*"},
	}))

	e.Run(standard.New(":1323"))
}