Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nodeinfo endpoint for federation purposes #16953

Merged
merged 15 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions modules/structs/nodeinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package structs

// NodeInfo contains standardized way of exposing metadata about a server running one of the distributed social networks
type NodeInfo struct {
Version string `json:"version"`
Software NodeInfoSoftware `json:"software"`
Protocols []string `json:"protocols"`
Services NodeInfoServices `json:"services"`
OpenRegistrations bool `json:"openRegistrations"`
Usage NodeInfoUsage `json:"usage"`
Metadata struct{} `json:"metadata"`
}

// NodeInfoSoftware contains Metadata about server software in use
type NodeInfoSoftware struct {
Name string `json:"name"`
Version string `json:"version"`
Repository string `json:"repository"`
Homepage string `json:"homepage"`
}

// NodeInfoServices contains the third party sites this server can connect to via their application API
type NodeInfoServices struct {
Inbound []string `json:"inbound"`
Outbound []string `json:"outbound"`
}

// NodeInfoUsage contains usage statistics for this server
type NodeInfoUsage struct {
Users NodeInfoUsageUsers `json:"users"`
LocalPosts int `json:"localPosts,omitempty"`
LocalComments int `json:"localComments,omitempty"`
}

// NodeInfoUsageUsers contains statistics about the users of this server
type NodeInfoUsageUsers struct {
Total int `json:"total,omitempty"`
ActiveHalfyear int `json:"activeHalfyear,omitempty"`
ActiveMonth int `json:"activeMonth,omitempty"`
}
1 change: 1 addition & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ func Routes() *web.Route {
})
}
m.Get("/version", misc.Version)
m.Get("/nodeinfo", misc.NodeInfo)
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
m.Get("/signing-key.gpg", misc.SigningKey)
m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
m.Post("/markdown/raw", misc.MarkdownRaw)
Expand Down
45 changes: 45 additions & 0 deletions routers/api/v1/misc/nodeinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package misc

import (
"net/http"

"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
)

// NodeInfo returns the NodeInfo for the Gitea instance to allow for federation
func NodeInfo(ctx *context.APIContext) {
// swagger:operation GET /nodeinfo miscellaneous getNodeInfo
// ---
// summary: Returns the nodeinfo of the Gitea application
// produces:
// - application/json
// responses:
// "200":
// "$ref": "#/responses/NodeInfo"

nodeInfo := &structs.NodeInfo{
Version: "2.1",
Software: structs.NodeInfoSoftware{
Name: "gitea",
zeripath marked this conversation as resolved.
Show resolved Hide resolved
Version: setting.AppVer,
Repository: "https://github.com/go-gitea/gitea.git",
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
Homepage: "https://gitea.io/",
},
Protocols: []string{"activitypub"},
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
Services: structs.NodeInfoServices{
Inbound: []string{},
Outbound: []string{},
},
OpenRegistrations: false,
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
Usage: structs.NodeInfoUsage{
Users: structs.NodeInfoUsageUsers{},
},
}
ctx.JSON(http.StatusOK, nodeInfo)
}
16 changes: 16 additions & 0 deletions routers/api/v1/swagger/nodeinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package swagger

import (
api "code.gitea.io/gitea/modules/structs"
)

// NodeInfo
// swagger:response NodeInfo
type swaggerResponseNodeInfo struct {
// in:body
Body api.NodeInfo `json:"body"`
}
33 changes: 33 additions & 0 deletions routers/web/nodeinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package web

import (
"fmt"
"net/http"

"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
)

type nodeInfoLinks struct {
Links []nodeInfoLink `json:"links"`
}

type nodeInfoLink struct {
Href string `json:"href"`
Rel string `json:"rel"`
}

