summaryrefslogtreecommitdiff
path: root/srv/src/api/index.go
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2022-05-20 11:17:31 -0600
committerBrian Picciano <mediocregopher@gmail.com>2022-05-20 11:17:31 -0600
commit09acb111a2b22f5794541fac175b024dd0f9100e (patch)
tree11d4578a42ad4aea968b42a2689f64c799f9176e /srv/src/api/index.go
parentf69ed83de73bbfc4b7af0931de6ced8cf12dea61 (diff)
Rename api package to http
Diffstat (limited to 'srv/src/api/index.go')
-rw-r--r--srv/src/api/index.go60
1 files changed, 0 insertions, 60 deletions
diff --git a/srv/src/api/index.go b/srv/src/api/index.go
deleted file mode 100644
index 5fb5a4f..0000000
--- a/srv/src/api/index.go
+++ /dev/null
@@ -1,60 +0,0 @@
-package api
-
-import (
- "fmt"
- "net/http"
- "path/filepath"
- "strings"
-
- "github.com/mediocregopher/blog.mediocregopher.com/srv/api/apiutil"
- "github.com/mediocregopher/blog.mediocregopher.com/srv/post"
-)
-
-func (a *api) renderIndexHandler() http.Handler {
-
- tpl := a.mustParseBasedTpl("index.html")
- const pageCount = 10
-
- return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
-
- if path := r.URL.Path; !strings.HasSuffix(path, "/") && filepath.Base(path) != "index.html" {
- http.Error(rw, "Page not found", 404)
- return
- }
-
- page, err := apiutil.StrToInt(r.FormValue("p"), 0)
- if err != nil {
- apiutil.BadRequest(
- rw, r, fmt.Errorf("invalid page number: %w", err),
- )
- return
- }
-
- posts, hasMore, err := a.params.PostStore.WithOrderDesc().Get(page, pageCount)
- if err != nil {
- apiutil.InternalServerError(
- rw, r, fmt.Errorf("fetching page %d of posts: %w", page, err),
- )
- return
- }
-
- tplPayload := struct {
- Posts []post.StoredPost
- PrevPage, NextPage int
- }{
- Posts: posts,
- PrevPage: -1,
- NextPage: -1,
- }
-
- if page > 0 {
- tplPayload.PrevPage = page - 1
- }
-
- if hasMore {
- tplPayload.NextPage = page + 1
- }
-
- executeTemplate(rw, r, tpl, tplPayload)
- })
-}