Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from nobodyiam/demo-page
Add demo page for apollo client usage illustration purpose
- Loading branch information
Showing
16 changed files
with
326 additions
and
31 deletions.
There are no files selected for viewing
29 changes: 0 additions & 29 deletions
29
apollo-demo/src/main/java/com/ctrip/apollo/demo/DemoController.java
This file was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
apollo-demo/src/main/java/com/ctrip/apollo/demo/controller/DemoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.ctrip.apollo.demo.controller; | ||
|
||
import com.ctrip.apollo.client.ApolloConfigManager; | ||
import com.ctrip.apollo.client.model.ApolloRegistry; | ||
import com.ctrip.apollo.client.util.ConfigUtil; | ||
import com.ctrip.apollo.demo.model.Config; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Jason Song(song_s@ctrip.com) | ||
*/ | ||
@RestController | ||
@RequestMapping("/demo") | ||
@PropertySource("classpath:application.properties") | ||
public class DemoController { | ||
@Autowired | ||
private Environment env; | ||
@Autowired | ||
private ApolloConfigManager apolloConfigManager; | ||
//Apollo config client internal impl, not intended to be use by application, only for this test page | ||
private ConfigUtil configUtil = ConfigUtil.getInstance(); | ||
|
||
@Value("${apollo.foo}") | ||
private String foo; | ||
|
||
@RequestMapping(value = "/config/{configName:.*}", method = RequestMethod.GET) | ||
public Config queryConfig(@PathVariable String configName) { | ||
return new Config(configName, env.getProperty(configName, "undefined")); | ||
} | ||
|
||
@RequestMapping(value = "/client/registries", method = RequestMethod.GET) | ||
public List<ApolloRegistry> loadApolloRegistries() throws IOException { | ||
return configUtil.loadApolloRegistries(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
apollo-demo/src/main/java/com/ctrip/apollo/demo/exception/RestExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.ctrip.apollo.demo.exception; | ||
|
||
import com.ctrip.apollo.demo.model.ErrorResult; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.context.request.WebRequest; | ||
|
||
/** | ||
* Created by Jason on 7/6/15. | ||
*/ | ||
@ControllerAdvice | ||
public class RestExceptionHandler { | ||
|
||
@ExceptionHandler(Exception.class) | ||
ResponseEntity<ErrorResult> handleWebExceptions(Exception ex, | ||
WebRequest request) throws JsonProcessingException { | ||
ErrorResult error = new ErrorResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage()); | ||
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(error); | ||
|
||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
apollo-demo/src/main/java/com/ctrip/apollo/demo/model/Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ctrip.apollo.demo.model; | ||
|
||
/** | ||
* Created by Jason on 2/25/16. | ||
*/ | ||
public class Config { | ||
private final String name; | ||
private final String value; | ||
|
||
public Config(String name, String value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
apollo-demo/src/main/java/com/ctrip/apollo/demo/model/ErrorResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ctrip.apollo.demo.model; | ||
|
||
/** | ||
* Created by Jason on 7/6/15. | ||
*/ | ||
public class ErrorResult { | ||
private final int code; | ||
private final String msg; | ||
|
||
public ErrorResult(int code, String msg) { | ||
this.code = code; | ||
this.msg = msg; | ||
} | ||
|
||
public int getCode() { | ||
return code; | ||
} | ||
|
||
public String getMsg() { | ||
return msg; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,29 @@ | ||
<html> | ||
<!DOCTYPE html> | ||
<%@page contentType="text/html" pageEncoding="UTF-8" %> | ||
<html ng-app="Demo"> | ||
<meta http-equiv=Content-Type content="text/html;charset=utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> | ||
<title>Apollo Config Client</title> | ||
<link rel="stylesheet" type="text/css" href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css"/> | ||
<link rel="stylesheet" type="text/css" href="/styles/angular-toastr-1.4.1.min.css"/> | ||
<link rel='stylesheet' href='/styles/loading-bar.min.css' type='text/css' media='all' /> | ||
<link rel="stylesheet" type="text/css" href="/styles/app.css"/> | ||
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script> | ||
<script type="text/javascript" src="http://apps.bdimg.com/libs/angular.js/1.3.9/angular.min.js"></script> | ||
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> | ||
<!-- WARNING: Respond.js doesn't work if you view the page via file:// --> | ||
<!--[if lt IE 9]> | ||
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> | ||
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script> | ||
<![endif]--> | ||
<body> | ||
<h2>Hello World!</h2> | ||
<div class="container-fluid" ng-include="'/templates/list.html'"></div> | ||
<script type="text/javascript" src="//cdn.bootcss.com/underscore.js/1.8.3/underscore-min.js"></script> | ||
<script type="text/javascript" src="/scripts/ui-bootstrap-0.13.0.min.js"></script> | ||
<script type="text/javascript" src="/scripts/ui-bootstrap-tpls-0.13.0.min.js"></script> | ||
<script type="text/javascript" src="/scripts/angular-toastr-1.4.1.tpls.min.js"></script> | ||
<script type='text/javascript' src='/scripts/loading-bar.min.js'></script> | ||
<script type="text/javascript" src="/scripts/http.js"></script> | ||
<script type="text/javascript" src="/scripts/app.js"></script> | ||
</body> | ||
</html> |
1 change: 1 addition & 0 deletions
1
apollo-demo/src/main/webapp/s/scripts/angular-toastr-1.4.1.tpls.min.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
(function ($) { | ||
var app = angular.module('Demo', [ | ||
'ui.bootstrap', | ||
'toastr', | ||
'angular-loading-bar', | ||
'httpInterceptors' //custom http interceptor | ||
]); | ||
|
||
app.controller('DemoController', function ($scope, $http, $modal, toastr) { | ||
//var NONE = "none"; | ||
|
||
this.registries = {}; | ||
this.configQuery = {}; | ||
//this.refreshResult = NONE; | ||
|
||
var self = this; | ||
|
||
this.loadRegistries = function() { | ||
$http.get("demo/client/registries") | ||
.success(function (data) { | ||
self.registries = data; | ||
}) | ||
.error(function (data, status) { | ||
toastr.error((data && data.msg) || 'Loading registries failed'); | ||
}); | ||
}; | ||
|
||
this.queryConfig = function() { | ||
$http.get("demo/config/" + encodeURIComponent(this.configQuery.configName)) | ||
.success(function(data) { | ||
self.configQuery.configValue = data.value; | ||
}) | ||
.error(function(data, status) { | ||
toastr.error((data && data.msg) || 'Load config failed'); | ||
}); | ||
}; | ||
|
||
//this.refreshConfig = function() { | ||
// $http.post("refresh") | ||
// .success(function(data) { | ||
// self.assembleRefreshResult(data); | ||
// }) | ||
// .error(function(data, status) { | ||
// toastr.error((data && data.msg) || 'Refresh config failed'); | ||
// }); | ||
// | ||
//}; | ||
|
||
//this.assembleRefreshResult = function(changedPropertyArray) { | ||
// if(!changedPropertyArray || !changedPropertyArray.length) { | ||
// this.refreshResult = NONE; | ||
// return; | ||
// } | ||
// this.refreshResult = changedPropertyArray.join(','); | ||
//}; | ||
|
||
this.loadRegistries(); | ||
|
||
}); | ||
|
||
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
(function ($) { | ||
var httpInterceptors = angular.module('httpInterceptors', []); | ||
|
||
httpInterceptors.factory('httpInterceptor', function ($q) { | ||
return { | ||
'request': function (config) { | ||
var t = new Date().getTime(); | ||
|
||
if (config.url.indexOf('.htm') != -1 || config.url.indexOf('?_=') != -1) { | ||
return config; | ||
} | ||
|
||
config.url = config.url + '?_=' + t; | ||
|
||
return config; | ||
}, | ||
'response': function (response) { | ||
if (typeof response.data === 'object') { | ||
if (response.data.code != null && response.data.code != 200) { | ||
return $q.reject(response); | ||
} | ||
} | ||
|
||
return response; | ||
} | ||
}; | ||
}); | ||
|
||
httpInterceptors.config(function ($httpProvider) { | ||
$httpProvider.interceptors.push('httpInterceptor'); | ||
}); | ||
})(jQuery); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
apollo-demo/src/main/webapp/s/scripts/ui-bootstrap-0.13.0.min.js
Large diffs are not rendered by default.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
apollo-demo/src/main/webapp/s/scripts/ui-bootstrap-tpls-0.13.0.min.js
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.