backend for xcvr appview
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

update some things

+89 -17
+87 -9
server/cmd/main.go
··· 3 3 import ( 4 4 "encoding/json" 5 5 "fmt" 6 + "net" 6 7 "net/http" 8 + "os" 9 + "slices" 10 + "sync" 11 + "time" 7 12 8 13 "github.com/rachel-mp4/lrc/lrcd/pkg/lrcd" 9 14 ) 10 15 11 16 var ( 12 - channelToServer map[string]*lrcd.Server 13 - channels []channel 17 + channelsMu sync.Mutex 18 + bandToServer map[string]*lrcd.Server 19 + channels []channel 14 20 ) 15 21 16 22 func main() { 17 - channelToServer = make(map[string]*lrcd.Server) 23 + bandToServer = make(map[string]*lrcd.Server) 24 + channels = make([]channel, 0) 25 + createChannel(channel{Band: "general", Sign: "hiiiiiiiii"}, false) 26 + createChannel(channel{Band: "sneep", Sign: "snirp"}, true) 18 27 fmt.Println("hello world") 19 - http.HandleFunc("GET /xrpc/getChannels", getChannels) 20 - 28 + http.HandleFunc("GET /xrpc/getChannels", withCORS(getChannels)) 21 29 http.HandleFunc("POST /xrpc/initChannel", initChannel) 22 30 http.ListenAndServe(":8080", nil) 23 31 } ··· 33 41 type channel struct { 34 42 Band string `json:"band"` 35 43 Sign string `json:"sign"` 44 + Port int `json:"port"` 36 45 } 37 46 38 47 func initChannel(w http.ResponseWriter, r *http.Request) { ··· 42 51 if err != nil { 43 52 panic(err) 44 53 } 45 - channels = append(channels, c) 54 + createChannel(c, true) 55 + 46 56 fmt.Printf("created a channel on band: %s and call sign: %s\n", c.Band, c.Sign) 47 57 } 48 58 49 - 50 - 51 - 59 + func createChannel(c channel, withDelete bool) error { 60 + port, err := getFreePort() 61 + if err != nil { 62 + fmt.Println(err.Error()) 63 + return err 64 + } 65 + c.Port = port 52 66 67 + options := []lrcd.Option{lrcd.WithWSPort(c.Port), 68 + lrcd.WithWSPath(c.Band), 69 + lrcd.WithWelcome(c.Sign), 70 + lrcd.WithLogging(os.Stdout, true), 71 + } 72 + ec := make(chan struct{}) 73 + 74 + if withDelete { 75 + options = append(options,lrcd.WithEmptyChannel(ec)) 76 + after := 10*time.Second 77 + options = append(options, lrcd.WithEmptySignalAfter(after)) 78 + } 79 + server, err := lrcd.NewServer(options...) 53 80 81 + if err != nil { 82 + fmt.Println(err.Error()) 83 + return err 84 + } 85 + fmt.Println("created", c.Band) 54 86 87 + err = server.Start() 88 + if err != nil { 89 + fmt.Println(err.Error()) 90 + return err 91 + } 92 + fmt.Println("started", c.Band) 55 93 94 + channelsMu.Lock() 95 + defer channelsMu.Unlock() 96 + bandToServer[c.Band] = server 97 + channels = append(channels, c) 98 + if withDelete { 99 + go func() { 100 + <-ec 101 + channelsMu.Lock() 102 + idx := slices.Index(channels, c) 103 + channels = slices.Delete(channels, idx, idx+1) 104 + err = bandToServer[c.Band].Stop() 105 + if err != nil { 106 + fmt.Println(err.Error()) 107 + } 108 + delete(bandToServer, c.Band) 109 + channelsMu.Unlock() 110 + fmt.Println("deleted", c.Band) 111 + }() 112 + } 113 + return nil 114 + } 56 115 116 + func getFreePort() (int, error) { 117 + nl, err := net.Listen("tcp", ":0") 118 + if err != nil { 119 + return -1, err 120 + } 121 + defer nl.Close() 122 + return (nl.Addr().(*net.TCPAddr)).Port, nil 123 + } 57 124 125 + func withCORS(h http.HandlerFunc) http.HandlerFunc { 126 + return func(w http.ResponseWriter, r *http.Request) { 127 + w.Header().Set("Access-Control-Allow-Origin", "http://localhost:5173") 128 + w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS") 129 + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") 130 + if r.Method == "OPTIONSONS" { 131 + w.WriteHeader(http.StatusNoContent) 132 + } 133 + h(w, r) 134 + } 135 + }
+2 -2
server/go.mod
··· 2 2 3 3 go 1.22.1 4 4 5 - require github.com/rachel-mp4/lrc/lrcd v0.0.0-20250408005617-2d344a3d04f7 5 + require github.com/rachel-mp4/lrc/lrcd v0.0.0-20250410002721-ca6a18431212 6 6 7 7 require ( 8 8 github.com/gorilla/websocket v1.5.3 // indirect 9 - github.com/rachel-mp4/lrc/lrc v0.0.0-20250408005617-2d344a3d04f7 // indirect 9 + github.com/rachel-mp4/lrc/lrc v0.0.0-20250408013928-75dc71a6060f // indirect 10 10 ) 11 11 12 12 replace github.com/rachel-mp4/lrc/lrcd => ../../lrc/lrcd
-6
server/go.sum
··· 1 1 github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= 2 2 github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 3 - github.com/rachel-mp4/lrc/lrc v0.0.0-20250408005617-2d344a3d04f7 h1:tdZLEbrHSRIDinWVbD/g0CKY6XxcsoZ0nH5P3aW4LDU= 4 - github.com/rachel-mp4/lrc/lrc v0.0.0-20250408005617-2d344a3d04f7/go.mod h1:8gurhwhM0qJhpRw4jWNPiTureKGbtql33khlbS17ULc= 5 - github.com/rachel-mp4/lrc/lrcd v0.0.0-20250407004606-f56de1e09d2a h1:PCr+Uo6LLOG0C04xNM3Uxbu3Atw/Nm3EtfMzFoS3Gcc= 6 - github.com/rachel-mp4/lrc/lrcd v0.0.0-20250407004606-f56de1e09d2a/go.mod h1:V/dWrPAee/3ULiIjm6psRNfep1dKt+AZBGs6/2NvSC4= 7 - github.com/rachel-mp4/lrc/lrcd v0.0.0-20250408005617-2d344a3d04f7 h1:wHl6d6Zmh2ENdtod4Ibe3TmuKvkioURSVmFc+TIWzzU= 8 - github.com/rachel-mp4/lrc/lrcd v0.0.0-20250408005617-2d344a3d04f7/go.mod h1:V/dWrPAee/3ULiIjm6psRNfep1dKt+AZBGs6/2NvSC4=