| # Readme for RestAPI | # Readme for RestAPI | ||||
| Version 1 | 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 | 1. /signup | ||||
| 2. /logout | 2. /logout | ||||
| 3. /loans?skip=page= GET | 3. /loans?skip=page= GET |
| ) | ) | ||||
| const apiV1Prefix = "/api/v1/" | const apiV1Prefix = "/api/v1/" | ||||
| const apiV1WebSocket = apiV1Prefix + "ws" | |||||
| type apiV1HandlerMap struct { | type apiV1HandlerMap struct { | ||||
| Method string | Method string |
| require ( | require ( | ||||
| biukop.com/sfm/loan v0.0.0-00010101000000-000000000000 | 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/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/sirupsen/logrus v1.7.0 | ||||
| github.com/stretchr/testify v1.2.2 | github.com/stretchr/testify v1.2.2 | ||||
| ) | ) |
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | 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 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= | ||||
| github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= | 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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||||
| github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= | github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= |
| static url <a href="test/">/test </a> → "./html/test/" | static url <a href="test/">/test </a> → "./html/test/" | ||||
| </li> | </li> | ||||
| <script> | |||||
| let socket = new WebSocket("wss://svr2021.lawipac.com:8080/api/v1/ws"); | |||||
| console.log("Attempting Connection..."); | |||||
| socket.onopen = () => { | |||||
| console.log("Successfully Connected"); | |||||
| socket.send("Hi From the Client!"); | |||||
| socket.send("send dummy string for 500 times"); //this is a special command server will respond; | |||||
| }; | |||||
| socket.onclose = event => { | |||||
| console.log("Socket Closed Connection: ", event); | |||||
| socket.send("Client Closed!") | |||||
| }; | |||||
| socket.onerror = error => { | |||||
| console.log("Socket Error: ", error); | |||||
| }; | |||||
| socket.onmessage = e => { | |||||
| if(typeof e.data != "string" ) { | |||||
| console.log("invalid data received ", e) | |||||
| }else{ | |||||
| let server_message = e.data; | |||||
| console.log("server said: ", server_message) | |||||
| document.getElementById("socketOutPut").innerHTML= server_message; | |||||
| } | |||||
| } | |||||
| </script> | |||||
| </ol> | </ol> | ||||
| <p id="socketOutPut"></p> | |||||
| </div> | </div> | ||||
| type httpEntry func(http.ResponseWriter, *http.Request) | type httpEntry func(http.ResponseWriter, *http.Request) | ||||
| var httpEntryMap = map[string]httpEntry{ | var httpEntryMap = map[string]httpEntry{ | ||||
| apiV1Prefix: apiV1Main, | |||||
| "/dummy/": dummyHandler, | |||||
| apiV1Prefix: apiV1Main, | |||||
| apiV1WebSocket: apiV1WebSocketHandler, | |||||
| "/dummy/": dummyHandler, | |||||
| } | } | ||||
| func main() { | func main() { |
| 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 | |||||
| } | |||||
| } | |||||
| } |