-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
55 lines (46 loc) · 1.03 KB
/
controller.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
package daxweb
import (
"fmt"
"github.com/danuxguin/daxweb/common"
"html/template"
"net/http"
"reflect"
"strings"
)
type Controller struct {
Data map[interface{}]interface{}
Tpl string
Ctx
}
func (this *Controller) Init(c interface{}, w http.ResponseWriter, r *http.Request) {
this.Data = make(map[interface{}]interface{})
this.Ctx.W = w
this.Ctx.R = r
defer func() {
if err := recover(); err != nil {
fmt.Printf("panic: %v\n", err)
http.NotFound(w, r)
}
}()
urlList := common.GetUrlSplit(r.URL.Path)
control := reflect.ValueOf(c)
if len(urlList) == 1 {
if r.Method == "GET" {
control.MethodByName("Get").Call(nil)
} else if r.Method == "POST" {
control.MethodByName("Post").Call(nil)
}
} else {
preurl := urlList[1]
urlFunc := strings.ToUpper(preurl[:1]) + preurl[1:]
control.MethodByName(urlFunc).Call(nil)
}
}
func (this *Controller) ExecuteTpl() {
t, err := template.ParseFiles(this.Tpl)
if err != nil {
http.NotFound(this.Ctx.W, this.Ctx.R)
return
}
t.Execute(this.Ctx.W, this.Data)
}