forked from remyoudompheng/go-misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webtoys.go
89 lines (77 loc) · 1.84 KB
/
webtoys.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
// Copyright 2012 Rémy Oudompheng. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"github.com/remyoudompheng/go-misc/pastehere"
"github.com/remyoudompheng/go-misc/webclock"
"github.com/remyoudompheng/go-misc/weblibs"
"github.com/remyoudompheng/go-misc/webtoys/irc"
_ "github.com/remyoudompheng/go-misc/webtoys/vdeck"
)
func init() {
log.SetPrefix("webtoys ")
log.SetFlags(log.LstdFlags)
pastehere.Register(nil) // at /pastehere/
webclock.Register(nil) // at /webclock/
irc.Register(nil) // at /irc/
err := weblibs.RegisterAll(nil)
if err != nil {
panic(err)
}
}
var toys = []string{
"irc",
"pastehere",
"vdeck",
"webclock",
}
const indexTplString = `<!DOCTYPE html>
<html>
<head>
<title>Rémy's Webtoys</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Rémy's Webtoys</h1>
<ul>
{{ range $ }}
<li><a href="/{{ . }}">{{ . }}</a></li>
{{ end }}
</ul>
</body>
</html>
`
var indexTpl = template.Must(template.New("index").Parse(indexTplString))
func index(resp http.ResponseWriter, req *http.Request) {
log.Printf("GET %s from %s", req.URL, req.RemoteAddr)
if req.URL.Path != "/" {
resp.WriteHeader(http.StatusNotFound)
fmt.Fprintf(resp, "No such page: %s", req.URL.Path)
return
}
indexTpl.Execute(resp, toys)
}
func init() {
http.HandleFunc("/", index)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
}
func main() {
var address string
flag.StringVar(&address, "http", ":8080", "listen address")
flag.Parse()
if address == "" {
flag.Usage()
return
}
log.Printf("start listening at %s", address)
err := http.ListenAndServe(address, nil)
if err != nil {
log.Fatal(err)
}
}