@@ -6,10 +6,12 @@ import (
66 "fmt"
77 "maps"
88 "os"
9+ "os/signal"
910 "path/filepath"
1011 "slices"
1112 "sort"
1213 "strings"
14+ "syscall"
1315
1416 "github.com/goravel/framework/config"
1517 frameworkconsole "github.com/goravel/framework/console"
@@ -36,8 +38,15 @@ func init() {
3638 setEnv ()
3739 setRootPath ()
3840
41+ ctx , cancel := context .WithCancel (context .Background ())
42+
3943 app := & Application {
40- Container : NewContainer (),
44+ Container : NewContainer (),
45+
46+ ctx : ctx ,
47+ cancel : cancel ,
48+ quit : make (chan os.Signal , 1 ),
49+
4150 publishes : make (map [string ]map [string ]string ),
4251 publishGroups : make (map [string ]map [string ]string ),
4352 }
@@ -50,6 +59,11 @@ func init() {
5059
5160type Application struct {
5261 * Container
62+
63+ ctx context.Context
64+ cancel context.CancelFunc
65+ quit chan os.Signal
66+
5367 configuredServiceProviders []foundation.ServiceProvider
5468 publishes map [string ]map [string ]string
5569 publishGroups map [string ]map [string ]string
@@ -61,6 +75,10 @@ func NewApplication() foundation.Application {
6175 return App
6276}
6377
78+ func (r * Application ) About (section string , items []foundation.AboutItem ) {
79+ console .AddAboutInformation (section , items ... )
80+ }
81+
6482func (r * Application ) Boot () {
6583 r .configuredServiceProviders = r .configuredServiceProviders [:0 ]
6684 clear (r .publishes )
@@ -87,75 +105,132 @@ func (r *Application) Commands(commands []contractsconsole.Command) {
87105 r .registerCommands (commands )
88106}
89107
90- func (r * Application ) Path ( path ... string ) string {
91- return internals . Path ( path ... )
108+ func (r * Application ) Context () context. Context {
109+ return r . ctx
92110}
93111
94- func (r * Application ) BasePath ( path ... string ) string {
95- return r .absPath ( path ... )
112+ func (r * Application ) GetJson () foundation. Json {
113+ return r .json
96114}
97115
98- func (r * Application ) ConfigPath (path ... string ) string {
99- path = append ([]string {support .RelativePath , "config" }, path ... )
100- return r .absPath (path ... )
116+ func (r * Application ) IsLocale (ctx context.Context , locale string ) bool {
117+ return r .CurrentLocale (ctx ) == locale
101118}
102119
103- func (r * Application ) DatabasePath (path ... string ) string {
104- path = append ([]string {support .RelativePath , "database" }, path ... )
105- return r .absPath (path ... )
120+ func (r * Application ) Publishes (packageName string , paths map [string ]string , groups ... string ) {
121+ if _ , exist := r .publishes [packageName ]; ! exist {
122+ r .publishes [packageName ] = make (map [string ]string )
123+ }
124+ maps .Copy (r .publishes [packageName ], paths )
125+ for _ , group := range groups {
126+ r .addPublishGroup (group , paths )
127+ }
106128}
107129
108- func (r * Application ) ExecutablePath ( path ... string ) string {
109- path = append ([] string { support . RootPath }, path ... )
110- return r . absPath ( path ... )
130+ func (r * Application ) Refresh () {
131+ r . Fresh ( )
132+ r . Boot ( )
111133}
112134
113- func (r * Application ) FacadesPath (path ... string ) string {
114- return internals .FacadesPath (path ... )
115- }
135+ func (r * Application ) Run () {
136+ signal .Notify (r .quit , syscall .SIGINT , syscall .SIGTERM )
116137
117- func (r * Application ) StoragePath (path ... string ) string {
118- path = append ([]string {support .RelativePath , "storage" }, path ... )
119- return r .absPath (path ... )
120- }
138+ log := r .MakeLog ()
139+ route := r .MakeRoute ()
140+ grpc := r .MakeGrpc ()
141+ queue := r .MakeQueue ()
142+ schedule := r .MakeSchedule ()
121143
122- func (r * Application ) Refresh () {
123- r .Fresh ()
124- r .Boot ()
144+ go func () {
145+ <- r .quit
146+ r .cancel ()
147+ }()
148+
149+ go func () {
150+ if route != nil {
151+ if err := route .Run (); err != nil {
152+ log .Errorf ("Route Run error: %v" , err )
153+ }
154+ }
155+
156+ if grpc != nil {
157+ if err := grpc .Run (); err != nil {
158+ log .Errorf ("Grpc Run error: %v" , err )
159+ }
160+ }
161+
162+ if queue != nil {
163+ if err := queue .Worker ().Run (); err != nil {
164+ log .Errorf ("Queue run error: %v" , err )
165+ }
166+ }
167+
168+ if schedule != nil {
169+ go schedule .Run ()
170+ }
171+ }()
172+
173+ go func () {
174+ <- r .ctx .Done ()
175+
176+ if route != nil {
177+ if err := route .Shutdown (); err != nil {
178+ log .Errorf ("Route Shutdown error: %v" , err )
179+ }
180+ }
181+
182+ if grpc != nil {
183+ if err := grpc .Shutdown (); err != nil {
184+ log .Errorf ("Grpc Shutdown error: %v" , err )
185+ }
186+ }
187+
188+ if queue != nil {
189+ if err := queue .Worker ().Shutdown (); err != nil {
190+ log .Errorf ("Queue Shutdown error: %v" , err )
191+ }
192+ }
193+
194+ if schedule != nil {
195+ if err := schedule .Shutdown (); err != nil {
196+ log .Errorf ("Schedule Shutdown error: %v" , err )
197+ }
198+ }
199+ }()
125200}
126201
127- func (r * Application ) ResourcePath (path ... string ) string {
128- path = append ([]string {support .RelativePath , "resources" }, path ... )
129- return r .absPath (path ... )
202+ func (r * Application ) SetJson (j foundation.Json ) {
203+ if j != nil {
204+ r .json = j
205+ }
130206}
131207
132- func (r * Application ) LangPath (path ... string ) string {
133- defaultPath := "lang"
134- if configFacade := r .MakeConfig (); configFacade != nil {
135- defaultPath = configFacade .GetString ("app.lang_path" , defaultPath )
208+ func (r * Application ) SetLocale (ctx context.Context , locale string ) context.Context {
209+ lang := r .MakeLang (ctx )
210+ if lang == nil {
211+ color .Errorln ("Lang facade not initialized." )
212+ return ctx
136213 }
137214
138- path = append ([]string {support .RelativePath , defaultPath }, path ... )
139- return r .absPath (path ... )
215+ return lang .SetLocale (locale )
140216}
141217
142- func (r * Application ) PublicPath (path ... string ) string {
143- path = append ([]string {support .RelativePath , "public" }, path ... )
218+ func (r * Application ) Version () string {
219+ return support .Version
220+ }
221+
222+ func (r * Application ) BasePath (path ... string ) string {
144223 return r .absPath (path ... )
145224}
146225
147- func (r * Application ) Publishes (packageName string , paths map [string ]string , groups ... string ) {
148- if _ , exist := r .publishes [packageName ]; ! exist {
149- r .publishes [packageName ] = make (map [string ]string )
150- }
151- maps .Copy (r .publishes [packageName ], paths )
152- for _ , group := range groups {
153- r .addPublishGroup (group , paths )
154- }
226+ func (r * Application ) ConfigPath (path ... string ) string {
227+ path = append ([]string {support .RelativePath , "config" }, path ... )
228+ return r .absPath (path ... )
155229}
156230
157- func (r * Application ) Version () string {
158- return support .Version
231+ func (r * Application ) DatabasePath (path ... string ) string {
232+ path = append ([]string {support .RelativePath , "database" }, path ... )
233+ return r .absPath (path ... )
159234}
160235
161236func (r * Application ) CurrentLocale (ctx context.Context ) string {
@@ -168,32 +243,42 @@ func (r *Application) CurrentLocale(ctx context.Context) string {
168243 return lang .CurrentLocale ()
169244}
170245
171- func (r * Application ) SetLocale (ctx context.Context , locale string ) context.Context {
172- lang := r .MakeLang (ctx )
173- if lang == nil {
174- color .Errorln ("Lang facade not initialized." )
175- return ctx
176- }
246+ func (r * Application ) ExecutablePath (path ... string ) string {
247+ path = append ([]string {support .RootPath }, path ... )
248+ return r .absPath (path ... )
249+ }
177250
178- return lang .SetLocale (locale )
251+ func (r * Application ) FacadesPath (path ... string ) string {
252+ return internals .FacadesPath (path ... )
179253}
180254
181- func (r * Application ) SetJson (j foundation.Json ) {
182- if j != nil {
183- r .json = j
255+ func (r * Application ) LangPath (path ... string ) string {
256+ defaultPath := "lang"
257+ if configFacade := r .MakeConfig (); configFacade != nil {
258+ defaultPath = configFacade .GetString ("app.lang_path" , defaultPath )
184259 }
260+
261+ path = append ([]string {support .RelativePath , defaultPath }, path ... )
262+ return r .absPath (path ... )
185263}
186264
187- func (r * Application ) GetJson () foundation. Json {
188- return r . json
265+ func (r * Application ) Path ( path ... string ) string {
266+ return internals . Path ( path ... )
189267}
190268
191- func (r * Application ) About (section string , items []foundation.AboutItem ) {
192- console .AddAboutInformation (section , items ... )
269+ func (r * Application ) PublicPath (path ... string ) string {
270+ path = append ([]string {support .RelativePath , "public" }, path ... )
271+ return r .absPath (path ... )
193272}
194273
195- func (r * Application ) IsLocale (ctx context.Context , locale string ) bool {
196- return r .CurrentLocale (ctx ) == locale
274+ func (r * Application ) ResourcePath (path ... string ) string {
275+ path = append ([]string {support .RelativePath , "resources" }, path ... )
276+ return r .absPath (path ... )
277+ }
278+
279+ func (r * Application ) StoragePath (path ... string ) string {
280+ path = append ([]string {support .RelativePath , "storage" }, path ... )
281+ return r .absPath (path ... )
197282}
198283
199284func (r * Application ) absPath (paths ... string ) string {
0 commit comments