caddyhandlerPlugin

package module
v0.0.0-...-f3e4cf9 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: MIT Imports: 8 Imported by: 0

README

caddy-handler-plugin

Build go-plugin for caddy handler

Usage

Create handler plugin

hanlder.go

package main

import (
    "fmt"
    "net/http"

    "github.com/hashicorp/go-hclog"

    "github.com/gsmlg-dev/caddy-handler-plugin/shared"
    "github.com/gsmlg-dev/caddy-handler-plugin/server"
)

type HandlerServer struct {
  logger hclog.Logger
  server.HandlerServerDefault
}

func (g *HandlerServer) Serve(q shared.PluginQuery, reply *shared.PluginReply) error {
  reply.Done = true
  header := http.Header{}
  header.Set("Server-Handler", "Custom Caddy Handler")
  header.Set("Content-Type", "text/plain")
  reply.Header = header
  out := fmt.Sprintf(`Hello World

  * with Query:

  %v

  * with Config:

  %v
`, q, g.Config)
  reply.Body = []byte(out)
  return nil
}

func main() {
  logger := hclog.New(&hclog.LoggerOptions{
		Level:      hclog.Trace,
		Output:     os.Stderr,
		JSONFormat: true,
	})
  handler := &HandlerServer{
    logger: logger,
  }
  server.New(handler)
}
# Build Plugin
go build -o hanlder.bin hanlder.go

Built Caddy with caddy-handler-plugin

xcaddy build --with github.com/gsmlg-dev/caddy-handler-plugin

Load plugin in Caddyfile

localhost:8080 {
    handler_plugin * "/caddy-plugins/hanlder.bin" {
        name "web handler"
        pass_next_if_not_match false
    }
}

Examples

TODO

  • Add logger

Documentation

Index

Constants

View Source
const DirectiveName = "handler_plugin"

Variables

This section is empty.

Functions

This section is empty.

Types

type CaddyHandlerPlugin

type CaddyHandlerPlugin struct {
	PluginPath   string              `json:"plugin_path,omitempty"`
	PluginConfig map[string][]string `json:"plugin_config,omitempty"`
	// contains filtered or unexported fields
}

func (CaddyHandlerPlugin) CaddyModule

func (CaddyHandlerPlugin) CaddyModule() caddy.ModuleInfo

func (*CaddyHandlerPlugin) Cleanup

func (chp *CaddyHandlerPlugin) Cleanup() error

func (*CaddyHandlerPlugin) Provision

func (chp *CaddyHandlerPlugin) Provision(ctx caddy.Context) error

func (*CaddyHandlerPlugin) ServeHTTP

func (*CaddyHandlerPlugin) UnmarshalCaddyfile

func (chp *CaddyHandlerPlugin) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

UnmarshalCaddyfile implements caddyfile.Unmarshaler. Syntax:

hanler_plgun <plugin_path> {
    <plugin_config>
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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