From 3dcdeb38cbd21c2e6333e9a53f7e4bc0073bc3f5 Mon Sep 17 00:00:00 2001 From: sp Date: Thu, 4 Mar 2021 09:53:17 +1100 Subject: [PATCH] websocket implemented with dummy send as a toy for test. --- README.md | 17 ++++++++++++- apiv1.go | 1 + go.mod | 3 +-- go.sum | 2 ++ html/index.html | 32 ++++++++++++++++++++++++ main.go | 5 ++-- websocket.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 websocket.go diff --git a/README.md b/README.md index 79dda65..e47b12e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,22 @@ # Readme for RestAPI Version 1 -1. /login +##1. /login + +input + **username** : must be email + **password** : 4-40 chars + +output + +```json +{ + "login": true, + "Biukop-Session": "biukop session id", + "Biukop-Mid": "biukop machine id" +} +``` + 1. /signup 2. /logout 3. /loans?skip=page= GET diff --git a/apiv1.go b/apiv1.go index 83e6d1e..8b16c54 100644 --- a/apiv1.go +++ b/apiv1.go @@ -15,6 +15,7 @@ import ( ) const apiV1Prefix = "/api/v1/" +const apiV1WebSocket = apiV1Prefix + "ws" type apiV1HandlerMap struct { Method string diff --git a/go.mod b/go.mod index c359225..b785722 100644 --- a/go.mod +++ b/go.mod @@ -6,9 +6,8 @@ replace biukop.com/sfm/loan => /home/sp/GolandProjects/SFM-loan require ( biukop.com/sfm/loan v0.0.0-00010101000000-000000000000 - github.com/VividCortex/mysqlerr v0.0.0-20201215173831-4c396ae82aac github.com/brianvoe/gofakeit/v6 v6.0.1 - github.com/go-sql-driver/mysql v1.5.0 + github.com/gorilla/websocket v1.4.2 github.com/sirupsen/logrus v1.7.0 github.com/stretchr/testify v1.2.2 ) diff --git a/go.sum b/go.sum index 5bd02a7..828fb15 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= diff --git a/html/index.html b/html/index.html index 84bdf5f..55e588c 100644 --- a/html/index.html +++ b/html/index.html @@ -24,7 +24,39 @@ static url /test → "./html/test/" + + +

diff --git a/main.go b/main.go index 4209b80..91bc951 100644 --- a/main.go +++ b/main.go @@ -12,8 +12,9 @@ import ( type httpEntry func(http.ResponseWriter, *http.Request) var httpEntryMap = map[string]httpEntry{ - apiV1Prefix: apiV1Main, - "/dummy/": dummyHandler, + apiV1Prefix: apiV1Main, + apiV1WebSocket: apiV1WebSocketHandler, + "/dummy/": dummyHandler, } func main() { diff --git a/websocket.go b/websocket.go new file mode 100644 index 0000000..ad20365 --- /dev/null +++ b/websocket.go @@ -0,0 +1,65 @@ +package main + +import ( + "fmt" + "github.com/gorilla/websocket" + log "github.com/sirupsen/logrus" + "net/http" + "time" +) + +// We'll need to define an upgradeToWs +// this will require a Read and Write buffer size +var upgradeToWs = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, +} + +func apiV1WebSocketHandler(w http.ResponseWriter, r *http.Request) { + upgradeToWs.CheckOrigin = func(r *http.Request) bool { return true } + ws, err := upgradeToWs.Upgrade(w, r, nil) + if err != nil { + log.Println(err) + } + // helpful log statement to show connections + log.Println("Websocket Api/V1: Client Connected") + + wsReader(ws) +} + +func wsReader(conn *websocket.Conn) { + for { + // read in a message + messageType, p, err := conn.ReadMessage() + if err != nil { + log.Println(err) + return + } + // print out that message for clarity + fmt.Println(string(p)) + + if err := conn.WriteMessage(messageType, p); err != nil { + log.Println(err) + return + } + + if string(p) == "send dummy string for 500 times" { + go wsDummySender(conn) + } + } +} + +func wsDummySender(conn *websocket.Conn) { + //write subsequent 5 copies, each after 1 second + log.Info("start sending server data to client ..") + p := "dummy string from server " + for i := 1; i < 500; i++ { + time.Sleep(1 * time.Second) + msg := fmt.Sprintf("copy %d, %s", i, p) + log.Info("dummy sender is working, ", msg) + if err := conn.WriteMessage(websocket.TextMessage, []byte(msg)); err != nil { + log.Println("wsDummySender stopped on error: ", err) + return + } + } +}