// NodeInfoLinks returns links to the node info endpoint
func NodeInfoLinks(ctx *context.Context) {
nodeinfolinks := &nodeInfoLinks{
Links: []nodeInfoLink{{
fmt.Sprintf("%sapi/v1/nodeinfo", setting.AppURL),
"http://nodeinfo.diaspora.software/ns/schema/2.1",
}},
}
ctx.JSON(http.StatusOK, nodeinfolinks)
}
1 change: 1 addition & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ func RegisterRoutes(m *web.Route) {
// for health check
m.Get("/", Home)
m.Get("/.well-known/openid-configuration", user.OIDCWellKnown)
m.Get("/.well-known/nodeinfo", NodeInfoLinks)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This maybe optional because some instance don't want to be discovered.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lunny updated with option (and docs)

m.Group("/explore", func() {
m.Get("", func(ctx *context.Context) {
ctx.Redirect(setting.AppSubURL + "/explore/repos")
Expand Down
144 changes: 144 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,23 @@
}
}
},
"/nodeinfo": {
"get": {
"produces": [
"application/json"
],
"tags": [
"miscellaneous"
],
"summary": "Returns the nodeinfo of the Gitea application",
"operationId": "getNodeInfo",
"responses": {
"200": {
"$ref": "#/responses/NodeInfo"
}
}
}
},
"/notifications": {
"get": {
"consumes": [
Expand Down Expand Up @@ -15521,6 +15538,127 @@
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"NodeInfo": {
"description": "NodeInfo contains standardized way of exposing metadata about a server running one of the distributed social networks",
"type": "object",
"properties": {
"metadata": {
"type": "object",
"x-go-name": "Metadata"
},
"openRegistrations": {
"type": "boolean",
"x-go-name": "OpenRegistrations"
},
"protocols": {
"type": "array",
"items": {
"type": "string"
},
"x-go-name": "Protocols"
},
"services": {
"$ref": "#/definitions/NodeInfoServices"
},
"software": {
"$ref": "#/definitions/NodeInfoSoftware"
},
"usage": {
"$ref": "#/definitions/NodeInfoUsage"
},
"version": {
"type": "string",
"x-go-name": "Version"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"NodeInfoServices": {
"description": "NodeInfoServices contains the third party sites this server can connect to via their application API",
"type": "object",
"properties": {
"inbound": {
"type": "array",
"items": {
"type": "string"
},
"x-go-name": "Inbound"
},
"outbound": {
"type": "array",
"items": {
"type": "string"
},
"x-go-name": "Outbound"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"NodeInfoSoftware": {
"description": "NodeInfoSoftware contains Metadata about server software in use",
"type": "object",
"properties": {
"homepage": {
"type": "string",
"x-go-name": "Homepage"
},
"name": {
"type": "string",
"x-go-name": "Name"
},
"repository": {
"type": "string",
"x-go-name": "Repository"
},
"version": {
"type": "string",
"x-go-name": "Version"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"NodeInfoUsage": {
"description": "NodeInfoUsage contains usage statistics for this server",
"type": "object",
"properties": {
"localComments": {
"type": "integer",
"format": "int64",
"x-go-name": "LocalComments"
},
"localPosts": {
"type": "integer",
"format": "int64",
"x-go-name": "LocalPosts"
},
"users": {
"$ref": "#/definitions/NodeInfoUsageUsers"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"NodeInfoUsageUsers": {
"description": "NodeInfoUsageUsers contains statistics about the users of this server",
"type": "object",
"properties": {
"activeHalfyear": {
"type": "integer",
"format": "int64",
"x-go-name": "ActiveHalfyear"
},
"activeMonth": {
"type": "integer",
"format": "int64",
"x-go-name": "ActiveMonth"
},
"total": {
"type": "integer",
"format": "int64",
"x-go-name": "Total"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"Note": {
"description": "Note contains information related to a git note",
"type": "object",
Expand Down Expand Up @@ -17494,6 +17632,12 @@
}
}
},
"NodeInfo": {
"description": "NodeInfo",
"schema": {
"$ref": "#/definitions/NodeInfo"
}
},
"Note": {
"description": "Note",
"schema": {
Expand Down