http

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HttpInlet

func HttpInlet(ctx *engine.Context) engine.Inlet
Example
package main

import (
	"bytes"
	"fmt"
	"io"
	"net"
	"net/http"
	"strings"
	"time"

	"github.com/OutOfBedlam/tine/engine"
	_ "github.com/OutOfBedlam/tine/plugin/codec/compress"
	_ "github.com/OutOfBedlam/tine/plugin/codec/csv"
	_ "github.com/OutOfBedlam/tine/plugin/codec/json"
	_ "github.com/OutOfBedlam/tine/plugin/flows/base"
	_ "github.com/OutOfBedlam/tine/plugin/inlets/http"
	_ "github.com/OutOfBedlam/tine/plugin/outlets/file"
)

var httpServer *http.Server

func runTestServer() (string, error) {
	httpServer = &http.Server{
		Addr: "127.0.0.1:0",
		Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("Content-Type", "application/json")
			w.Write([]byte(`{"a":1, "b":{"c":true, "d":3.14}}` + "\n"))
			w.WriteHeader(200)
		}),
	}

	lsnr, err := net.Listen("tcp", httpServer.Addr)
	if err != nil {
		return "", err
	}

	go http.Serve(lsnr, httpServer.Handler)

	// Get the address the server is listening on
	addr := lsnr.Addr().String()
	return addr, nil
}

func main() {
	addr, err := runTestServer()
	if err != nil {
		panic(err)
	}
	defer httpServer.Close()

	dsl := fmt.Sprintf(`
	[[inlets.http]]
		address = "http://%s"
		success = 200
		timeout = "3s"
		count = 1
	[[flows.select]]
		includes = ["**"]
	[[outlets.file]]
		format = "json"
	`, addr)
	// Make the output time deterministic. so we can compare it.
	// This line is not needed in production code.
	engine.Now = func() time.Time { return time.Unix(1721954797, 0) }
	// Create a new engine.
	out := &bytes.Buffer{}
	pipeline, err := engine.New(engine.WithConfig(dsl), engine.WithWriter(out))
	if err != nil {
		panic(err)
	}
	// Run the engine.
	if err := pipeline.Run(); err != nil {
		panic(err)
	}
	result, _ := io.ReadAll(out)
	fmt.Println(strings.TrimSpace(string(result)))
}
Output:

{"_in":"http","_ts":1721954797,"a":1,"b.c":true,"b.d":3.14}

func Map2Record added in v0.0.4

func Map2Record(prefix string, obj map[string]any) engine.Record

Types

This section is empty.

Jump to

Keyboard shortcuts

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