/
main.go
67 lines (53 loc) · 1.68 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"time"
"github.com/pbberlin/struc2frm"
)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
log.SetFlags(log.Lshortfile | log.Ltime)
pfx := "struc2frm"
mux1 := http.NewServeMux() // base router
mux1.HandleFunc("/", struc2frm.FormH)
if pfx != "" {
mux1.HandleFunc("/"+pfx, struc2frm.FormH)
mux1.HandleFunc("/"+pfx+"/", struc2frm.FormH)
}
mux1.HandleFunc("/file-upload", struc2frm.FileUploadH)
if pfx != "" {
mux1.HandleFunc("/"+pfx+"/file-upload", struc2frm.FileUploadH)
mux1.HandleFunc("/"+pfx+"/file-upload/", struc2frm.FileUploadH)
}
mux1.HandleFunc("/card", struc2frm.CardH)
if pfx != "" {
mux1.HandleFunc("/"+pfx+"/card", struc2frm.CardH)
mux1.HandleFunc("/"+pfx+"/card/", struc2frm.CardH)
}
mux4 := http.NewServeMux() // top router for non-middlewared handlers
mux4.Handle("/", mux1)
serveIcon := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/x-icon")
// w.Header().Set("Cache-Control", fmt.Sprintf("public,max-age=%d", 60*60*24))
fv := "favicon.ico"
bts, _ := ioutil.ReadFile("./static/" + fv)
fmt.Fprint(w, bts)
// log.Printf("%v bytes written", len(bts))
}
mux4.HandleFunc("favicon.ico", serveIcon)
mux4.HandleFunc("/favicon.ico", serveIcon)
if pfx != "" {
mux1.HandleFunc("/"+pfx+"/favicon.ico", serveIcon)
mux1.HandleFunc("/"+pfx+"/favicon.ico/", serveIcon)
}
IPPort := fmt.Sprintf("%v:%v", "localhost", "8085")
log.Printf("starting http server at %v ... ", IPPort)
log.Printf("==========================")
log.Printf(" ")
log.Fatal(http.ListenAndServe(IPPort, mux4))
}