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 package main
import ( import (
"log"
"os"
"os/signal"
"syscall"
"github.com/OktopUSP/oktopus/ws/internal/config"
"github.com/OktopUSP/oktopus/ws/internal/ws" "github.com/OktopUSP/oktopus/ws/internal/ws"
) )
// TODO: refact from where this version is get
const VERSION = "0.0.1"
func main() { 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" "log"
"net/http" "net/http"
"github.com/OktopUSP/oktopus/ws/internal/config"
"github.com/OktopUSP/oktopus/ws/internal/ws/handler" "github.com/OktopUSP/oktopus/ws/internal/ws/handler"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
// Starts New Websockets Server // Starts New Websockets Server
func StartNewServer() { func StartNewServer(c config.Config) {
// Initialize handlers of websockets events // Initialize handlers of websockets events
go handler.InitHandlers() go handler.InitHandlers()
@ -20,14 +21,15 @@ func StartNewServer() {
handler.ServeAgent(w, r) handler.ServeAgent(w, r)
}) })
r.HandleFunc("/ws/controller", func(w http.ResponseWriter, r *http.Request) { 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") log.Println("Websockets server running")
// Blocks application running until it receives a KILL signal go func() {
err := http.ListenAndServe(":8080", r) err := http.ListenAndServe(c.Port, r)
if err != nil { if err != nil {
log.Fatal("ListenAndServe: ", err) log.Fatal("ListenAndServe: ", err)
} }
}()
} }