Documentation ¶
Index ¶
- Variables
- func GetPathInt(ctx *gin.Context, name string) (int, error)
- func SetupRoutes(node *node.OracleNode, workerManager *workers.WorkHandlerManager, ...) *gin.Engine
- type API
- func (api *API) CfLlmChat() gin.HandlerFunc
- func (api *API) ChatPageHandler() gin.HandlerFunc
- func (api *API) CompleteAuth() gin.HandlerFunc
- func (api *API) CreateNewTopicHandler() gin.HandlerFunc
- func (api *API) GetBlockByHash() gin.HandlerFunc
- func (api *API) GetBlocks() gin.HandlerFunc
- func (api *API) GetChannelMessagesHandler() gin.HandlerFunc
- func (api *API) GetFromDHT() gin.HandlerFunc
- func (api *API) GetNodeApiKey() gin.HandlerFunc
- func (api *API) GetNodeDataHandler() gin.HandlerFunc
- func (api *API) GetNodeHandler() gin.HandlerFunc
- func (api *API) GetPeerAddresses() gin.HandlerFunc
- func (api *API) GetPeersHandler() gin.HandlerFunc
- func (api *API) GetPublicKeysHandler() gin.HandlerFunc
- func (api *API) LocalLlmChat() gin.HandlerFunc
- func (api *API) NodeStatusPageHandler() gin.HandlerFunc
- func (api *API) PostNodeStatusHandler() gin.HandlerFunc
- func (api *API) PostToDHT() gin.HandlerFunc
- func (api *API) PostToTopicHandler() gin.HandlerFunc
- func (api *API) PublishPublicKeyHandler() gin.HandlerFunc
- func (api *API) SearchAllGuilds() gin.HandlerFunc
- func (api *API) SearchChannelMessages() gin.HandlerFunc
- func (api *API) SearchDiscordProfile() gin.HandlerFunc
- func (api *API) SearchGuildChannels() gin.HandlerFunc
- func (api *API) SearchTweetsProfile() gin.HandlerFunc
- func (api *API) SearchTweetsRecent() gin.HandlerFunc
- func (api *API) SearchTwitterFollowers() gin.HandlerFunc
- func (api *API) SearchUserGuilds() gin.HandlerFunc
- func (api *API) StartAuth() gin.HandlerFunc
- func (api *API) Test() gin.HandlerFunc
- func (api *API) WebData() gin.HandlerFunc
- type APIConfig
- type LLMChat
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = APIConfig{ WorkerResponseTimeout: 60 * time.Second, }
Functions ¶
func GetPathInt ¶
GetPathInt converts the path parameter with name to an int. It returns the int value and nil error if the path parameter is present and a valid integer. It returns 0 and a formatted error if the path parameter is missing or not a valid integer.
func SetupRoutes ¶
func SetupRoutes(node *node.OracleNode, workerManager *workers.WorkHandlerManager, pubkeySubscriptionHandler *pubsub.PublicKeySubscriptionHandler) *gin.Engine
SetupRoutes configures the router with all API routes. It takes an OracleNode instance and returns a configured gin.Engine. Routes are added for peers, ads, subscriptions, node data, public keys, topics, the DHT, node status, and serving HTML pages. Middleware is added for CORS and templates.
Types ¶
type API ¶
type API struct { Node *node.OracleNode EventTracker *event.EventTracker WorkManager *workers.WorkHandlerManager PubKeySubscriptionHandler *pubsub.PublicKeySubscriptionHandler }
func NewAPI ¶
func NewAPI(node *node.OracleNode, workManager *workers.WorkHandlerManager, pubkeySubscriptionHandler *pubsub.PublicKeySubscriptionHandler) *API
NewAPI creates a new API instance with the given OracleNode.
func (*API) CfLlmChat ¶
func (api *API) CfLlmChat() gin.HandlerFunc
CfLlmChat handles the Cloudflare LLM chat requests. It reads the request body, appends a system message, and forwards the request to the configured LLM endpoint. The response from the LLM endpoint is then returned to the client.
{ "model": "@cf/meta/llama-3-8b-instruct", "messages": [ { "role": "user", "content": "why is the sky blue?" } ] }
Models
@cf/qwen/qwen1.5-0.5b-chat @cf/meta/llama-2-7b-chat-fp16 @cf/meta/llama-3-8b-instruct @cf/mistral/mistral-7b-instruct @cf/mistral/mistral-7b-instruct-v0.1 @hf/google/gemma-7b-it @hf/nousresearch/hermes-2-pro-mistral-7b @hf/thebloke/llama-2-13b-chat-awq @hf/thebloke/neural-chat-7b-v3-1-awq @cf/openchat/openchat-3.5-0106 @cf/microsoft/phi-2
@return gin.HandlerFunc - the handler function for the LLM chat requests.
func (*API) ChatPageHandler ¶
func (api *API) ChatPageHandler() gin.HandlerFunc
ChatPageHandler returns a gin.HandlerFunc that renders the chat page. It responds to HTTP GET requests by serving the "chat.html" template. The handler sets the HTTP status to 200 (OK) and provides an empty gin.H map to the template, which can be used to pass data if needed in the future.
func (*API) CompleteAuth ¶ added in v0.5.1
func (api *API) CompleteAuth() gin.HandlerFunc
CompleteAuth completes the authentication process with Telegram.
func (*API) CreateNewTopicHandler ¶
func (api *API) CreateNewTopicHandler() gin.HandlerFunc
CreateNewTopicHandler creates a new topic with a given name and subscribes a handler to it.
func (*API) GetBlockByHash ¶ added in v0.5.1
func (api *API) GetBlockByHash() gin.HandlerFunc
GetBlockByHash returns a gin.HandlerFunc that handles requests to retrieve a specific block from the blockchain by its hash.
This function: 1. Extracts the block hash from the request parameters. 2. Decodes the hexadecimal block hash. 3. Retrieves the block from the blockchain using the decoded hash. 4. Unmarshals the block data and formats it for the response. 5. Returns the formatted block data as a JSON response.
If any errors occur during this process (e.g., invalid hash, block not found), appropriate error responses are sent back to the client.
func (*API) GetBlocks ¶ added in v0.5.1
func (api *API) GetBlocks() gin.HandlerFunc
GetBlocks returns a gin.HandlerFunc that handles requests to retrieve all blocks from the blockchain.
This function: 1. Checks if the node is a validator. 2. Retrieves all blocks from the blockchain. 3. Formats each block's data into a more readable structure. 4. Encodes the input data of each block in base64. 5. Returns the formatted blocks as a JSON response.
The function is only accessible to validator nodes and will return an error for non-validator nodes.
func (*API) GetChannelMessagesHandler ¶ added in v0.5.1
func (api *API) GetChannelMessagesHandler() gin.HandlerFunc
func (*API) GetFromDHT ¶
func (api *API) GetFromDHT() gin.HandlerFunc
GetFromDHT handles GET requests to retrieve data from the DHT given a key. It looks up the key in the DHT, unmarshals the value into a SharedData struct, and returns the data in the response.
func (*API) GetNodeApiKey ¶
func (api *API) GetNodeApiKey() gin.HandlerFunc
GetNodeApiKey returns a gin.HandlerFunc that generates and returns a JWT token for the node. The JWT token is signed using the node's host ID as the secret key. On success, it returns the generated JWT token in a JSON response. On failure, it returns an appropriate error message and HTTP status code.
func (*API) GetNodeDataHandler ¶
func (api *API) GetNodeDataHandler() gin.HandlerFunc
GetNodeDataHandler handles GET requests to retrieve paginated node data from the node tracker. It parses the page number and page size from the request path, retrieves all node data from the node tracker, calculates pagination details like total pages based on page size, and returns a page of node data in the response.
func (*API) GetNodeHandler ¶
func (api *API) GetNodeHandler() gin.HandlerFunc
GetNodeHandler handles GET requests to retrieve node data for a specific peer ID. It extracts the peer ID from the request URL parameters, retrieves the node data from the node tracker, calculates additional uptime info, and returns the node data in the response.
func (*API) GetPeerAddresses ¶
func (api *API) GetPeerAddresses() gin.HandlerFunc
GetPeerAddresses handles GET requests to retrieve the list of peer addresses from the node's libp2p host network. It gets the list of connected peers, finds the multiaddrs for connections to each peer, and returns the peer IDs mapped to their addresses.
func (*API) GetPeersHandler ¶
func (api *API) GetPeersHandler() gin.HandlerFunc
GetPeersHandler handles GET requests to retrieve the list of peer IDs from the DHT routing table. It retrieves the routing table from the node's DHT instance, extracts the peer IDs, and returns them in the response.
func (*API) GetPublicKeysHandler ¶
func (api *API) GetPublicKeysHandler() gin.HandlerFunc
GetPublicKeysHandler handles the endpoint to retrieve all known public keys. It gets the public key subscription handler from the PubSub manager, extracts the public keys, and returns them in the response.
func (*API) LocalLlmChat ¶
func (api *API) LocalLlmChat() gin.HandlerFunc
LocalLlmChat handles requests for chatting with AI models hosted by ollama. It expects a JSON request body with a structure formatted for the model. For example for Ollama:
{ "model": "llama3", "messages": [ { "role": "user", "content": "why is the sky blue?" } ], "stream": false }
This function acts as a proxy, forwarding the request to hosted models and returning the proprietary structured response. This is intended to be compatible with code that is looking to leverage a common payload for LLMs that is based on the model name/type So if it is an Ollama request it is the responsibility of the caller to properly format their payload to conform to the required structure similar to above.
See: https://platform.openai.com/docs/api-reference/authentication https://docs.anthropic.com/claude/reference/complete_post https://github.com/ollama/ollama/blob/main/docs/api.md note: Ollama recently added support for the OpenAI structure which can simplify integrating it.
func (*API) NodeStatusPageHandler ¶
func (api *API) NodeStatusPageHandler() gin.HandlerFunc
NodeStatusPageHandler handles HTTP requests to show the node status page. It retrieves the node data from the node tracker, formats it, and renders an HTML page displaying the node's status and uptime info.
func (*API) PostNodeStatusHandler ¶
func (api *API) PostNodeStatusHandler() gin.HandlerFunc
PostNodeStatusHandler allows posting a message to the Topic
func (*API) PostToDHT ¶
func (api *API) PostToDHT() gin.HandlerFunc
PostToDHT handles POST requests to write data to the DHT. It expects a JSON body with "key" and "value" fields. The "key" is used to store the data in the DHT under /db/key. The "value" is marshalled to JSON and written to the DHT. Returns 200 OK on success with the key in the response. Returns 400 Bad Request on invalid request or JSON errors.
func (*API) PostToTopicHandler ¶
func (api *API) PostToTopicHandler() gin.HandlerFunc
PostToTopicHandler allows posting a message to a specified topic.
func (*API) PublishPublicKeyHandler ¶
func (api *API) PublishPublicKeyHandler() gin.HandlerFunc
PublishPublicKeyHandler handles the /publickey endpoint. It retrieves the node's public key, signs the public key with the private key, creates a public key message with the key info, signs it, and publishes it to the public key topic. This allows other nodes to obtain this node's public key.
func (*API) SearchAllGuilds ¶
func (api *API) SearchAllGuilds() gin.HandlerFunc
SearchAllGuilds returns a gin.HandlerFunc that queries each node for the Discord guilds they are part of.
func (*API) SearchChannelMessages ¶
func (api *API) SearchChannelMessages() gin.HandlerFunc
SearchChannelMessages returns a gin.HandlerFunc that processes a request to search for messages in a Discord channel.
func (*API) SearchDiscordProfile ¶
func (api *API) SearchDiscordProfile() gin.HandlerFunc
SearchDiscordProfile returns a gin.HandlerFunc that processes a request to search for a Discord user profile. It expects a URL parameter "userID" representing the Discord user ID to search for. The handler validates the userID, ensuring it is provided. If the request is valid, it attempts to fetch the user's profile. On success, it returns the fetched profile information in a JSON response. On failure, it returns an appropriate error message and HTTP status code.
func (*API) SearchGuildChannels ¶
func (api *API) SearchGuildChannels() gin.HandlerFunc
SearchGuildChannels returns a gin.HandlerFunc that processes a request to search for channels in a Discord guild.
func (*API) SearchTweetsProfile ¶
func (api *API) SearchTweetsProfile() gin.HandlerFunc
SearchTweetsProfile returns a gin.HandlerFunc that processes a request to search for tweets from a specific user profile. It expects a URL parameter "username" representing the Twitter username to search for. The handler validates the username, ensuring it is provided. If the request is valid, it attempts to scrape the user's profile and tweets. On success, it returns the scraped profile information in a JSON response. On failure, it returns an appropriate error message and HTTP status code.
func (*API) SearchTweetsRecent ¶
func (api *API) SearchTweetsRecent() gin.HandlerFunc
SearchTweetsRecent returns a gin.HandlerFunc that processes a request to search for tweets based on a query and count. It expects a JSON body with fields "query" (string) and "count" (int), representing the search query and the number of tweets to return, respectively. The handler validates the request body, ensuring the query is not empty and the count is positive. If the request is valid, it attempts to scrape tweets using the specified query and count. On success, it returns the scraped tweets in a JSON response. On failure, it returns an appropriate error message and HTTP status code.
func (*API) SearchTwitterFollowers ¶
func (api *API) SearchTwitterFollowers() gin.HandlerFunc
SearchTwitterFollowers returns a gin.HandlerFunc that retrieves the followers of a given Twitter user.
Dev Notes: - This function uses URL parameters to get the username. - The default count is set to 20 if not provided.
func (*API) SearchUserGuilds ¶
func (api *API) SearchUserGuilds() gin.HandlerFunc
SearchUserGuilds returns a gin.HandlerFunc that processes a request to search for guilds associated with a Discord user.
func (*API) StartAuth ¶ added in v0.5.1
func (api *API) StartAuth() gin.HandlerFunc
StartAuth starts the authentication process with Telegram.
func (*API) Test ¶
func (api *API) Test() gin.HandlerFunc
Test is a temporary function that handles test requests. TODO: Remove this function once testing is complete.
func (*API) WebData ¶
func (api *API) WebData() gin.HandlerFunc
WebData returns a gin.HandlerFunc that processes web scraping requests. It expects a JSON body with fields "url" (string) and "depth" (int), representing the URL to scrape and the depth of the scrape, respectively. The handler validates the request body, ensuring the URL is not empty and the depth is positive. If the node has not staked, it returns an error indicating the node cannot participate. On a valid request, it attempts to scrape web data using the specified URL and depth. On success, it returns the scraped data in a sanitized JSON response. On failure, it returns an appropriate error message and HTTP status code.
type APIConfig ¶ added in v0.6.0
APIConfig contains configuration settings for the API
func LoadConfig ¶ added in v0.6.0
LoadConfig loads the API configuration This can be expanded later to load from environment variables or a file