Skip to content

Commit

Permalink
Add JSON endpoints
Browse files Browse the repository at this point in the history
This introduces some duplication but let's consider it foundation work for
now.
  • Loading branch information
rofirrim committed Aug 2, 2017
1 parent 6c8c2f1 commit ddcf8b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
49 changes: 30 additions & 19 deletions app/controllers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,12 @@ func (c App) ShowLog(id int) revel.Result {
revel.INFO.Println("Plog not found. Redirecting to menu", err)
return c.Menu(1)
} else {
if c.Request.ContentType == "application/json" {
c.Response.ContentType = "application/json"
return c.RenderJSON(plog)
}
c.ViewArgs["plog"] = plog
return c.FinishAndRender("single_log.html")
}
}


func (c App) Top20() revel.Result {
plogs, err := GetTop20Plogs()
if err != nil {
Expand All @@ -186,13 +183,6 @@ func (c App) Random() revel.Result {
return c.RenderError(err)
}

if c.Request.ContentType == "application/json" {
c.Response.ContentType = "application/json"
if len(plogs) == 0 {
return c.NotFound("Log not found", nil)
}
return c.RenderJSON(plogs[0])
}
processLogs(&plogs)
c.ViewArgs["plogs"] = plogs
c.ViewArgs["menuitem"] = "tapeta"
Expand All @@ -219,9 +209,6 @@ func (c App) Avatar(id int) revel.Result {
}

func (c App) Search(page int) revel.Result {
if c.Request.ContentType == "application/json" {
return c.searchJSON(c.Params)
}
if page <= 0 {
page = 1
}
Expand Down Expand Up @@ -250,19 +237,43 @@ func (c App) Search(page int) revel.Result {
return c.FinishAndRender("search.html")
}

func (c App) searchJSON(params *revel.Params) revel.Result {
// --------------
// JSON endpoints
// --------------

func (c App) ShowLogJSON(id int) revel.Result {
plog, err := GetPlog(id)

if err != nil {
return c.NotFound("Log not found")
} else {
return c.RenderJSON(plog)
}
}

func (c App) RandomJSON() revel.Result {
plogs, err := GetRandomPlogs()
if err != nil {
revel.ERROR.Println("Error when showing page", err)
return c.RenderError(err)
}

return c.RenderJSON(plogs[0])
}

func (c App) SearchJSON(page int) revel.Result {
c.Response.ContentType = "application/json"
keywords := strings.Split(params.Get("s"), " ")
keywords := strings.Split(c.Params.Get("s"), " ")
if len(keywords) == 0 {
return c.NotFound("Log not found", nil)
}
var numplogs = 1
plogs, err := SearchPlogs(keywords, 1, &numplogs)
var numplogs int
plogs, err := SearchPlogs(keywords, page, &numplogs)
if err != nil {
c.RenderError(err)
}
if len(plogs) == 0 {
return c.NotFound("Log not found", nil)
}
return c.RenderJSON(plogs[0])
return c.RenderJSON(plogs)
}
6 changes: 6 additions & 0 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ GET /.well-known/acme-challenge/*filepath Static.Serve("certbot/.well-kno
# Catch all, redirects to /menu if not a valid :id
GET /:id App.ShowLog

# JSON endpoints
GET /json/random App.RandomJSON
GET /json/search App.SearchJSON(1)
GET /json/search/:page App.SearchJSON
# JSON: Catch-all
GET /json/:id App.ShowLogJSON

0 comments on commit ddcf8b9

Please sign in to comment.