-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
36 lines (29 loc) · 861 Bytes
/
server.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
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net"
"net/http"
)
var index *template.Template
func serveIndex(w http.ResponseWriter, r *http.Request) {
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
log.Println(fmt.Sprintf("%s - %s %s - %s", ip, r.Method, r.URL.Path, r.UserAgent()))
w.WriteHeader(200)
index.Execute(w, nil)
}
func serveRobots(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("User-agent: *\nAllow: /"))
}
func main() {
templateStr, _ := ioutil.ReadFile("index.html")
index, _ = template.New("index").Parse(string(templateStr))
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/", serveIndex)
http.HandleFunc("/favicon.ico", http.NotFound)
http.HandleFunc("/robots.txt", serveRobots)
log.Fatal(http.ListenAndServe(":80", nil))
}