-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
executable file
·38 lines (34 loc) · 845 Bytes
/
main.go
File metadata and controls
executable file
·38 lines (34 loc) · 845 Bytes
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
package main
import (
"net/http"
"sync"
"html/template"
"path/filepath"
"log"
"flag"
)
// templは一つのテンプレートファイルを表す
type templateHandler struct {
once sync.Once
filename string
templ *template.Template
}
func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.once.Do(func() {
t.templ = template.Must(template.ParseFiles(filepath.Join("templates", t.filename)))
})
t.templ.Execute(w, r)
}
func main() {
var addr = flag.String("addr", ":8080", "ポート番号")
flag.Parse()
r := newRoom()
http.Handle("/", &templateHandler{filename: "chat.html"})
http.Handle("/room", r)
go r.run()
log.Println("Webサーバーを起動します ポート番号:", *addr)
if err := http.ListenAndServe(*addr, nil);
err != nil {
log.Fatal("ListenAndServe:", err)
}
}