feat(ws): adequate ws server to use dynamic config

This commit is contained in:
leandrofars 2024-02-06 00:17:56 -03:00
parent 9e549f722f
commit ef45c5cb0c
2 changed files with 32 additions and 8 deletions

View File

@ -1,9 +1,31 @@
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/OktopUSP/oktopus/ws/internal/config"
"github.com/OktopUSP/oktopus/ws/internal/ws"
)
// TODO: refact from where this version is get
const VERSION = "0.0.1"
func main() {
ws.StartNewServer()
done := make(chan os.Signal, 1)
conf := config.NewConfig()
// Locks app running until it receives a stop command as Ctrl+C.
signal.Notify(done, syscall.SIGINT)
log.Println("Starting Oktopus Websockets Version:", VERSION)
ws.StartNewServer(conf)
<-done
log.Println("(⌐■_■) Websockets server is out!")
}

View File

@ -6,12 +6,13 @@ import (
"log"
"net/http"
"github.com/OktopUSP/oktopus/ws/internal/config"
"github.com/OktopUSP/oktopus/ws/internal/ws/handler"
"github.com/gorilla/mux"
)
// Starts New Websockets Server
func StartNewServer() {
func StartNewServer(c config.Config) {
// Initialize handlers of websockets events
go handler.InitHandlers()
@ -20,14 +21,15 @@ func StartNewServer() {
handler.ServeAgent(w, r)
})
r.HandleFunc("/ws/controller", func(w http.ResponseWriter, r *http.Request) {
//TODO: Implement controller handler
handler.ServeController(w, r, c.Token)
})
log.Println("Websockets server running")
// Blocks application running until it receives a KILL signal
err := http.ListenAndServe(":8080", r)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
go func() {
err := http.ListenAndServe(c.Port, r)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}()
}