-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
51 lines (44 loc) · 1.2 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package methuforecasttimelapse
import (
"fmt"
"html/template"
"log"
"net/http"
)
func gifHandler(w http.ResponseWriter, r *http.Request) {
log.Println(*r)
w.Header().Set("content-type", "image/gif")
http.ServeFile(w, r, "gifs/anim.gif")
}
func galleryHandler(w http.ResponseWriter, r *http.Request) {
log.Println(*r)
w.Header().Set("content-type", "text/html")
t, err := template.ParseFiles("templates/gallery.template")
if err != nil {
log.Println(err)
return
}
images, err := ListImageFiles("images")
t.Execute(w, images)
}
func initHandlers() {
staticServer := http.FileServer(http.Dir("static"))
imageServer := http.FileServer(http.Dir("images"))
http.HandleFunc("/gif", gifHandler)
http.HandleFunc("/gallery", galleryHandler)
http.Handle("/images/", http.StripPrefix("/images/", imageServer))
http.Handle("/", staticServer)
}
func StartServer(localAddress string, port int) {
initHandlers()
serverAddress := fmt.Sprintf("%v:%v", localAddress, port)
log.Println("Listening on", serverAddress)
err := http.ListenAndServe(serverAddress, nil)
if err != nil {
log.Println("Problem with the server start", err)
}
}
// For the Google App Engine initialization
func init() {
initHandlers()
}