sakura_iot_go

package module
v0.0.1-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2016 License: Apache-2.0 Imports: 10 Imported by: 0

README

sakura-iot-go

さくらのIoT Platformとの連携サービス用ライブラリ

This project is still developping.

概要

sakura-iot-goは以下を提供しています。

  • さくらのIoT Platformとの連携を行うためのライブラリ(HTTPハンドラ(net/http) + ペイロード用構造体の定義)
  • HTTPハンドラのサンプル実装としてエコーサーバー

HTTPハンドラはさくらのIoT PlatformからのOutgoing Webhookを受信し、

  • ペイロードの解析
  • HMAC-SHA1でのメッセージ署名検証

を行います。

ライブラリとしての利用

net/httpライブラリでHTTPサーバーを起動する例


package main

import (
	"fmt"
	sakura "github.com/yamamoto-febc/sakura-iot-go"
	"net/http"
)

func main() {

	http.Handle("/", &sakura.WebhookHandler{
		Secret: "[put your secret]",
		HandleFunc: func(p sakura.Payload) {

			// [ここにWebhook 受信時の処理を書く]

			fmt.Printf("Module:%s\n", p.Module)
			fmt.Printf("Type  :%s\n", p.Type)
			fmt.Printf("Channels:%#v\n", p.Payload.Channels)

		},
	})
	http.ListenAndServe(":8080", nil)
}


サンプル実装(エコーサーバー) : Goビルド環境がある場合

# エコーサーバーの起動
$ go run cmd/echo_server.go

# 各種オプションの指定ありの場合
$ go run cmd/echo_server.go --port 8081 --path "/webhook" --secret "put your secret"  --debug

# ヘルプ:指定できるオプションの説明など
$ go run cmd/echo_server.go --help

サンプル実装(エコーサーバー) : Goビルド環境がない場合

リリースページにて実行ファイルを配布しています。

ダウンロードして展開、実行権を付与してください。 (以下の例ではカレントディレクトリに展開した場合のものです)

# エコーサーバーの起動
$ ./sakura-iot-echo-server

# 各種オプションの指定ありの場合
$ ./sakura-iot-echo-server --port 8081 --path "/webhook" --secret "put your secret" --debug

# ヘルプ:指定できるオプションの説明など
$ ./sakura-iot-echo-server --help

License

sakura-iot-go Copyright (C) 2016 Kazumichi Yamamoto.

This project is published under Apache 2.0 License.

Author

Documentation

Overview

Example
package main

import (
	"fmt"
	sakura "github.com/yamamoto-febc/sakura-iot-go"
	"net/http"
)

func main() {

	http.Handle("/", &sakura.WebhookHandler{
		Secret: "[put your secret]",
		HandleFunc: func(p sakura.Payload) {

			// [ここにWebhook 受信時の処理を書く]

			fmt.Printf("Module:%s\n", p.Module)
			fmt.Printf("Type  :%s\n", p.Type)
			fmt.Printf("Channels:%#v\n", p.Payload.Channels)

		},
	})
	http.ListenAndServe(":8080", nil)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	PayloadTypesKeepAlive = "keepalive"
	PayloadTypesChannels  = "channels"
)

Functions

This section is empty.

Types

type Channel

type Channel struct {
	Channel int64       `json:"channel"`
	Type    string      `json:"type"`
	Value   interface{} `json:"value"`
}

func (*Channel) GetDouble

func (c *Channel) GetDouble() (float64, error)

func (*Channel) GetFloat

func (c *Channel) GetFloat() (float32, error)

func (*Channel) GetHexString

func (c *Channel) GetHexString() (string, error)

func (*Channel) GetInt

func (c *Channel) GetInt() (int32, error)

func (*Channel) GetInt64

func (c *Channel) GetInt64() (int64, error)

func (*Channel) GetUint

func (c *Channel) GetUint() (uint32, error)

func (*Channel) GetUint64

func (c *Channel) GetUint64() (uint64, error)

type InnerPayload

type InnerPayload struct {
	Channels []Channel `json:"channels"`
}

type Payload

type Payload struct {
	Datetime time.Time    `json:"datetime"`
	Module   string       `json:"module"`
	Payload  InnerPayload `json:"payload"`
	Type     string       `json:"type"`
}

func (*Payload) IsChannelValue

func (p *Payload) IsChannelValue() bool

func (*Payload) IsKeepAlive

func (p *Payload) IsKeepAlive() bool

type WebhookHandler

type WebhookHandler struct {
	Secret     string
	HandleFunc WebhookHandlerFunc
	Debug      bool
}

func (*WebhookHandler) ServeHTTP

func (h *WebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type WebhookHandlerFunc

type WebhookHandlerFunc func(Payload)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL