websrv

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2021 License: BSD-3-Clause Imports: 8 Imported by: 0

README

Using the server and client with the proto

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/deweppro/go-http/v2/proto"
	"github.com/deweppro/go-http/v2/clients/webcli"
	"github.com/deweppro/go-http/v2/servers/httpsrv"
	"github.com/deweppro/go-http/v2/servers/websrv"
	"github.com/deweppro/go-logger"
)

type A struct {
	Name string `json:"name"`
}

type B struct {
	Name string `json:"name"`
}

type Demo struct{}

func (s *Demo) Index(in *proto.Request, out *proto.Response) {
	var model B
	if err := in.DecodeJSON(&model); err != nil {
		fmt.Println("Index Decode", err)
	}

	out.SetStatusCode(proto.StatusCodeOK)
	if err := out.EncodeJSON(B{Name: model.Name + "+B"}); err != nil {
		fmt.Println("Index Encode", err)
	}
}

func main() {

	/**
	init client
	*/

	sign := proto.NewSigner("1", "1234567890")
	pool := proto.Pool{Items: map[string]proto.List{
		"demo": []string{"http://localhost:8090"},
	}}

	cli := webcli.NewClient()
	cli.Debug(true, os.Stdout)

	protoClient := proto.NewClient(pool)
	protoClient.Client("demo", cli.Call)

	/**
	init server
	*/

	protoServer := proto.NewServer()
	protoServer.Handler("/", 1, (&Demo{}).Index)

	conf := &websrv.Config{
		HTTP: httpsrv.ConfigItem{Addr: `localhost:8080`},
		Headers: websrv.Headers{
			ProxyHeaders:   []string{"X-Forwarded-For", "Accept-Language", "User-Agent"},
			DefaultHeaders: map[string]string{"Content-Type": "text/html"},
		},
	}

	srv := websrv.NewServer(conf, protoServer, logger.Default())
	if err := srv.Up(); err != nil {
		panic(err)
	}
	defer func() {
		if err := srv.Down(); err != nil {
			panic(err)
		}
	}()

	<-time.After(time.Second * 2)

	/**
	make request
	*/

	req := proto.NewRequest()
	res := proto.NewResponse()

	if err := req.EncodeJSON(A{Name: "A"}); err != nil {
		fmt.Println("client encode", err)
	}
	req.CreateSign(sign)
	if err := protoClient.Call("demo", req, res); err != nil {
		fmt.Println(err)
	}

	var result A
	if err := res.DecodeJSON(&result); err != nil {
		fmt.Println("client decode", err)
	}

	fmt.Println("UUID is", req.GetUUID() == res.GetUUID())
	fmt.Println("BODY is", result.Name == "A+B")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	HTTP    httpsrv.ConfigItem `yaml:"http" json:"http"`
	Headers Headers            `yaml:"headers" json:"headers"`
}

Config model

func (Config) MarshalEasyJSON

func (v Config) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (Config) MarshalJSON

func (v Config) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*Config) UnmarshalEasyJSON

func (v *Config) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*Config) UnmarshalJSON

func (v *Config) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type Headers

type Headers struct {
	ProxyHeaders   []string          `yaml:"proxy_headers" json:"proxy_headers"`
	DefaultHeaders map[string]string `yaml:"default_headers" json:"default_headers"`
}

Headers model

func (Headers) MarshalEasyJSON

func (v Headers) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (Headers) MarshalJSON

func (v Headers) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*Headers) UnmarshalEasyJSON

func (v *Headers) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*Headers) UnmarshalJSON

func (v *Headers) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type Server

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

Server ...

func NewServer

func NewServer(c *Config, h *proto.Server, l logger.Logger) *Server

NewServer ...

func (*Server) Down

func (s *Server) Down() error

Down ...

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP ...

func (*Server) Up

func (s *Server) Up() error

Up ...

Jump to

Keyboard shortcuts

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