From f8655e116c23a7fdcf793646ea4764dca3f93f42 Mon Sep 17 00:00:00 2001 From: crusader Date: Fri, 25 Aug 2017 19:41:43 +0900 Subject: [PATCH] ing --- handler/file/file.go | 49 +++++++++++++++++++++++++++++++++++++++++++ handler/handler.go | 9 ++++++++ handler/web/web.go | 50 ++++++++++++++++++++++++++++++++++++++++++++ main.go | 47 +++++++++++++++++++++++------------------ 4 files changed, 135 insertions(+), 20 deletions(-) create mode 100644 handler/file/file.go create mode 100644 handler/handler.go create mode 100644 handler/web/web.go diff --git a/handler/file/file.go b/handler/file/file.go new file mode 100644 index 0000000..e5c0fd0 --- /dev/null +++ b/handler/file/file.go @@ -0,0 +1,49 @@ +package file + +import ( + "log" + + "git.loafle.net/overflow/overflow_gateway_web/handler" + gws "git.loafle.net/overflow/overflow_gateway_websocket" + "git.loafle.net/overflow/overflow_gateway_websocket/protocol/jsonrpc" +) + +type FileHandler interface { + handler.Handler +} + +type fileHandler struct { + co *gws.SocketOptions + ho *jsonrpc.Options + handler gws.MessageHandler +} + +func New() FileHandler { + h := &fileHandler{} + + h.ho = &jsonrpc.Options{ + OnRequest: h.onRequest, + OnNotify: h.onNotify, + } + + h.handler = jsonrpc.NewHandler(h.ho) + + h.co = &gws.SocketOptions{ + Handler: h.handler, + } + + return h +} + +func (h *fileHandler) GetSocketOption() *gws.SocketOptions { + return h.co +} + +func (h *fileHandler) onRequest(soc gws.Socket, method string, params interface{}) (interface{}, error) { + log.Printf("path: %s, m: %s, params: %v", soc.Path(), method, params) + return nil, nil +} + +func (h *fileHandler) onNotify(soc gws.Socket, method string, params interface{}) error { + return nil +} diff --git a/handler/handler.go b/handler/handler.go new file mode 100644 index 0000000..fc2c727 --- /dev/null +++ b/handler/handler.go @@ -0,0 +1,9 @@ +package handler + +import ( + gws "git.loafle.net/overflow/overflow_gateway_websocket" +) + +type Handler interface { + GetSocketOption() *gws.SocketOptions +} diff --git a/handler/web/web.go b/handler/web/web.go new file mode 100644 index 0000000..c47e124 --- /dev/null +++ b/handler/web/web.go @@ -0,0 +1,50 @@ +package web + +import ( + "log" + + "git.loafle.net/overflow/overflow_gateway_web/handler" + gws "git.loafle.net/overflow/overflow_gateway_websocket" + "git.loafle.net/overflow/overflow_gateway_websocket/protocol/jsonrpc" +) + +type WebHandler interface { + handler.Handler +} + +type webHandler struct { + co *gws.SocketOptions + ho *jsonrpc.Options + handler gws.MessageHandler +} + +func New() WebHandler { + h := &webHandler{} + + h.ho = &jsonrpc.Options{ + OnRequest: h.onRequest, + OnNotify: h.onNotify, + } + + h.handler = jsonrpc.NewHandler(h.ho) + + h.co = &gws.SocketOptions{ + Handler: h.handler, + } + + return h +} + +func (h *webHandler) GetSocketOption() *gws.SocketOptions { + return h.co +} + +func (h *webHandler) onRequest(soc gws.Socket, method string, params interface{}) (interface{}, error) { + log.Printf("path: %s, m: %s, params: %v", soc.Path(), method, params) + return nil, nil +} + +func (h *webHandler) onNotify(soc gws.Socket, method string, params interface{}) error { + log.Printf("path: %s, m: %s, params: %v", soc.Path(), method, params) + return nil +} diff --git a/main.go b/main.go index a4ff684..7988fd1 100644 --- a/main.go +++ b/main.go @@ -1,40 +1,47 @@ package main import ( - "git.loafle.net/overflow/overflow_gateway_websocket" - "git.loafle.net/overflow/overflow_gateway_websocket/clients" + "log" + + "github.com/valyala/fasthttp" + + "git.loafle.net/overflow/overflow_gateway_web/handler/file" + "git.loafle.net/overflow/overflow_gateway_web/handler/web" + gws "git.loafle.net/overflow/overflow_gateway_websocket" ) func main() { - o := &overflow_gateway_websocket.Options{ + o := &gws.ServerOptions{ OnConnection: onConnection, OnDisconnected: onDisconnected, + OnCheckOrigin: onCheckOrigin, } - s := overflow_gateway_websocket.NewServer(o) + s := gws.NewServer(o) - co := &clients.Options{ - OnRequest: onRequest, - OnNotify: onNotify, - } + wh := web.New() + fh := file.New() - s.HandleClient("/web", co) + s.HandleSocket("/web", wh.GetSocketOption()) + s.HandleSocket("/file", fh.GetSocketOption()) s.ListenAndServe(":19090") } -func onConnection(path string, c clients.Client) { - +func onCheckOrigin(ctx *fasthttp.RequestCtx) bool { + if origin := string(ctx.Request.Header.Peek("Origin")); origin != "" { + ctx.Response.Header.Set("Access-Control-Allow-Origin", origin) + if string(ctx.Method()) == "OPTIONS" && string(ctx.Request.Header.Peek("Access-Control-Request-Method")) != "" { + ctx.Response.Header.Set("Access-Control-Allow-Headers", "Content-Type, Accept") + ctx.Response.Header.Set("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE") + } + } + return true } -func onDisconnected(c clients.Client) { - +func onConnection(soc gws.Socket) { + log.Printf("connect: path: %s, id:%s\n", soc.Path(), soc.ID()) } -func onRequest(c clients.Client, method string, params interface{}) (interface{}, error) { - - return nil, nil -} - -func onNotify(c clients.Client, method string, params interface{}) error { - return nil +func onDisconnected(soc gws.Socket) { + log.Printf("disconnect: path: %s, id:%s\n", soc.Path(), soc.ID()) }