mirror of
https://github.com/cadmium-im/zirconium-go.git
synced 2024-12-27 11:21:51 +00:00
Initialize server template
This commit is contained in:
commit
5d823dbabe
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/zr
|
59
cmd/zr/main.go
Normal file
59
cmd/zr/main.go
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
}
|
9
go.mod
Normal file
9
go.mod
Normal file
@ -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
|
||||
)
|
6
go.sum
Normal file
6
go.sum
Normal file
@ -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=
|
1
internal/handler.go
Normal file
1
internal/handler.go
Normal file
@ -0,0 +1 @@
|
||||
package internal
|
Loading…
Reference in New Issue
Block a user