Skip to content

Commit

Permalink
Add support for coap
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavosbarreto committed Oct 18, 2018
1 parent 1c56a89 commit 31152a0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 19 deletions.
32 changes: 32 additions & 0 deletions coap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"net"
"time"

"github.com/OSSystems/crosscoap"
)

func startCoapServer(coapPort, httpPort int) error {
udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("0.0.0.0:%d", coapPort))
if err != nil {
return err
}

udpListener, err := net.ListenUDP("udp", udpAddr)
if err != nil {
return err
}
defer udpListener.Close()

timeout := time.Second * 10

p := crosscoap.Proxy{
Listener: udpListener,
BackendURL: fmt.Sprintf("http://127.0.0.1:%d/", httpPort),
Timeout: &timeout,
}

return p.Serve()
}
40 changes: 22 additions & 18 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ import:
- installmodes/ubifs
- libarchive
- metadata
- package: github.com/OSSystems/crosscoap
11 changes: 10 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func main() {
rootCmd.PersistentFlags().StringP("password", "", "admin", "Admin password")
rootCmd.PersistentFlags().IntP("port", "", 8080, "Port")
rootCmd.PersistentFlags().StringP("dir", "", "./", "Packages storage dir")
rootCmd.PersistentFlags().IntP("coap", "", 5683, "Coap server listen port")

viper.BindPFlag("db", rootCmd.PersistentFlags().Lookup("db"))
viper.BindPFlag("username", rootCmd.PersistentFlags().Lookup("username"))
Expand Down Expand Up @@ -168,5 +169,13 @@ func execute(cmd *cobra.Command, args []string) {
e.GET("/ui", handler)
}

log.Fatal(e.Start(fmt.Sprintf(":%d", viper.GetInt("port"))))
go func() {
log.Fatal(e.Start(fmt.Sprintf(":%d", viper.GetInt("port"))))
}()

go func() {
log.Fatal(startCoapServer(viper.GetInt("coap"), viper.GetInt("port")))
}()

select {}
}

0 comments on commit 31152a0

Please sign in to comment.