-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: 1998-felix <[email protected]>
- Loading branch information
1 parent
55594f0
commit 0089ebe
Showing
4 changed files
with
90 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) Abstract Machines | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
coap "github.com/plgd-dev/go-coap/v3" | ||
"github.com/plgd-dev/go-coap/v3/message" | ||
"github.com/plgd-dev/go-coap/v3/message/codes" | ||
"github.com/plgd-dev/go-coap/v3/mux" | ||
) | ||
|
||
const defaultPort = "5683" | ||
|
||
func handleRequest(w mux.ResponseWriter, r *mux.Message) { | ||
resp := w.Conn().AcquireMessage(r.Context()) | ||
defer w.Conn().ReleaseMessage(resp) | ||
resp.SetCode(codes.Content) | ||
resp.SetToken(r.Token()) | ||
resp.SetContentFormat(message.TextPlain) | ||
resp.SetBody(strings.NewReader(fmt.Sprintf("%v OK", r.Code()))) | ||
err := w.Conn().WriteMessage(resp) | ||
if err != nil { | ||
log.Printf("Cannot send response: %v", err) | ||
} | ||
} | ||
|
||
func main() { | ||
r := mux.NewRouter() | ||
r.DefaultHandle(mux.HandlerFunc(handleRequest)) | ||
log.Println("starting coap server, listening on port " + defaultPort) | ||
log.Fatal(coap.ListenAndServe("udp", ":"+defaultPort, r)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters