Пример #1
0
// Entry point for running the application.
// It defines all routes and middleware used
// and starts the underlying server.
func Start() {
	// Set up our Martini instance
	m := martini.Classic()

	// Middleware
	// m.Use(gzip.All())
	m.Use(render.Renderer())

	// Routes
	m.Group("/admin", func(r martini.Router) {
		a := handlers.Admin{}

		r.Get("", a.Index)
		r.Get("/posts", a.Posts.Index)
		r.Get("/posts/new", a.Posts.New)
		r.Get("/posts/:id/edit", a.Posts.Edit)

	}, render.Renderer(render.Options{
		Layout: "admin/layout",
	}))

	m.Group("/api", func(r martini.Router) {
		a := handlers.Api{}

		r.Get("", a.Index)
		r.Get("/posts", a.Posts.Index)
		r.Post("/posts", a.Posts.Create)
		r.Get("/posts/:id", a.Posts.Show)
		r.Get("/render/markdown", a.Render.Markdown)
	})

	// Serve from static if possible
	m.Use(martini.Static(util.Config().App.SiteDir))
	m.Run()
}
Пример #2
0
// Create allows for the creation of new posts. It returns a 204
// response on creation or a 500 response on error.
func (ap ApiPosts) Create(r render.Render, req *http.Request) {
	var (
		post models.Post
		err  error
		t    = "urlencoded"
		p    interface{}
	)
	root := util.Config().App.PostsDir
	contentType := req.Header.Get("content-type")

	if strings.Contains(contentType, "application/json") {
		t = "json"
	}

	switch t {
	case "json":
		dec := json.NewDecoder(req.Body)
		err = dec.Decode(&post)
		p = post
	default:
		err = req.ParseForm()
		p = req.Form
	}

	if err != nil {
		r.Data(500, []byte(err.Error()))
		return
	}

	if err = models.SavePost(root, p); err == nil {
		r.Data(204, []byte("Created"))
	} else {
		r.Data(500, []byte(err.Error()))
	}
}
Пример #3
0
// Show returns a single post.
func (ap ApiPosts) Show(params martini.Params, r render.Render) {
	var post models.Post

	root := util.Config().App.PostsDir
	path := models.ParsePostId(params["id"])
	file := root + string(os.PathSeparator) + path
	found := false

	for _, p := range models.GetAllPosts(root) {
		if p.Directory+string(os.PathSeparator)+p.Filename == file {
			post = p
			found = true
		}
	}

	if found {
		r.JSON(200, post)
	} else {
		r.Error(404)
	}
}
Пример #4
0
// Index returns all available posts.
func (ap ApiPosts) Index(r render.Render) {
	root := util.Config().App.PostsDir
	posts := models.GetAllPosts(root)

	r.JSON(200, posts)
}