github.com/pion/webrtc/v3@v3.2.24/examples/internal/signal/http.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package signal
     5  
     6  import (
     7  	"flag"
     8  	"fmt"
     9  	"io/ioutil"
    10  	"net/http"
    11  	"strconv"
    12  )
    13  
    14  // HTTPSDPServer starts a HTTP Server that consumes SDPs
    15  func HTTPSDPServer() chan string {
    16  	port := flag.Int("port", 8080, "http server port")
    17  	flag.Parse()
    18  
    19  	sdpChan := make(chan string)
    20  	http.HandleFunc("/sdp", func(w http.ResponseWriter, r *http.Request) {
    21  		body, _ := ioutil.ReadAll(r.Body)
    22  		fmt.Fprintf(w, "done")
    23  		sdpChan <- string(body)
    24  	})
    25  
    26  	go func() {
    27  		// nolint: gosec
    28  		err := http.ListenAndServe(":"+strconv.Itoa(*port), nil)
    29  		if err != nil {
    30  			panic(err)
    31  		}
    32  	}()
    33  
    34  	return sdpChan
    35  }