apigraph

package
v14.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: BSD-3-Clause, BSD-3-Clause Imports: 7 Imported by: 0

README

API Graph

The apigraph controller provides a visual representation of your application's routes using D3.js. It generates an interactive HTML page that displays the routes, methods, and handlers in a tree layout. This visualization helps developers understand the structure and relationships of the routes in their application.

Features

  • Visual representation of routes using D3.js
  • Interactive graph with hover and click functionality
  • Customizable themes (light and dark)
  • Displays route paths, methods, and handlers
  • Opens handler files in Visual Studio Code using the vscode://file/ URL scheme

Installation

To use the apigraph controller, you need to import it in your Iris application:

import "github.com/kataras/iris/v14/controller/apigraph"

Usage

To use the apigraph controller, you need to create an implementation of router.Controller and register it with your Iris application. You can customize the theme using the Options struct.

package main

import (
	"github.com/kataras/iris/v14"
	"github.com/kataras/iris/v14/controller/apigraph"
)

func main() {
	app := iris.New()
	app.Router("/apigraph", apigraph.New(apigraph.Options{Theme: "dark"}))

	// Example routes with the same path but different methods:
	app.Get("/", func(ctx iris.Context) { ctx.WriteString("GET /") })
	app.Get("/users", func(ctx iris.Context) { ctx.WriteString("GET /users") })
	app.Get("/users/list", func(ctx iris.Context) { ctx.WriteString("GET /users/list") })
	app.Post("/users", func(ctx iris.Context) { ctx.WriteString("POST /users") })
	app.Get("/users/{id:int}", func(ctx iris.Context) { ctx.WriteString("GET /users/{id:int}") })
	app.Put("/users/{id:int}", func(ctx iris.Context) { ctx.WriteString("PUT /users/{id:int}") })
	app.Get("/users/{id:int}/posts", func(ctx iris.Context) { ctx.WriteString("GET /users/{id:int}/posts") })

	app.Listen(":8080")
}

Options

The Options struct configures the Controller. It currently has one field:

  • Theme: The theme to be used for the graph. Available themes are "light" and "dark".

Themes

The apigraph controller supports two themes: light and dark. You can register additional themes using the RegisterTheme function.

Light Theme

The light theme is the default theme. It uses a white background with black text.

Dark Theme

The dark theme uses a dark background with light text.

Theme Interface

The Theme interface represents a theme for the graph.

type Theme interface {
	GetName() string
	GetStyle() string
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(options Options) router.Controller

New creates a new API Graph router.Controller with the given options. It registers a handler that renders a graph of the application's routes. It generates an HTML page with a D3.js visualization of the routes, showing the paths, methods, and handlers. The graph is interactive, allowing users to directly open the handler files in Visual Studio Code. Users can also to hover over links and nodes to see more details.

Parameters:

  • options: Options for the graph, such as the theme. If no options are provided, the default theme is used. The theme determines the background color, text color, and link color of the graph. The default theme is light, but a dark theme is also available. To use the dark theme, pass "dark" as the theme.

The generated HTML includes a D3.js script that creates a horizontal tree layout of the routes. Each node represents a route, and each link represents the relationship between parent and child routes. The nodes display the route paths and methods, and clicking on a node opens the corresponding handler file in Visual Studio Code using the vscode://file/ URL scheme.

Usage:

func main() {
    app := iris.New()
    app.Router("/apigraph", apigraph.New(apigraph.Options{Theme: "dark"}))

    // Example routes with the same path but different methods:
    app.Get("/", func(ctx iris.Context) { ctx.WriteString("GET /") })
    app.Get("/users", func(ctx iris.Context) { ctx.WriteString("GET /users") })
    app.Get("/users/list", func(ctx iris.Context) { ctx.WriteString("GET /users/list") })
    app.Post("/users", func(ctx iris.Context) { ctx.WriteString("POST /users") })
    app.Get("/users/{id:int}", func(ctx iris.Context) { ctx.WriteString("GET /users/{id:int}") })
    app.Put("/users/{id:int}", func(ctx iris.Context) { ctx.WriteString("PUT /users/{id:int}") })
    app.Get("/users/{id:int}/posts", func(ctx iris.Context) { ctx.WriteString("GET /users/{id:int}/posts") })

    app.Listen(":8080")
}

func RegisterTheme

func RegisterTheme(theme Theme)

RegisterTheme registers a theme . If a theme with the same name already exists, it is overwritten.

Types

type DefaultTheme

type DefaultTheme struct {
	// Name is the name of the theme.
	Name string
	// BackgroundColor is the background color of the graph.
	BackgroundColor string
	// Color is the text color of the graph.
	Color string
	// ReactColor is the color used for interactive elements.
	ReactColor string
	// StrokeColor is the color used for strokes around elements.
	StrokeColor string
}

DefaultTheme defines the default theme structure.

func Dark

func Dark() *DefaultTheme

Dark returns a dark theme.

func Light

func Light() *DefaultTheme

Light returns a light theme. It's the default theme.

func (*DefaultTheme) GetName

func (t *DefaultTheme) GetName() string

GetName returns the name of the theme.

func (*DefaultTheme) GetStyle

func (t *DefaultTheme) GetStyle() string

GetStyle generates the CSS for the theme.

type Options

type Options struct {
	// Theme is the theme name to be used for the graph.
	// See RegisterTheme for more information.
	Theme string
}

Options defines the options for the Controller.

type Theme

type Theme interface {
	// GetName returns the name of the theme.
	GetName() string
	// GetStyle generates the CSS for the theme.
	GetStyle() string
}

Theme represents a theme for the graph.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL