質問編集履歴

1

コードの追加

2018/09/09 12:00

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,137 @@
2
2
 
3
3
 
4
4
 
5
+ ※コードを追記しました
5
6
 
7
+ ```ここに言語を入力
8
+
9
+ package main
10
+
11
+
12
+
13
+ import (
14
+
15
+ "flag"
16
+
17
+ "log"
18
+
19
+ "net/http"
20
+
21
+ "path/filepath"
22
+
23
+ "sync"
24
+
25
+ "text/template"
26
+
27
+
28
+
29
+ "github.com/stretchr/gomniauth"
30
+
31
+ "github.com/stretchr/gomniauth/providers/facebook"
32
+
33
+ "github.com/stretchr/gomniauth/providers/github"
34
+
35
+ "github.com/stretchr/gomniauth/providers/google"
36
+
37
+ "github.com/stretchr/objx"
38
+
39
+ )
40
+
41
+
42
+
43
+ //templは1つのテンプレートを表す
44
+
45
+ type templateHandler struct {
46
+
47
+ once sync.Once
48
+
49
+ filename string
50
+
51
+ templ *template.Template
52
+
53
+ }
54
+
55
+
56
+
57
+ //ServeHTTPはHTTPリクエストを処理する
58
+
59
+ func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
60
+
61
+ t.once.Do(func() {
62
+
63
+ t.templ =
64
+
65
+ template.Must(template.ParseFiles(filepath.Join("templates",
66
+
67
+ t.filename)))
68
+
69
+ })
70
+
71
+ data := map[string]interface{}{
72
+
73
+ "Host": r.Host,
74
+
75
+ }
76
+
77
+ if authCookie, err := r.Cookie("auth"); err == nil {
78
+
79
+ data["UserData"] = objx.MustFromBase64(authCookie.Value)
80
+
81
+ }
82
+
83
+ t.templ.Execute(w, data)
84
+
85
+ }
86
+
87
+
88
+
89
+ func main() {
90
+
91
+ var addr = flag.String("addr", ":8080", "アプリケーションのアドレス")
92
+
93
+ flag.Parse() //フラグを解釈します
94
+
95
+ //Gomniauthのセットアップ
96
+
97
+ gomniauth.SetSecurityKey("セキュリティキー")
98
+
99
+ gomniauth.WithProviders(
100
+
101
+ facebook.New("", "", "http://localhost:8080/auth/callback/facebook"),
102
+
103
+ github.New("", "", "http://localhost:8080/auth/callback/github"),
104
+
105
+ google.New("526115198658-gvoqprgin282r5ipkiiprii3pjqqi7vu.apps.googleusercontent.com", "tXvtbh5236VAlVZhonJaJMgE", "http://localhost:8080/auth/callback/google"),
106
+
107
+ )
108
+
109
+ r := newRoom()
110
+
111
+ http.Handle("/chat", MustAuth(&templateHandler{filename: "chat.html"}))
112
+
113
+ http.Handle("/login", &templateHandler{filename: "login.html"})
114
+
115
+ http.HandleFunc("/auth/", loginHandler)
116
+
117
+ http.Handle("/room", r)
118
+
119
+ //チャットルームを開始します
120
+
121
+ go r.run()
122
+
123
+ //webサーバーを起動します
124
+
125
+ log.Println("Webサーバーを開始します。ポート:", *addr)
126
+
127
+ if err := http.ListenAndServe(":8080", nil); err != nil {
128
+
129
+ log.Fatal("ListenAndServe:", err)
130
+
131
+ }
132
+
133
+ }
134
+
135
+ ```
6
136
 
7
137
  打ち込むコマンド
8
138