Merge branch 'ft/main_router'
This commit is contained in:
68
api/http_handler.go
Normal file
68
api/http_handler.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
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) {
|
||||||
|
// 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"
|
||||||
|
}
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
var upgrader = websocket.Upgrader{}
|
||||||
|
|
||||||
|
func signalWS(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
party := vars["party"]
|
||||||
|
session := vars["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)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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 := 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)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error running the web server %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
21
api/ws_handler.go
Normal file
21
api/ws_handler.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
0
front_files/index.css
Normal file
0
front_files/index.css
Normal file
3
front_files/index.html
Normal file
3
front_files/index.html
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
|
||||||
|
<title>Echo: True Peer-to-Peer file and text transfers</title>
|
||||||
0
front_files/index.js
Normal file
0
front_files/index.js
Normal file
4
go.mod
4
go.mod
@@ -1,3 +1,7 @@
|
|||||||
module echo
|
module echo
|
||||||
|
|
||||||
go 1.22.2
|
go 1.22.2
|
||||||
|
|
||||||
|
require github.com/gorilla/mux v1.8.1
|
||||||
|
|
||||||
|
require github.com/gorilla/websocket v1.5.3
|
||||||
|
|||||||
4
go.sum
Normal file
4
go.sum
Normal file
@@ -0,0 +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=
|
||||||
Reference in New Issue
Block a user