/
HandlerApiRoomStatus.go
94 lines (69 loc) · 2.37 KB
/
HandlerApiRoomStatus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*!
Copyright © 2022 chouette.21.00@gmail.com
Released under the MIT license
https://opensource.org/licenses/mit-license.php
*/
package srhandler
import (
"html/template"
"log"
"net/http"
"strings"
"time"
"github.com/dustin/go-humanize"
yaml "gopkg.in/yaml.v2"
"github.com/Chouette2100/exsrapi"
"github.com/Chouette2100/srapi"
)
/*
ApiRoomStatus() の戻り値を表示する。
Ver. 0.1.0
*/
// "/ApiRoomStatus"に対するハンドラー
// http://localhost:8080/t009top で呼び出される
func HandlerApiRoomStatus(
w http.ResponseWriter,
r *http.Request,
) {
// cookiejarがセットされたHTTPクライアントを作る
client, jar, err := exsrapi.CreateNewClient("XXXXXX")
if err != nil {
log.Printf("CreateNewClient: %s\n", err.Error())
return
}
// すべての処理が終了したらcookiejarを保存する。
defer jar.Save()
// テンプレートで使用する関数を定義する
funcMap := template.FuncMap{
"Comma": func(i int) string { return humanize.Comma(int64(i)) }, // 3桁ごとに","を入れる関数。
"UnixTimeToYYYYMMDDHHMM": func(i int64) string { return time.Unix(int64(i), 0).Format("2006-01-02 15:04") }, // UnixTimeを時分に変換する関数。
"UnixTimeToHHMM": func(i int64) string { return time.Unix(int64(i), 0).Format("15:04") }, // UnixTimeを時分に変換する関数。
}
// テンプレートをパースする
tpl := template.Must(template.New("").Funcs(funcMap).ParseFiles("templates/apiroomstatus.gtpl", "templates/footer.gtpl"))
url := r.FormValue("room_url_key")
if url == "" {
log.Printf("room_url_key が設定されていません。URLのあとに\"?room_url_key=.....\"を追加してください。\n")
return
}
var roomstatus *srapi.RoomStatus
roomstatus, err = srapi.ApiRoomStatus(client, url)
if err != nil {
log.Printf("ApiRoomStatus(): %s\n", err.Error())
return
}
var top struct {
Roomstatus []string
}
// top.Roomstatus = fmt.Sprintf("%v", roomstatus)
data, err := yaml.Marshal(roomstatus)
if err != nil {
log.Printf(" yaml.Marshal() returned %v\n",err)
return
}
top.Roomstatus = strings.Split(string(data), "\n")
// テンプレートへのデータの埋め込みを行う
if err = tpl.ExecuteTemplate(w, "apiroomstatus.gtpl", top); err != nil {
log.Printf("tpl.ExecuteTemplate() returned error: %s\n", err.Error())
}
}