forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakeapp_test.go
111 lines (94 loc) · 2.34 KB
/
fakeapp_test.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package revel
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
)
type Hotel struct {
HotelID int
Name, Address string
City, State, Zip string
Country string
Price int
}
type Hotels struct {
*Controller
}
type Static struct {
*Controller
}
func (c Hotels) Show(id int) Result {
title := "View Hotel"
hotel := &Hotel{id, "A Hotel", "300 Main St.", "New York", "NY", "10010", "USA", 300}
return c.Render(title, hotel)
}
func (c Hotels) Book(id int) Result {
hotel := &Hotel{id, "A Hotel", "300 Main St.", "New York", "NY", "10010", "USA", 300}
return c.RenderJSON(hotel)
}
func (c Hotels) Index() Result {
return c.RenderText("Hello, World!")
}
func (c Static) Serve(prefix, path string) Result {
var basePath, dirName string
if !filepath.IsAbs(dirName) {
basePath = BasePath
}
fname := filepath.Join(basePath, prefix, path)
file, err := os.Open(fname)
if os.IsNotExist(err) {
return c.NotFound("")
} else if err != nil {
WARN.Printf("Problem opening file (%s): %s ", fname, err)
return c.NotFound("This was found but not sure why we couldn't open it.")
}
return c.RenderFile(file, "")
}
func startFakeBookingApp() {
Init("prod", "github.com/netassist-ua/revel/testdata", "")
// Disable logging.
TRACE = log.New(ioutil.Discard, "", 0)
INFO = TRACE
WARN = TRACE
ERROR = TRACE
MainTemplateLoader = NewTemplateLoader([]string{ViewsPath, filepath.Join(RevelPath, "templates")})
if err := MainTemplateLoader.Refresh(); err != nil {
ERROR.Fatal(err)
}
RegisterController((*Hotels)(nil),
[]*MethodType{
{
Name: "Index",
},
{
Name: "Show",
Args: []*MethodArg{
{"id", reflect.TypeOf((*int)(nil))},
},
RenderArgNames: map[int][]string{34: {"title", "hotel"}},
},
{
Name: "Book",
Args: []*MethodArg{
{"id", reflect.TypeOf((*int)(nil))},
},
},
})
RegisterController((*Static)(nil),
[]*MethodType{
{
Name: "Serve",
Args: []*MethodArg{
{Name: "prefix", Type: reflect.TypeOf((*string)(nil))},
{Name: "filepath", Type: reflect.TypeOf((*string)(nil))},
},
RenderArgNames: map[int][]string{},
},
})
runStartupHooks()
}