-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
64 lines (55 loc) · 1.41 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
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"html/template"
"log"
"net/http"
"time"
)
type Page struct {
ContractNum string
ContractVer string
ContractType string
ContractTypeCN string
ContractBranch string
ContractBranchCN string
UrlParameters string
TimeNow string
NumOfAccess int
PageOffsetConfig template.HTML
}
func printHandler(w http.ResponseWriter, r *http.Request) {
log.Println("printHandler -> request received from", r.URL.Path)
path := r.URL.Path
path = path[len("/print/"):]
log.Println("printHandler -> path:", path)
if len(path) == 0 {
t, err := template.ParseFiles("./static/html_templates/print_index.html")
if err != nil {
log.Println("printHandler ->", err)
}
p := &Page{
TimeNow: time.Now().Format("2006-01-02 15:04"),
}
t.Execute(w, p)
return
}
const TEMPLATE_URL string = "./static/html_templates/print_main_template.html"
t, err := template.ParseFiles(TEMPLATE_URL)
if err != nil {
log.Println("printHandler ->", err)
}
p := &Page{
ContractNum: path,
}
t.Execute(w, p)
log.Println("printHandler -> response created")
// Debug the output:
//var buf bytes.Buffer
//t.Execute(&buf, p)
//log.Println("printHandler -> response created:", buf.String())
}
func main() {
http.HandleFunc("/print/", printHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
log.Fatal(http.ListenAndServe(":1084", nil))
}