Added API examples
This commit is contained in:
parent
f3b9eff1f4
commit
c5de0c6d6f
3
main.go
3
main.go
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/animenotifier/notify.moe/auth"
|
"github.com/animenotifier/notify.moe/auth"
|
||||||
"github.com/animenotifier/notify.moe/middleware"
|
"github.com/animenotifier/notify.moe/middleware"
|
||||||
"github.com/animenotifier/notify.moe/pages"
|
"github.com/animenotifier/notify.moe/pages"
|
||||||
|
"github.com/animenotifier/notify.moe/utils/routetests"
|
||||||
)
|
)
|
||||||
|
|
||||||
var app = aero.New()
|
var app = aero.New()
|
||||||
@ -73,7 +74,7 @@ func configure(app *aero.Application) *aero.Application {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Specify test routes
|
// Specify test routes
|
||||||
for route, examples := range routeTests {
|
for route, examples := range routetests.All() {
|
||||||
app.Test(route, examples)
|
app.Test(route, examples)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
|
"github.com/animenotifier/notify.moe/utils/routetests"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestRouteStatusCodes tests the status code of every route registered in routeTests.
|
// TestRouteStatusCodes tests the status code of every route registered in routeTests.
|
||||||
@ -14,7 +15,7 @@ func TestRouteStatusCodes(t *testing.T) {
|
|||||||
app := configure(aero.New())
|
app := configure(aero.New())
|
||||||
|
|
||||||
// Iterate through every route
|
// Iterate through every route
|
||||||
for _, examples := range routeTests {
|
for _, examples := range routetests.All() {
|
||||||
// Iterate through every example specified for that route
|
// Iterate through every example specified for that route
|
||||||
for _, example := range examples {
|
for _, example := range examples {
|
||||||
// Create a new HTTP request
|
// Create a new HTTP request
|
||||||
|
@ -2,12 +2,14 @@ package apidocs
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
"github.com/animenotifier/arn"
|
"github.com/animenotifier/arn"
|
||||||
"github.com/animenotifier/notify.moe/components"
|
"github.com/animenotifier/notify.moe/components"
|
||||||
"github.com/animenotifier/notify.moe/utils"
|
"github.com/animenotifier/notify.moe/utils"
|
||||||
|
"github.com/animenotifier/notify.moe/utils/routetests"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ByType renders the api docs page for the given type.
|
// ByType renders the api docs page for the given type.
|
||||||
@ -16,39 +18,44 @@ func ByType(typeName string) func(*aero.Context) string {
|
|||||||
t := arn.API.Type(typeName)
|
t := arn.API.Type(typeName)
|
||||||
fields := []*utils.APIField{}
|
fields := []*utils.APIField{}
|
||||||
|
|
||||||
for i := 0; i < t.NumField(); i++ {
|
if t.Kind() == reflect.Struct {
|
||||||
field := t.Field(i)
|
for i := 0; i < t.NumField(); i++ {
|
||||||
|
field := t.Field(i)
|
||||||
|
|
||||||
if field.Anonymous || !unicode.IsUpper(rune(field.Name[0])) {
|
if field.Anonymous || !unicode.IsUpper(rune(field.Name[0])) {
|
||||||
continue
|
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() + "[]"
|
typeName := ""
|
||||||
|
|
||||||
default:
|
switch field.Type.Kind() {
|
||||||
typeName = field.Type.Name()
|
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,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fields = append(fields, &utils.APIField{
|
|
||||||
Name: field.Name,
|
|
||||||
JSON: field.Tag.Get("json"),
|
|
||||||
Type: typeName,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx.HTML(components.APIDocs(t, fields))
|
route := "/api/" + strings.ToLower(typeName) + "/:id"
|
||||||
|
examples := routetests.All()[route]
|
||||||
|
|
||||||
|
return ctx.HTML(components.APIDocs(t, examples, fields))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,32 @@
|
|||||||
component APIDocs(t reflect.Type, fields []*utils.APIField)
|
component APIDocs(t reflect.Type, examples []string, fields []*utils.APIField)
|
||||||
h1= "API: " + t.Name()
|
.api-docs-page
|
||||||
|
h1= "API: " + t.Name()
|
||||||
|
|
||||||
table
|
h2 Examples
|
||||||
thead
|
|
||||||
tr
|
if len(examples) == 0
|
||||||
th Field name
|
p.no-data No examples have been added to this type yet.
|
||||||
th JavaScript notation
|
else
|
||||||
th Type
|
.buttons
|
||||||
tbody
|
each example in examples
|
||||||
each field in fields
|
a.button(href="https://notify.moe" + example, target="_blank")
|
||||||
|
Icon("external-link")
|
||||||
|
span= example
|
||||||
|
|
||||||
|
h2 Fields
|
||||||
|
|
||||||
|
table
|
||||||
|
thead
|
||||||
tr
|
tr
|
||||||
td= field.Name
|
th Field
|
||||||
td= field.JSON
|
th Type
|
||||||
td= field.Type
|
tbody
|
||||||
|
each field in fields
|
||||||
|
tr.api-field
|
||||||
|
td.api-field-json(title=field.Name + " (Go)")= field.JSON
|
||||||
|
td.api-field-type= field.Type
|
||||||
|
|
||||||
.corner-buttons
|
.corner-buttons
|
||||||
a.button(href="/api")
|
a.button(href="/api")
|
||||||
Icon("code")
|
Icon("code")
|
||||||
span Overview
|
span Overview
|
19
pages/apiview/apidocs/apidocs.scarlet
Normal file
19
pages/apiview/apidocs/apidocs.scarlet
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
.api-docs-page
|
||||||
|
h1,
|
||||||
|
h2
|
||||||
|
text-align left
|
||||||
|
|
||||||
|
table
|
||||||
|
margin 0
|
||||||
|
|
||||||
|
.buttons
|
||||||
|
justify-content flex-start
|
||||||
|
|
||||||
|
.api-field
|
||||||
|
//
|
||||||
|
|
||||||
|
.api-field-json
|
||||||
|
//
|
||||||
|
|
||||||
|
.api-field-type
|
||||||
|
opacity 0.5
|
@ -1,9 +1,15 @@
|
|||||||
h1, h2
|
h1
|
||||||
font-size 2em
|
font-size 2em
|
||||||
font-weight bold
|
font-weight bold
|
||||||
text-align center
|
text-align center
|
||||||
line-height 1.3em
|
line-height 1.3em
|
||||||
|
|
||||||
|
h2
|
||||||
|
font-size 1.6em
|
||||||
|
line-height 1.3em
|
||||||
|
font-weight bold
|
||||||
|
text-align center
|
||||||
|
|
||||||
h3
|
h3
|
||||||
font-size 1.5em
|
font-size 1.5em
|
||||||
line-height 1.6em
|
line-height 1.6em
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package routetests
|
||||||
|
|
||||||
var routeTests = map[string][]string{
|
var routeTests = map[string][]string{
|
||||||
// User
|
// User
|
||||||
@ -364,3 +364,8 @@ var routeTests = map[string][]string{
|
|||||||
"/inventory": nil,
|
"/inventory": nil,
|
||||||
"/extension/embed": nil,
|
"/extension/embed": nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// All returns which specific routes to test for a given generic route.
|
||||||
|
func All() map[string][]string {
|
||||||
|
return routeTests
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user