backend for xcvr appview
2
fork

Configure Feed

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

add errors for initChannel endpoint

+69 -25
+69 -25
server/cmd/main.go
··· 9 9 "slices" 10 10 "sync" 11 11 "time" 12 + "unicode/utf8" 12 13 13 14 "github.com/rachel-mp4/lrc/lrcd/pkg/lrcd" 14 15 ) ··· 19 20 channels []channel 20 21 ) 21 22 23 + type channel struct { 24 + Band string `json:"band"` 25 + Sign string `json:"sign"` 26 + Port int `json:"port"` 27 + } 28 + 22 29 func main() { 23 30 bandToServer = make(map[string]*lrcd.Server) 24 31 channels = make([]channel, 0) 25 - createChannel(channel{Band: "general", Sign: "hiiiiiiiii"}, false) 32 + createChannel(channel{Band: "general", Sign: "this is the general channel"}, false) 26 33 createChannel(channel{Band: "sneep", Sign: "snirp"}, true) 27 34 fmt.Println("hello world") 28 35 http.HandleFunc("GET /xrpc/getChannels", withCORS(getChannels)) ··· 30 37 http.ListenAndServe(":8080", nil) 31 38 } 32 39 33 - func getChannels(w http.ResponseWriter, r *http.Request) { 34 - encoder := json.NewEncoder(w) 35 - err := encoder.Encode(channels) 40 + func initChannel(w http.ResponseWriter, r *http.Request) { 41 + decoder := json.NewDecoder(r.Body) 42 + var c channel 43 + err := decoder.Decode(&c) 36 44 if err != nil { 37 - panic(err) 45 + http.Error(w, "invalid json", http.StatusBadRequest) 46 + } 47 + switch isValidInit(c) { 48 + case ieOK: 49 + c, err = createChannel(c, true) 50 + case ieNoBand: 51 + http.Error(w, "must give a band", http.StatusBadRequest) 52 + case ieLongBand: 53 + http.Error(w, "band must be shorter than 32 bytes", http.StatusBadRequest) 54 + case ieCollision: 55 + http.Error(w, "band must be unique", http.StatusBadRequest) 56 + case ieLongSign: 57 + http.Error(w, "sign must be shorter than 51 code points", http.StatusBadRequest) 38 58 } 59 + fmt.Printf("created a channel on band: %s and call sign: %s\n", c.Band, c.Sign) 60 + encoder := json.NewEncoder(w) 61 + encoder.Encode(c) 39 62 } 40 63 41 - type channel struct { 42 - Band string `json:"band"` 43 - Sign string `json:"sign"` 44 - Port int `json:"port"` 64 + type initError = int 65 + 66 + const ( 67 + ieOK initError = iota 68 + ieNoBand 69 + ieLongBand 70 + ieCollision 71 + ieLongSign 72 + iePort 73 + ) 74 + 75 + func isValidInit(c channel) initError { 76 + if c.Band == "" { 77 + return ieNoBand 78 + } 79 + if len(c.Band) > 31 { 80 + return ieLongBand 81 + } 82 + _, ok := bandToServer[c.Band] 83 + if ok { 84 + return ieCollision 85 + } 86 + if utf8.RuneCountInString(c.Sign) > 50 { 87 + return ieLongSign 88 + } 89 + if c.Port != 0 { 90 + return iePort 91 + } 92 + return ieOK 45 93 } 46 94 47 - func initChannel(w http.ResponseWriter, r *http.Request) { 48 - decoder := json.NewDecoder(r.Body) 49 - var c channel 50 - err := decoder.Decode(&c) 95 + func getChannels(w http.ResponseWriter, r *http.Request) { 96 + encoder := json.NewEncoder(w) 97 + err := encoder.Encode(channels) 51 98 if err != nil { 52 99 panic(err) 53 100 } 54 - createChannel(c, true) 55 - 56 - fmt.Printf("created a channel on band: %s and call sign: %s\n", c.Band, c.Sign) 57 101 } 58 102 59 - func createChannel(c channel, withDelete bool) error { 103 + func createChannel(c channel, withDelete bool) (channel, error) { 60 104 port, err := getFreePort() 61 105 if err != nil { 62 106 fmt.Println(err.Error()) 63 - return err 107 + return channel{}, err 64 108 } 65 109 c.Port = port 66 110 67 - options := []lrcd.Option{lrcd.WithWSPort(c.Port), 111 + options := []lrcd.Option{lrcd.WithWSPort(c.Port), 68 112 lrcd.WithWSPath(c.Band), 69 113 lrcd.WithWelcome(c.Sign), 70 114 lrcd.WithLogging(os.Stdout, true), 71 115 } 72 116 ec := make(chan struct{}) 73 - 117 + 74 118 if withDelete { 75 - options = append(options,lrcd.WithEmptyChannel(ec)) 76 - after := 10*time.Second 119 + options = append(options, lrcd.WithEmptyChannel(ec)) 120 + after := 10 * time.Second 77 121 options = append(options, lrcd.WithEmptySignalAfter(after)) 78 122 } 79 123 server, err := lrcd.NewServer(options...) 80 124 81 125 if err != nil { 82 126 fmt.Println(err.Error()) 83 - return err 127 + return channel{}, err 84 128 } 85 129 fmt.Println("created", c.Band) 86 130 87 131 err = server.Start() 88 132 if err != nil { 89 133 fmt.Println(err.Error()) 90 - return err 134 + return channel{}, err 91 135 } 92 136 fmt.Println("started", c.Band) 93 137 ··· 110 154 fmt.Println("deleted", c.Band) 111 155 }() 112 156 } 113 - return nil 157 + return c, nil 114 158 } 115 159 116 160 func getFreePort() (int, error) {