From 5d71f42361ba16121020afd0fee016d289cfc543 Mon Sep 17 00:00:00 2001 From: Abdulhade Date: Fri, 14 Nov 2025 03:13:17 +0300 Subject: [PATCH 1/4] Created the main router with ping endpoint --- api/http_handler.go | 27 +++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 2 ++ main.go | 7 ++++++- 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 api/http_handler.go create mode 100644 go.sum diff --git a/api/http_handler.go b/api/http_handler.go new file mode 100644 index 0000000..c348bdc --- /dev/null +++ b/api/http_handler.go @@ -0,0 +1,27 @@ +package api + +import ( + "fmt" + "log" + "net/http" + + "github.com/gorilla/mux" +) + +func ping(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, "pong") +} + +func BuildRouter(listenAddress string) { + mainRouter := mux.NewRouter() + + mainRouter.HandleFunc("/ping", ping) + + // api_router := base_router.PathPrefix("/api").Subrouter() + + err := http.ListenAndServe(listenAddress, mainRouter) + + if err != nil { + log.Fatalf("Error running the web server %s", err) + } +} diff --git a/go.mod b/go.mod index a251ea0..2113e65 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module echo go 1.22.2 + +require github.com/gorilla/mux v1.8.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7128337 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= diff --git a/main.go b/main.go index 300c7c2..ad14952 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,12 @@ package main -import "fmt" +import ( + "echo/api" + "fmt" +) func main() { fmt.Println("Hello from echo-echo-cho-o") + + api.BuildRouter(":8080") } From d506865ca4a123c44e263382231fcffefa519db4 Mon Sep 17 00:00:00 2001 From: Abdulhade Date: Fri, 14 Nov 2025 03:30:08 +0300 Subject: [PATCH 2/4] Created Front-end files router --- api/http_handler.go | 9 +++++++++ front_files/index.css | 0 front_files/index.html | 3 +++ front_files/index.js | 0 4 files changed, 12 insertions(+) create mode 100644 front_files/index.css create mode 100644 front_files/index.html create mode 100644 front_files/index.js diff --git a/api/http_handler.go b/api/http_handler.go index c348bdc..8ea5676 100644 --- a/api/http_handler.go +++ b/api/http_handler.go @@ -12,10 +12,19 @@ func ping(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "pong") } +func serveStaticFile(fileName string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "front_files/"+fileName) + } +} + func BuildRouter(listenAddress string) { mainRouter := mux.NewRouter() mainRouter.HandleFunc("/ping", ping) + mainRouter.HandleFunc("/", serveStaticFile("index.html")) + mainRouter.HandleFunc("/index.js", serveStaticFile("index.js")) + mainRouter.HandleFunc("/index.css", serveStaticFile("index.css")) // api_router := base_router.PathPrefix("/api").Subrouter() diff --git a/front_files/index.css b/front_files/index.css new file mode 100644 index 0000000..e69de29 diff --git a/front_files/index.html b/front_files/index.html new file mode 100644 index 0000000..cc67b22 --- /dev/null +++ b/front_files/index.html @@ -0,0 +1,3 @@ + + +Echo: True Peer-to-Peer file and text transfers \ No newline at end of file diff --git a/front_files/index.js b/front_files/index.js new file mode 100644 index 0000000..e69de29 From 5791786a9fd38a073e3e545e6be266b12c6dbb52 Mon Sep 17 00:00:00 2001 From: Abdulhade Date: Fri, 14 Nov 2025 03:46:57 +0300 Subject: [PATCH 3/4] Created api router --- api/http_handler.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/api/http_handler.go b/api/http_handler.go index 8ea5676..6834ea9 100644 --- a/api/http_handler.go +++ b/api/http_handler.go @@ -14,10 +14,30 @@ func ping(w http.ResponseWriter, r *http.Request) { func serveStaticFile(fileName string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + // TODO: check out why does this even need the request? does it take the file path from it, + // is is possible to manipulate the path like using .. to get around?! http.ServeFile(w, r, "front_files/"+fileName) } } +func createSession(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, ` + { + "sessionId":"XyZ123" + } + `) +} + +func signalWS(w http.ResponseWriter, r *http.Request) { + // This will create the websocket for the signaling service. + vars := mux.Vars(r) + party := vars["party"] + session := vars["session"] + + fmt.Printf("Initiating a websocket, party=%s session=%s", party, session) + +} + func BuildRouter(listenAddress string) { mainRouter := mux.NewRouter() @@ -26,7 +46,9 @@ func BuildRouter(listenAddress string) { mainRouter.HandleFunc("/index.js", serveStaticFile("index.js")) mainRouter.HandleFunc("/index.css", serveStaticFile("index.css")) - // api_router := base_router.PathPrefix("/api").Subrouter() + api_router := mainRouter.PathPrefix("/api").Subrouter() + api_router.HandleFunc("/session", createSession).Methods("POST") + api_router.HandleFunc("/signal/session/{session}/party/{party}", signalWS) err := http.ListenAndServe(listenAddress, mainRouter) From 333cf981c94896ff42f826077791e69ede55730b Mon Sep 17 00:00:00 2001 From: Abdulhade Date: Fri, 14 Nov 2025 03:56:00 +0300 Subject: [PATCH 4/4] Created websocket signaler handler placeholder --- api/http_handler.go | 14 ++++++++++++-- api/ws_handler.go | 21 +++++++++++++++++++++ go.mod | 2 ++ go.sum | 2 ++ 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 api/ws_handler.go diff --git a/api/http_handler.go b/api/http_handler.go index 6834ea9..34ea7af 100644 --- a/api/http_handler.go +++ b/api/http_handler.go @@ -6,6 +6,7 @@ import ( "net/http" "github.com/gorilla/mux" + "github.com/gorilla/websocket" ) func ping(w http.ResponseWriter, r *http.Request) { @@ -28,13 +29,22 @@ func createSession(w http.ResponseWriter, r *http.Request) { `) } +var upgrader = websocket.Upgrader{} + func signalWS(w http.ResponseWriter, r *http.Request) { - // This will create the websocket for the signaling service. vars := mux.Vars(r) party := vars["party"] session := vars["session"] - fmt.Printf("Initiating a websocket, party=%s session=%s", party, session) + fmt.Printf("Initiating a websocket, party=%s session=%s\n", party, session) + + conn, err := upgrader.Upgrade(w, r, nil) + + if err != nil { + fmt.Printf("Error initiating websocket, r.URL=%s e=%s", r.URL.String(), err) + } + + websocketSignaler(conn) } diff --git a/api/ws_handler.go b/api/ws_handler.go new file mode 100644 index 0000000..e81645d --- /dev/null +++ b/api/ws_handler.go @@ -0,0 +1,21 @@ +package api + +import "github.com/gorilla/websocket" + +func websocketSignaler(conn *websocket.Conn) { + defer conn.Close() + + for { + msgType, msg, err := conn.ReadMessage() + if err != nil { + return + } + + // Let's just echo for now as a placeholder + + err = conn.WriteMessage(msgType, msg) + if err != nil { + return + } + } +} diff --git a/go.mod b/go.mod index 2113e65..a117d9a 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,5 @@ module echo go 1.22.2 require github.com/gorilla/mux v1.8.1 + +require github.com/gorilla/websocket v1.5.3 diff --git a/go.sum b/go.sum index 7128337..7ed87b7 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=