Пример #1
0
func main() {

	// serve ng-admin angular app
	fs := http.FileServer(http.Dir("static"))
	http.Handle("/admin/", http.StripPrefix("/admin/", fs))

	db, err := sql.Open("sqlite3", "./blog.db")

	defer db.Close()

	if err != nil {
		panic(err)
	}

	api := rest.NewApi()
	api.Use(rest.DefaultProdStack...)

	api.Use(&rest.CorsMiddleware{
		RejectNonCorsRequests: false,
		OriginValidator: func(origin string, request *rest.Request) bool {
			return true
		},
		AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
		AllowedHeaders: []string{
			"Accept", "Content-Type", "X-Total-Count", "Origin"},
		AccessControlAllowCredentials: true,
		AccessControlMaxAge:           3600,
	})

	// initialize Entity REST API
	entityManager := eram.NewEntityDbManager(db)
	entityRestApi := era.NewEntityRestAPI(entityManager)

	router, err := rest.MakeRouter(
		rest.Get("/api/:entity", entityRestApi.GetAllEntities),
		rest.Post("/api/:entity", entityRestApi.PostEntity),
		rest.Get("/api/:entity/:id", entityRestApi.GetEntity),
		rest.Put("/api/:entity/:id", entityRestApi.PutEntity),
		rest.Delete("/api/:entity/:id", entityRestApi.DeleteEntity),
	)

	if err != nil {
		log.Fatal(err)
	}

	api.SetApp(router)

	http.Handle("/api/", api.MakeHandler())
	log.Fatal(http.ListenAndServe(":8080", nil))
}
func init() {

	db, err := sql.Open("sqlite3", ":memory:")

	if err != nil {
		log.Fatal("An error '%s' was not expected when opening a stub database connection", err)
	} else {

		dat, err := ioutil.ReadFile("./../test.sql")
		if err != nil {
			log.Fatal("An error '%s' was not expected when opening sql file", err)
		} else {
			db.Exec(string(dat))
		}
	}

	api := rest.NewApi()
	api.Use(rest.DefaultDevStack...)

	entityManager := eram.NewEntityDbManager(db)
	entityRestApi := NewEntityRestAPI(entityManager)

	router, err := rest.MakeRouter(
		rest.Get("/api/:entity", entityRestApi.GetAllEntities),
		rest.Post("/api/:entity", entityRestApi.PostEntity),
		rest.Get("/api/:entity/:id", entityRestApi.GetEntity),
		rest.Put("/api/:entity/:id", entityRestApi.PutEntity),
		rest.Delete("/api/:entity/:id", entityRestApi.DeleteEntity),
	)

	if err != nil {
		log.Fatal("An error '%s' was not expected when creating the router", err)
	}

	api.SetApp(router)
	handler = api.MakeHandler()

	server = httptest.NewServer(handler)
}