Skip to content

Commit

Permalink
improve webserver, play with webtransport
Browse files Browse the repository at this point in the history
  • Loading branch information
benleb committed Apr 25, 2023
1 parent b64b986 commit 7bfe9b5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
32 changes: 30 additions & 2 deletions internal/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@ package web
//

import (
"fmt"
"html/template"
"log"
"net/http"

"github.com/benleb/gloomberg/internal"
"github.com/benleb/gloomberg/internal/gbl"
"github.com/benleb/gloomberg/internal/nemo/totra"
"github.com/charmbracelet/log"
"github.com/quic-go/quic-go/http3"
"github.com/quic-go/webtransport-go"
)

func StartWebUI(queueWsOutTokenTransactions chan *totra.TokenTransaction) {
// Create a Manager instance used to handle WebSocket Connections
hub := NewHub(queueWsOutTokenTransactions)

listenOn := ":8080"
certPath := "./home.benleb.de.crt"
keyPath := "./home.benleb.de.key"

// load index template
// tmpl := template.Must(template.ParseFiles("www/index.html"))

Expand All @@ -31,6 +38,27 @@ func StartWebUI(queueWsOutTokenTransactions chan *totra.TokenTransaction) {
gbl.Log.Error(err)
}

go func() {
// create a new webtransport.Server, listening on (UDP) port 443 (8080)
s := webtransport.Server{H3: http3.Server{Addr: listenOn}}

// Create a new HTTP endpoint /webtransport.
http.HandleFunc("/webtransport", func(w http.ResponseWriter, r *http.Request) {
conn, err := s.Upgrade(w, r)
if err != nil {
log.Printf("upgrading failed: %s", err)
w.WriteHeader(http.StatusInternalServerError)

return
}

// Handle the connection. Here goes the application logic.
log.Print(fmt.Sprintf("new connection from %s | %+v", conn.RemoteAddr(), conn))
})

log.Fatal(s.ListenAndServeTLS(certPath, keyPath))
}()

http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
data := map[string]string{
"Title": "gloomberg | " + internal.GloombergVersion,
Expand All @@ -47,5 +75,5 @@ func StartWebUI(queueWsOutTokenTransactions chan *totra.TokenTransaction) {
http.HandleFunc("/ws", hub.serveWS)

// Serve on port :8080, fudge yeah hardcoded port
log.Fatal(http.ListenAndServeTLS(":8080", "./home.benleb.de.crt", "./home.benleb.de.key", nil))
log.Fatal(http.ListenAndServeTLS(listenOn, certPath, keyPath, nil))
}
13 changes: 13 additions & 0 deletions www/javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@
// document.getElementById("chatroom-selection").onsubmit = changeChatRoom;
// document.getElementById("chatroom-message").onsubmit = sendMessage;

const transport = new WebTransport("https://" + document.location.host + "/webtransport", {

});

// Optionally, set up functions to respond to
// the connection closing:
transport.closed.then(() => {
console.log(`The HTTP/3 connection to ${url} closed gracefully.`);
}).catch((error) => {
console.error(`The HTTP/3 connection to ${url} closed due to ${error}.`);
});


// check websockets support
if (window["WebSocket"]) {
var url = "wss://" + document.location.host + "/ws";
Expand Down

0 comments on commit 7bfe9b5

Please sign in to comment.