Skip to content

Commit

Permalink
Merge pull request #18 from nobodyiam/demo-page
Browse files Browse the repository at this point in the history
Add demo page for apollo client usage illustration purpose
  • Loading branch information
yiming187 committed Mar 21, 2016
2 parents 6e99c57 + c9d72d5 commit 26b171f
Show file tree
Hide file tree
Showing 16 changed files with 326 additions and 31 deletions.

This file was deleted.

@@ -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();
}
}
@@ -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 apollo-demo/src/main/java/com/ctrip/apollo/demo/model/Config.java
@@ -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;
}
}
@@ -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;
}
}
28 changes: 26 additions & 2 deletions apollo-demo/src/main/webapp/WEB-INF/views/welcome.jsp
@@ -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>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions apollo-demo/src/main/webapp/s/scripts/app.js
@@ -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);
32 changes: 32 additions & 0 deletions apollo-demo/src/main/webapp/s/scripts/http.js
@@ -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);
7 changes: 7 additions & 0 deletions apollo-demo/src/main/webapp/s/scripts/loading-bar.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit 26b171f

Please sign in to comment.