From 5d823dbabee54ebc145cbb1e36619d9be8e4efba Mon Sep 17 00:00:00 2001 From: ChronosX88 Date: Fri, 7 Feb 2020 14:41:29 +0400 Subject: [PATCH] Initialize server template --- .gitignore | 1 + cmd/zr/main.go | 59 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 9 +++++++ go.sum | 6 +++++ internal/handler.go | 1 + 5 files changed, 76 insertions(+) create mode 100644 .gitignore create mode 100644 cmd/zr/main.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/handler.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5d8caaa --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/zr diff --git a/cmd/zr/main.go b/cmd/zr/main.go new file mode 100644 index 0000000..8d30f05 --- /dev/null +++ b/cmd/zr/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "log" + "net/http" + + "github.com/google/uuid" + "github.com/gorilla/mux" + "github.com/gorilla/websocket" +) + +var clients = make(map[*websocket.Conn]string) +var clientsReverse = make(map[string]*websocket.Conn) +var upgrader = websocket.Upgrader{ + CheckOrigin: func(r *http.Request) bool { + return true + }, +} + +func main() { + router := mux.NewRouter() + router.HandleFunc("/", func(response http.ResponseWriter, request *http.Request) { + response.Write([]byte("Zirconium server is up and running!")) + }).Methods("GET") + router.HandleFunc("/ws", wsHandler) + + log.Println("Zirconium successfully started!") + log.Fatal(http.ListenAndServe(":8844", router)) +} + +func wsHandler(w http.ResponseWriter, r *http.Request) { + ws, err := upgrader.Upgrade(w, r, nil) + if err != nil { + log.Fatal(err) + } + + // register client + clients[ws] = uuid.New().String() + clientsReverse[clients[ws]] = ws + go readLoop(ws) + log.Printf("Connection %s created!", clients[ws]) +} + +func readLoop(c *websocket.Conn) { + for { + if _, _, err := c.NextReader(); err != nil { + connectionID := clients[c] + if connectionID != "" { + delete(clients, c) + delete(clientsReverse, connectionID) + log.Printf("Connection %s closed!", connectionID) + } else { + log.Println("connection wasn't found") + } + c.Close() + break + } + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3817981 --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/ChronosX88/zirconium + +go 1.13 + +require ( + github.com/google/uuid v1.1.1 + github.com/gorilla/mux v1.7.3 + github.com/gorilla/websocket v1.4.1 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..dbe64b7 --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= +github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= +github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= diff --git a/internal/handler.go b/internal/handler.go new file mode 100644 index 0000000..5bf0569 --- /dev/null +++ b/internal/handler.go @@ -0,0 +1 @@ +package internal