Added listing of API fields

This commit is contained in:
Eduard Urbach 2018-03-28 00:26:06 +02:00
parent 199512dbc9
commit f3b9eff1f4
4 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,54 @@
package apidocs
import (
"reflect"
"unicode"
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
// ByType renders the api docs page for the given type.
func ByType(typeName string) func(*aero.Context) string {
return func(ctx *aero.Context) string {
t := arn.API.Type(typeName)
fields := []*utils.APIField{}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Anonymous || !unicode.IsUpper(rune(field.Name[0])) {
continue
}
typeName := ""
switch field.Type.Kind() {
case reflect.Ptr:
typeName = field.Type.Elem().Name()
case reflect.Slice:
sliceElementType := field.Type.Elem()
if sliceElementType.Kind() == reflect.Ptr {
sliceElementType = sliceElementType.Elem()
}
typeName = sliceElementType.Name() + "[]"
default:
typeName = field.Type.Name()
}
fields = append(fields, &utils.APIField{
Name: field.Name,
JSON: field.Tag.Get("json"),
Type: typeName,
})
}
return ctx.HTML(components.APIDocs(t, fields))
}
}

View File

@ -0,0 +1,21 @@
component APIDocs(t reflect.Type, fields []*utils.APIField)
h1= "API: " + t.Name()
table
thead
tr
th Field name
th JavaScript notation
th Type
tbody
each field in fields
tr
td= field.Name
td= field.JSON
td= field.Type
.corner-buttons
a.button(href="/api")
Icon("code")
span Overview

View File

@ -1,6 +1,8 @@
package pages package pages
import ( import (
"strings"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/aerogo/layout" "github.com/aerogo/layout"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
@ -12,6 +14,7 @@ import (
"github.com/animenotifier/notify.moe/pages/animelist" "github.com/animenotifier/notify.moe/pages/animelist"
"github.com/animenotifier/notify.moe/pages/animelistitem" "github.com/animenotifier/notify.moe/pages/animelistitem"
"github.com/animenotifier/notify.moe/pages/apiview" "github.com/animenotifier/notify.moe/pages/apiview"
"github.com/animenotifier/notify.moe/pages/apiview/apidocs"
"github.com/animenotifier/notify.moe/pages/calendar" "github.com/animenotifier/notify.moe/pages/calendar"
"github.com/animenotifier/notify.moe/pages/character" "github.com/animenotifier/notify.moe/pages/character"
"github.com/animenotifier/notify.moe/pages/charge" "github.com/animenotifier/notify.moe/pages/charge"
@ -85,6 +88,11 @@ func Configure(app *aero.Application) {
l.Page("/halloffame", halloffame.Get) l.Page("/halloffame", halloffame.Get)
l.Page("/login", login.Get) l.Page("/login", login.Get)
l.Page("/api", apiview.Get) l.Page("/api", apiview.Get)
for name := range arn.DB.Types() {
l.Page("/api/"+strings.ToLower(name), apidocs.ByType(name))
}
// l.Ajax("/dashboard", dashboard.Get) // l.Ajax("/dashboard", dashboard.Get)
// l.Ajax("/best/anime", best.Get) // l.Ajax("/best/anime", best.Get)
// l.Ajax("/artworks", artworks.Get) // l.Ajax("/artworks", artworks.Get)

8
utils/APIField.go Normal file
View File

@ -0,0 +1,8 @@
package utils
// APIField describes a field in the API.
type APIField struct {
Name string
JSON string
Type string
}