Add API Framework Revel Source Files
[iec.git] / src / foundation / api / revel / fakeapp_test.go
1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
4
5 package revel
6
7 import (
8         "os"
9         "path/filepath"
10         "reflect"
11 )
12
13 type Hotel struct {
14         HotelID          int
15         Name, Address    string
16         City, State, Zip string
17         Country          string
18         Price            int
19 }
20
21 type Hotels struct {
22         *Controller
23 }
24
25 type Static struct {
26         *Controller
27 }
28
29 type Implicit struct {
30         *Controller
31 }
32
33 type Application struct {
34         *Controller
35 }
36
37 func (c Hotels) Show(id int) Result {
38         title := "View Hotel"
39         hotel := &Hotel{id, "A Hotel", "300 Main St.", "New York", "NY", "10010", "USA", 300}
40         // The line number below must match the one with the code : RenderArgNames: map[int][]string{43: {"title", "hotel"}},
41         return c.Render(title, hotel)
42 }
43
44 func (c Hotels) Book(id int) Result {
45         hotel := &Hotel{id, "A Hotel", "300 Main St.", "New York", "NY", "10010", "USA", 300}
46         return c.RenderJSON(hotel)
47 }
48
49 func (c Hotels) Index() Result {
50         return c.RenderText("Hello, World!")
51 }
52
53 func (c Static) Serve(prefix, path string) Result {
54         var basePath, dirName string
55
56         if !filepath.IsAbs(dirName) {
57                 basePath = BasePath
58         }
59
60         fname := filepath.Join(basePath, prefix, path)
61         file, err := os.Open(fname)
62         if os.IsNotExist(err) {
63                 return c.NotFound("")
64         } else if err != nil {
65                 RevelLog.Errorf("Problem opening file (%s): %s ", fname, err)
66                 return c.NotFound("This was found but not sure why we couldn't open it.")
67         }
68         return c.RenderFile(file, "")
69 }
70
71 // Register controllers is in its own function so the route test can use it as well
72 func registerControllers() {
73         controllers = make(map[string]*ControllerType)
74         RaiseEvent(ROUTE_REFRESH_REQUESTED, nil)
75         RegisterController((*Hotels)(nil),
76                 []*MethodType{
77                         {
78                                 Name: "Index",
79                         },
80                         {
81                                 Name: "Show",
82                                 Args: []*MethodArg{
83                                         {"id", reflect.TypeOf((*int)(nil))},
84                                 },
85                                 RenderArgNames: map[int][]string{41: {"title", "hotel"}},
86                         },
87                         {
88                                 Name: "Book",
89                                 Args: []*MethodArg{
90                                         {"id", reflect.TypeOf((*int)(nil))},
91                                 },
92                         },
93                 })
94
95         RegisterController((*Static)(nil),
96                 []*MethodType{
97                         {
98                                 Name: "Serve",
99                                 Args: []*MethodArg{
100                                         {Name: "prefix", Type: reflect.TypeOf((*string)(nil))},
101                                         {Name: "filepath", Type: reflect.TypeOf((*string)(nil))},
102                                 },
103                                 RenderArgNames: map[int][]string{},
104                         },
105                 })
106         RegisterController((*Implicit)(nil),
107                 []*MethodType{
108                         {
109                                 Name: "Implicit",
110                                 Args: []*MethodArg{
111                                         {Name: "prefix", Type: reflect.TypeOf((*string)(nil))},
112                                         {Name: "filepath", Type: reflect.TypeOf((*string)(nil))},
113                                 },
114                                 RenderArgNames: map[int][]string{},
115                         },
116                 })
117         RegisterController((*Application)(nil),
118                 []*MethodType{
119                         {
120                                 Name: "Application",
121                                 Args: []*MethodArg{
122                                         {Name: "prefix", Type: reflect.TypeOf((*string)(nil))},
123                                         {Name: "filepath", Type: reflect.TypeOf((*string)(nil))},
124                                 },
125                                 RenderArgNames: map[int][]string{},
126                         },
127                         {
128                                 Name: "Index",
129                                 Args: []*MethodArg{
130                                         {Name: "foo", Type: reflect.TypeOf((*string)(nil))},
131                                         {Name: "bar", Type: reflect.TypeOf((*string)(nil))},
132                                 },
133                                 RenderArgNames: map[int][]string{},
134                         },
135                 })
136 }
137 func startFakeBookingApp() {
138         Init("prod", "github.com/revel/revel/testdata", "")
139
140         MainTemplateLoader = NewTemplateLoader([]string{ViewsPath, filepath.Join(RevelPath, "templates")})
141         if err := MainTemplateLoader.Refresh(); err != nil {
142                 RevelLog.Fatal("Template error","error",err)
143         }
144
145         registerControllers()
146
147         InitServerEngine(9000, GO_NATIVE_SERVER_ENGINE)
148         RaiseEvent(ENGINE_BEFORE_INITIALIZED, nil)
149         InitServer()
150
151         RaiseEvent(ENGINE_STARTED, nil)
152 }