forked from immoren/hellotoall
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.go
43 lines (35 loc) · 844 Bytes
/
main.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
// Copyright 2010 The Go Authors. 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 (
"html/template"
"net/http"
"os"
"time"
)
type Page struct {
Hostname string
Year string
}
func loadPage() (*Page, error) {
t := time.Now()
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
return &Page{Hostname: hostname, Year: t.Format("2006") }, nil
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
p, _ := loadPage()
t, _ := template.ParseFiles("index.tpl")
t.Execute(w, p)
}
func resourceHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:])
}
func main() {
http.HandleFunc("/public/", resourceHandler)
http.HandleFunc("/", viewHandler)
http.ListenAndServe(":8080", nil)
}