-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapplication.go
91 lines (76 loc) · 2.25 KB
/
application.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
package foundation
import (
"fmt"
"log"
"os"
"reflect"
"github.com/RobyFerro/go-web-framework/kernel"
"github.com/RobyFerro/go-web-framework/register"
"github.com/common-nighthawk/go-figure"
)
type BaseEntities struct {
Controllers register.ControllerRegister
Commands register.CommandRegister
CommandServices register.ServiceRegister
Middlewares register.MiddlewareRegister
Models register.ModelRegister
Router []register.HTTPRouter
}
// Start will run the HTTP web server
func Start(e BaseEntities, c kernel.ServerConf) {
startup(e)
kernel.RunServer(c, e.Router)
}
// StartCommand method runs specific CLI command
func StartCommand(args []string, e BaseEntities) {
startup(e)
c := kernel.BuildCommandContainer()
cmd := kernel.Commands[args[0]]
if cmd == nil {
fmt.Println("Command not found!")
os.Exit(1)
}
rc := reflect.ValueOf(cmd)
if len(args) == 2 {
reflect.Indirect(rc).FieldByName("Args").SetString(args[1])
}
err := c.Invoke(rc.MethodByName("Run").Interface())
if err != nil {
log.Fatal(err)
}
}
func startup(e BaseEntities) {
myFigure := figure.NewFigure("Go-Web", "graffiti", true)
myFigure.Print()
fmt.Println("Go-Web CLI tool - Author: [email protected]")
RegisterBaseEntities(e)
}
// RegisterBaseEntities base entities in Go-Web kernel
// This method will register: Controllers, Models, CLI commands, Services and middleware
func RegisterBaseEntities(entities BaseEntities) {
kernel.Controllers = entities.Controllers
kernel.Middlewares = entities.Middlewares
kernel.Models = entities.Models
kernel.Router = entities.Router
mergeCommands(entities.Commands)
mergeMiddleware(entities.Middlewares)
mergeCommandServices(entities.CommandServices)
}
// MergeCommands will merge system command with customs
func mergeCommands(commands register.CommandRegister) {
for i, c := range commands {
kernel.Commands[i] = c
}
}
// MergeCommands will merge system command with customs
func mergeMiddleware(mw register.MiddlewareRegister) {
for i, c := range mw {
kernel.Middlewares[i] = c
}
}
// MergeCommands will merge system command with customs
func mergeCommandServices(services []interface{}) {
for _, s := range services {
kernel.CommandServices = append(kernel.CommandServices, s)
}
}