-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
45 lines (36 loc) · 1.12 KB
/
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
43
44
45
package main
import (
"flag"
"fmt"
"html/template"
_ "image/gif"
_ "image/jpeg"
"log"
"net/http"
_ "net/http/pprof"
"github.com/gobuffalo/packr/v2"
)
//go:generate packr2
var listen = flag.String("listen", "localhost:8080", "")
var templates = packr.New("templateBox", "./assets/templates")
var static = packr.New("staticBox", "./assets/static")
func mustFile(name string) string {
bytes, _ := templates.FindString(name)
return string(bytes)
}
func render(templateName string, dat interface{}, w http.ResponseWriter, r *http.Request) {
templates := template.Must(template.New("").Parse(mustFile("main.html")))
templates, _ = templates.Parse(mustFile(fmt.Sprintf("%s.html", templateName)))
err := templates.ExecuteTemplate(w, "base", dat)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
flag.Parse() // Scan the arguments list
http.HandleFunc("/", mainHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(static)))
http.HandleFunc("/upload", uploadHandler)
log.Printf("Start at http://%s/", *listen)
log.Fatal(http.ListenAndServe(*listen, nil))
}