lemo

package module
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2020 License: Apache-2.0 Imports: 1 Imported by: 0

README

package main

import (
	"net/http/httputil"
	"net/url"
	"os"

	"github.com/Lemo-yxk/lemo"
	"github.com/Lemo-yxk/lemo/console"
	"github.com/Lemo-yxk/lemo/exception"
	"github.com/Lemo-yxk/lemo/utils"
)

func main() {

	var httpServer = lemo.HttpServer{Host: "0.0.0.0", Port: 8666}

	var httpServerRouter = &lemo.HttpServerRouter{}

	httpServer.Use(func(next lemo.HttpServerMiddle) lemo.HttpServerMiddle {
		return func(stream *lemo.Stream) {
			if stream.Request.Header.Get("Upgrade") == "websocket" {
				httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: "0.0.0.0:8667"}).ServeHTTP(stream.Response, stream.Request)
			} else {
				next(stream)
			}
		}
	})

	httpServerRouter.Group("/hello").Handler(func(handler *lemo.HttpServerRouteHandler) {
		handler.Get("/world").Handler(func(t *lemo.Stream) exception.Error {
			// console.Log(t.Json.Empty("a"))
			// console.Log(t.Query.Get("a"))
			// console.Log(t.Form.Get("a"))
			// console.Log(t.Files.Get("a"))
			return t.JsonFormat("SUCCESS", 200, "hello world")
		})
	})

	go httpServer.SetRouter(httpServerRouter).Start()

	var webSocketServer = &lemo.WebSocketServer{Host: "0.0.0.0", Port: 8667, Path: "/"}

	var webSocketServerRouter = &lemo.WebSocketServerRouter{}

	webSocketServer.Use(func(next lemo.WebSocketServerMiddle) lemo.WebSocketServerMiddle {
		return func(conn *lemo.WebSocket, receive *lemo.ReceivePackage) {
			console.Log(1)
			next(conn, receive)
			console.Log(2)
		}
	})

	webSocketServerRouter.Group("/hello").Handler(func(handler *lemo.WebSocketServerRouteHandler) {
		handler.Route("/world").Handler(func(conn *lemo.WebSocket, receive *lemo.Receive) exception.Error {
			console.Log(3)
			return conn.JsonFormat(lemo.JsonPackage{
				Event: "/hello/world",
				Message: &lemo.JsonMessage{
					Status: "",
					Code:   0,
					Msg:    nil,
				},
			})
		})
	})

	go webSocketServer.SetRouter(webSocketServerRouter).Start()

	console.Log("start success")

	utils.Signal.ListenAll().Done(func(sig os.Signal) {
		console.Log(sig)
	})
}


Documentation

Index

Constants

View Source
const (
	// Version
	Version byte = 'V'

	// message type
	Unknown  int = 0
	TextData int = 1
	BinData  int = 2
	PingData int = 9
	PongData int = 10

	// proto type
	Text     int = 1
	Json     int = 2
	ProtoBuf int = 3
)
View Source
const (
	XForwardedFor = "X-Forwarded-For"
	XRealIP       = "X-Real-IP"
	Host          = "Host"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type A

type A []interface{}

type Context

type Context interface{}

type JsonPackage

type JsonPackage struct {
	Event string      `json:"event"`
	Data  interface{} `json:"data"`
}

type M

type M map[string]interface{}

type Params

type Params struct {
	Keys   []string
	Values []string
}

func (Params) ByName

func (ps Params) ByName(name string) string

type ProtoBufPackage

type ProtoBufPackage struct {
	Event string
	Data  proto.Message
}

type PushPackage added in v1.0.9

type PushPackage struct {
	Type int
	FD   uint32
	Data []byte
}

type Receive

type Receive struct {
	Context Context
	Params  Params
	Body    *ReceivePackage
}

type ReceivePackage

type ReceivePackage struct {
	MessageType int
	Event       string
	Message     []byte
	ProtoType   int
	Raw         []byte
}

Jump to

Keyboard shortcuts

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