@@ -25,9 +25,10 @@ export default Ember.Service.extend({
2525 title : 'NagiosTV for Nagios 4' ,
2626 iconUrl : '/images/tv-xxl.png' ,
2727 connectionStyle : 'direct' , // direct, proxy
28+ nodeServerHost : 'http://localhost:3000' ,
29+ nodeServerPath : '/nagios' ,
2830 nagiosServerHost : 'http://example.com' ,
2931 nagiosServerCgiPath : '/nagios/cgi-bin' ,
30- nodeServerHost : 'http://localhost:3000' ,
3132 auth : true ,
3233 username : 'nagiosadmin' ,
3334 password : ''
@@ -103,27 +104,105 @@ export default Ember.Service.extend({
103104 // overlay the loaded settings over top of the default settings
104105 // this helps prevent issues when we add new settings
105106 const defaultSettings = this . get ( 'settings' ) ;
106- const mergedSettings = Object . assign ( defaultSettings , loadedSettings ) ;
107+ const mergedSettings = Object . assign ( { } , defaultSettings , loadedSettings ) ;
107108 this . set ( 'settings' , mergedSettings ) ;
109+
110+ // If connectionStyle is 'proxy', then let's try to load settings from the proxy location
111+ if ( mergedSettings . connectionStyle === 'proxy' ) {
112+ this . fetchProxySettings ( ) ;
113+ }
108114 }
109115 } ,
110116
111117 saveLocalSettings : function ( ) {
112118 console . log ( 'saveLocalSettings()' ) ;
113119 localStorage . setItem ( 'nagiostv-settings' , JSON . stringify ( this . settings ) ) ;
120+
121+ // If connectionStyle is 'proxy', then let's try to load settings from the proxy location
122+ if ( this . get ( 'settings' ) . connectionStyle === 'proxy' ) {
123+ this . fetchProxySettings ( ) ;
124+ }
114125 } ,
115126
116127 clearLocalSettings : function ( ) {
117128 console . log ( 'clearLocalSettings()' ) ;
118129 localStorage . removeItem ( 'nagiostv-settings' ) ;
119130 } ,
120131
132+ fetchProxySettings : function ( ) {
133+ this . fetchProxySettingsPromise ( ) . then ( ( data ) => {
134+ //console.log('fetchProxySettings success');
135+ //console.log(data);
136+ //console.log('Overwriting local settings with server settings');
137+ this . set ( 'settings' , data ) ;
138+ } , ( err ) => {
139+ console . log ( 'fetchProxySettings error' ) ;
140+ } ) ;
141+ } ,
142+
143+ fetchProxySettingsPromise : function ( ) {
144+ var nodeServerHost = this . get ( 'settings.nodeServerHost' ) ;
145+ return new Promise ( ( resolve , reject ) => {
146+ $ . ajax ( {
147+ url : nodeServerHost + '/settings' ,
148+ method : 'GET' ,
149+ dataType : 'json' ,
150+ success : function ( data ) {
151+ resolve ( data ) ;
152+ } ,
153+ error : function ( fail ) {
154+ reject ( fail ) ;
155+ }
156+ } ) ;
157+ } ) ;
158+ } ,
159+
121160 saveProxySettings : function ( ) {
122161 console . log ( 'saveProxySettings()' ) ;
162+
163+ // save a subset of the settings locally
164+ const localSettings = { } ;
165+ localSettings . connectionStyle = this . settings . connectionStyle ;
166+ localSettings . nodeServerHost = this . settings . nodeServerHost ;
167+ localSettings . nodeServerPath = this . settings . nodeServerPath ;
168+ localStorage . setItem ( 'nagiostv-settings' , JSON . stringify ( localSettings ) ) ;
169+
170+ // send the settings to the proxy
171+ var settings = this . get ( 'settings' ) ;
172+ return new Promise ( ( resolve , reject ) => {
173+ $ . ajax ( {
174+ url : settings . nodeServerHost + '/settings' ,
175+ method : 'POST' ,
176+ contentType : "application/json; charset=utf-8" ,
177+ data : JSON . stringify ( settings ) ,
178+ dataType : 'json' ,
179+ success : function ( data ) {
180+ resolve ( data ) ;
181+ } ,
182+ error : function ( fail ) {
183+ reject ( fail ) ;
184+ }
185+ } ) ;
186+ } ) ;
187+
123188 } ,
124189
125190 clearProxySettings : function ( ) {
126191 console . log ( 'clearProxySettings()' ) ;
192+ var nodeServerHost = this . get ( 'settings.nodeServerHost' ) ;
193+ return new Promise ( ( resolve , reject ) => {
194+ $ . ajax ( {
195+ url : nodeServerHost + '/settings-clear' ,
196+ method : 'GET' ,
197+ dataType : 'json' ,
198+ success : function ( data ) {
199+ resolve ( data ) ;
200+ } ,
201+ error : function ( fail ) {
202+ reject ( fail ) ;
203+ }
204+ } ) ;
205+ } ) ;
127206 } ,
128207
129208 /**************************************
@@ -205,8 +284,18 @@ export default Ember.Service.extend({
205284 fetchUpdateFromNagios4 : function ( ) {
206285
207286 var that = this ;
208- var baseUrl = this . get ( 'settings.nagiosServerHost' ) ;
209- var basePath = this . get ( 'settings.nagiosServerCgiPath' ) ;
287+ var settings = this . get ( 'settings' ) ;
288+
289+ let baseUrl ;
290+ let basePath ;
291+ // switch for direct vs proxy connection
292+ if ( settings . connectionStyle === 'proxy' ) {
293+ baseUrl = settings . nodeServerHost ;
294+ basePath = settings . nodeServerPath ;
295+ } else {
296+ baseUrl = settings . nagiosServerHost ;
297+ basePath = settings . nagiosServerCgiPath ;
298+ }
210299
211300 this . getJSON ( baseUrl + basePath + '/statusjson.cgi?query=hostlist&details=true' ) . then ( ( data ) => {
212301
0 commit comments