goapi

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: MIT Imports: 19 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

View Source
var API_JSON = ""

Functions

This section is empty.

Types

type Server added in v0.0.2

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

func NewServer added in v0.0.2

func NewServer() *Server

func (*Server) RegisterHTTP added in v0.0.2

func (s *Server) RegisterHTTP(path string, function func(*fasthttp.RequestCtx))

func (*Server) RegisterService added in v0.0.2

func (s *Server) RegisterService(services ...interface{})

func (*Server) Serve added in v0.0.2

func (s *Server) Serve() error

func (*Server) Use added in v0.0.2

func (s *Server) Use(m middleware.Middleware) *Server

Directories

Path Synopsis
example
client
This file auto generated by sscli
This file auto generated by sscli
pkg

Jump to

Keyboard shortcuts

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