gofunc

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: MIT Imports: 6 Imported by: 0

README

Example:

package main

import (
	"context"
	"fmt"

	"github.com/ottstack/gofunc"
	"github.com/ottstack/gofunc/pkg/middleware"
	"github.com/ottstack/gofunc/pkg/websocket"
	"github.com/valyala/fasthttp"
)

type Request struct {
	Name string `json:"name" schema:"name" validate:"required" comment:"Required Name"`
}

type Response struct {
	Reply string `json:"reply"`
}

func HelloFunc(ctx context.Context, req *Request, rsp *Response) error {
	rsp.Reply = "Hello " + req.Name
	return nil
}

func Stream(ctx context.Context, req websocket.RecvStream, rsp websocket.SendStream) error {
	ct := 0
	for {
		msg, err := req.Recv()
		if err != nil {
			return err
		}
		fmt.Println("recv", string(msg))
		ct++
		if err := rsp.Send([]byte(fmt.Sprintf("hello %s %d times", string(msg), ct))); err != nil {
			return err
		}
		fmt.Println("send", string(msg))
		if ct > 2 {
			return nil
		}
	}
}

func main() {
	// curl '127.0.0.1:9001/api/hello?name=bob'
	// curl -X PUT '127.0.0.1:9001/api/hello' -d '{"name":"tom"}'
	gofunc.New("Default").
		Get("/api/hello", HelloFunc).
		Put("/api/hello", HelloFunc)

	// websocket: 127.0.0.1:9001/api/hello-ws
	gofunc.New("OtherService").
		Stream("/api/hello-ws", Stream)

	// origin http: curl '127.0.0.1:9001/api/hello/2'
	gofunc.HandleHTTP("GET", "/api/hello/2", func(rc *fasthttp.RequestCtx) {
		rc.Response.BodyWriter().Write([]byte("HELLO FAST HTTP"))
	})

	gofunc.Use(middleware.Recover).Use(middleware.Validator)
	gofunc.Serve()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleHTTP

func HandleHTTP(method, path string, f func(*fasthttp.RequestCtx))

func Serve

func Serve()

Serve ...

func Use

Types

type Router

type Router struct {
	// contains filtered or unexported fields
}

func ApiGroup added in v0.0.2

func ApiGroup(name string) *Router

func (*Router) Delete

func (r *Router) Delete(path string, function interface{}) *Router

func (*Router) Get

func (r *Router) Get(path string, function interface{}) *Router

func (*Router) Post

func (r *Router) Post(path string, function interface{}) *Router

func (*Router) Put

func (r *Router) Put(path string, function interface{}) *Router

func (*Router) Stream

func (r *Router) Stream(path string, function interface{}) *Router

Directories

Path Synopsis
example
internal
pkg

Jump to

Keyboard shortcuts

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