Files
dandelion/main.go
bobbydigitales f869d3f111 update TODOs
2024-03-10 15:50:01 -07:00

141 lines
4.7 KiB
Go

// TODO
// 1. Convery network messages to flatbuffers so we don't need to parse JSON in Go.
// Use binary WS messages. Generate for Go and JS and use that everywhere. We can
// also use this for WebRTC messages.
// 2. Keep a list of all nodes that bootstrapped in the last N minutes.
// 3. When a node bootstraps, send it a random list of the nodes we know about.
// 4. Do the signalling to connect those nodes.
// 5. Each node will know about N other nodes. To find get the data for a feed,
// a node will need to find another node that has that data. To do that we'll need to
// implement a search message that is sent to all currently connected nodes, and they
// forward to all their nodes, passing back the address of the node that has the data.
// Once we find it, we'll do the signalling to connect to it via Web RTC via our existing connected nodes.
// -----
// Feeds. People can curate feeds which can be any combination of hashtags, serch terms and users.
// Invite-only communities. Just block everyone else even if they post to it.
// Limit to friends and friends of friends
// MVP
// You connect to the person you want to get the post from to get the post
// they give you the post
// If they're offline, you can't get their updates.
// This is very stupid, but it's simplest thing.
// The bootstrap server connects you to them directly via WebRTC
// This will make the thing actually function as a little toy for people to play with.
// This will let us test whether background tabs respond to webrtc requests.
// THEN
// Need to have identity sorted out
// When you read someone's posts, you also cache them locally
// cache priority goes mutuals->people you follow->people who you folllow, follow, so you're always
// caching your mutual's posts
// Posts are samll, so caching per-post will work fine.
// Then the process is for the bootstrap server to remember all nodes and what they're caching
// This will allow distributed content delivery but put a memory and bendwidth strain on the
// bootstrap sever. Look into Web Transport for the raspberry pi overhead. Could buy a few more RPIs
// and make a little cluster
// Domain name so we can get a certificate and serve HTTPS / HTTP3
// Think about compiling Typescript on initial access and caching the JS in a service worker
// so you don't need a build system to change things.
// Think about self-hosting the client so the system can be completely self-hosted
package main
import (
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true // Note: In production, you'd want to check the origin.
},
}
// handleWebSocket handles WebSocket requests from the peer.
func handleWebSocket(w http.ResponseWriter, r *http.Request) {
log.Println("Websocket connection!", r.RemoteAddr)
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println("Upgrade error:", err)
return
}
defer conn.Close()
for {
mt, message, err := conn.ReadMessage()
if err != nil {
log.Println("Read error:", err)
break
}
log.Printf("recv: %s", message)
err = conn.WriteMessage(mt, message)
if err != nil {
log.Println("Write error:", err)
break
}
}
}
// LoggingHandler logs requests and delegates them to the underlying handler.
type LoggingHandler struct {
handler http.Handler
}
func (lh *LoggingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("Serving file: %s", r.URL.Path)
lh.handler.ServeHTTP(w, r)
}
// noDirListing wraps an http.FileServer handler to prevent directory listings
func noDirListing(h http.Handler, root string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Serve index.html when root is requested
if r.URL.Path == "/" {
http.ServeFile(w, r, filepath.Join(root, "index.html"))
return
}
// Check if the path is a directory
path := filepath.Join(root, r.URL.Path)
if info, err := os.Stat(path); err == nil && info.IsDir() {
http.NotFound(w, r) // Return 404 for directories other than root
return
}
h.ServeHTTP(w, r)
}
}
func main() {
dir := "./"
port := 6789
addr := ":" + strconv.Itoa(port)
log.Printf("Starting server on %s", addr)
// Set up file server and WebSocket endpoint
fs := http.FileServer(http.Dir(dir))
// loggingHandler := &LoggingHandler{handler: fs}
// http.Handle("/", loggingHandler)
http.Handle("/", noDirListing(fs, dir))
http.HandleFunc("/ws", handleWebSocket)
// Configure and start the HTTP server
server := &http.Server{
Addr: addr,
Handler: nil, // nil uses the default ServeMux, which we configured above
}
log.Printf("Server is configured and serving on port %d...", port)
log.Fatal(server.ListenAndServe())
}