Skip to content

Commit

Permalink
首页环境切换 & app主页显示app基本信息 & 样式优化
Browse files Browse the repository at this point in the history
  • Loading branch information
lepdou committed Apr 25, 2016
1 parent 22756eb commit 0f8aa33
Show file tree
Hide file tree
Showing 15 changed files with 466 additions and 98 deletions.
Expand Up @@ -27,12 +27,16 @@ public static class AppAPI extends API {

public static String APP_API = "/apps";

public List<AppDTO> getApps(Env env) {
public List<AppDTO> findApps(Env env) {
AppDTO[] appDTOs =
restTemplate.getForObject(getAdminServiceHost(env) + APP_API, AppDTO[].class);
return Arrays.asList(appDTOs);
}

public AppDTO loadApp(Env env, String appId){
return restTemplate.getForObject(getAdminServiceHost(env) + APP_API + "/" + appId, AppDTO.class);
}

public AppDTO save(Env env, AppDTO app) {
return restTemplate.postForEntity(getAdminServiceHost(env) + APP_API, app, AppDTO.class)
.getBody();
Expand Down
Expand Up @@ -9,6 +9,7 @@
import org.springframework.web.bind.annotation.RestController;

import com.ctrip.apollo.core.dto.AppDTO;
import com.ctrip.apollo.core.enums.Env;
import com.ctrip.apollo.core.exception.BadRequestException;
import com.ctrip.apollo.core.utils.StringUtils;
import com.ctrip.apollo.portal.entity.ClusterNavTree;
Expand All @@ -24,9 +25,12 @@ public class AppController {
private AppService appService;


@RequestMapping("")
public List<AppDTO> findAllApp(){
return appService.findAll();
@RequestMapping("/env/{env}")
public List<AppDTO> findAllApp(@PathVariable String env){
if (StringUtils.isEmpty(env)){
throw new BadRequestException("env can not be empty");
}
return appService.findAll(Env.valueOf(env));
}

@RequestMapping("/{appId}/navtree")
Expand All @@ -47,6 +51,14 @@ public ResponseEntity<Void> create(@RequestBody AppDTO app) {
return ResponseEntity.ok().build();
}

@RequestMapping(value = "/{appId}", method = RequestMethod.GET)
public AppDTO load(@PathVariable String appId){
if (StringUtils.isEmpty(appId)){
throw new BadRequestException("app id can not be empty.");
}
return appService.load(appId);
}

private boolean isInvalidApp(AppDTO app) {
return StringUtils.isContainEmpty(app.getName(), app.getAppId(), app.getOwnerEmail(), app.getOwnerName());
}
Expand Down
@@ -0,0 +1,25 @@
package com.ctrip.apollo.portal.controller;

import com.ctrip.apollo.core.enums.Env;
import com.ctrip.apollo.portal.PortalSettings;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/envs")
public class EnvController {

@Autowired
private PortalSettings portalSettings;

@RequestMapping(value = "", method = RequestMethod.GET)
public List<Env> envs(){
return portalSettings.getEnvs();
}

}
Expand Up @@ -5,12 +5,17 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpStatusCodeException;

import com.ctrip.apollo.common.utils.ExceptionUtils;
import com.ctrip.apollo.core.dto.AppDTO;
import com.ctrip.apollo.core.enums.Env;
import com.ctrip.apollo.core.exception.BadRequestException;
import com.ctrip.apollo.core.exception.ServiceException;
import com.ctrip.apollo.core.utils.StringUtils;
import com.ctrip.apollo.portal.PortalSettings;
import com.ctrip.apollo.portal.api.AdminServiceAPI;
import com.ctrip.apollo.portal.entity.ClusterNavTree;
Expand All @@ -29,10 +34,33 @@ public class AppService {
@Autowired
private AdminServiceAPI.AppAPI appAPI;

public List<AppDTO> findAll() {
// TODO: 16/4/21 先从 portalSettings第一个环境去apps,后续可以优化
Env env = portalSettings.getEnvs().get(0);
return appAPI.getApps(env);
public List<AppDTO> findAll(Env env) {
return appAPI.findApps(env);
}

public AppDTO load(String appId) {
//轮询环境直到能找到此app的信息
AppDTO app = null;
for (Env env : portalSettings.getEnvs()) {
try {
app = appAPI.loadApp(env, appId);
break;
} catch (HttpClientErrorException e) {
//not exist maybe because create app fail.
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
logger.warn("app:{} in {} not exist", appId, env);
} else {
logger.error("load app info({}) from env:{} error.", appId, env);
throw new ServiceException("can not load app from all envs");
}
}
}
if (app == null){
throw new BadRequestException(String.format("invalid app id %s", appId));
}

return app;

}

public ClusterNavTree buildClusterNavTree(String appId) {
Expand Down
2 changes: 1 addition & 1 deletion apollo-portal/src/main/resources/application.yml
Expand Up @@ -15,4 +15,4 @@ ctrip:

apollo:
portal:
env: local
env: local,dev
20 changes: 13 additions & 7 deletions apollo-portal/src/main/resources/static/index.html
Expand Up @@ -13,29 +13,37 @@

<body ng-controller="IndexController">

<!--<div ng-include="'views/common/nav.html'"></div>-->
<div class="site-notice">当前站点支持
&nbsp;<a ng-repeat="env in envs" ng-class="{selected:selectedEnv == env}" ng-click="switchEnv(env)"><em>{{env}}
</em></a>&nbsp;
环境点击切换
</div>

<header class="site-header jumbotron">
<div class="container">
<div class="row">
<div class="col-xs-12"><h1>Apollo</h1>
<p>携程统一配置中心<br>
<span class="package-amount">共收录了 <strong>{{appsCount}}</strong> 个项目</span>
<a class="btn btn-success btn-lg" href="views/create-app.html">创建项目</a>
<a class="btn btn-success" href="views/create-app.html">创建项目</a>
</p>

<form class="" role="search">
<div class="form-group"><input type="text" class="form-control search clearable"
placeholder="搜索App, 例如:900088" ng-model="searchKey" ng-change="search()"></div>
placeholder="搜索App, 例如:apollo" ng-model="searchKey"
ng-change="search()"></div>
</form>
</div>

</div>
</div>

</header>

<div class="container-fluid apollo-container">
<div class="list-group apps">
<a class="package list-group-item" target="_blank" href="/views/app.html?#/appid={{app.appId}}" ng-repeat="app in apps ">
<a class="package list-group-item" target="_blank" href="/views/app.html?#/appid={{app.appId}}"
ng-repeat="app in apps ">
<div class="row">
<div class="col-md-3"><h4 class="apps-name">{{app.appId}}</h4></div>
<div class="col-md-7 hidden-xs">
Expand All @@ -49,9 +57,6 @@
</p>

</div>
<!--<div class="package-extra-info col-md-9 col-md-offset-3 col-xs-12"><span><i-->
<!--class="glyphicon glyphicon-fire"></i> 92793</span>-->
<!--</div>-->
</div>
</a>
</div>
Expand All @@ -73,6 +78,7 @@

<script type="application/javascript" src="scripts/app.js"></script>
<script type="application/javascript" src="scripts/services/AppService.js"></script>
<script type="application/javascript" src="scripts/services/EnvService.js"></script>
<script type="application/javascript" src="scripts/controller/IndexController.js"></script>
</body>
</html>
@@ -1,27 +1,49 @@
index_module.controller('IndexController', ['$scope', '$window', 'toastr', 'AppService',
function ($scope, $window, toastr, AppService) {
index_module.controller('IndexController', ['$scope', '$window', 'toastr', 'AppService', 'EnvService',
function ($scope, $window, toastr, AppService, EnvService) {

var apps = [];
AppService.find_all_app().then(function (result) {
apps = result;
$scope.apps = apps;
$scope.appsCount = apps.length;
});
$scope.envs = [];
$scope.selectedEnv = '';
EnvService.find_all_envs().then(function (result) {
$scope.envs = result;
//default select first env
$scope.switchEnv($scope.envs[0]);
}, function (result) {
toastr.error(result.status + result.data.message, "load env error");
});

$scope.search = function () {
var key = $scope.searchKey;
if (key == '') {
$scope.apps = apps;
return;
}
var result = [];
apps.forEach(function (item) {
if (item.appId.indexOf(key) >= 0 || item.name.indexOf(key) >= 0) {
result.push(item);
}
});
var apps = [];

$scope.switchEnv = function (env) {
$scope.selectedEnv = env;
loadApps(env);
};

function loadApps(env){
AppService.find_all_app(env).then(function (result) {
apps = result;
$scope.apps = apps;
$scope.appsCount = apps.length;
$scope.selectedEnv = env;
}, function (result) {
toastr.error(result.status + result.data.message, "load apps error");
});
};


$scope.apps = result;
};
$scope.search = function () {
var key = $scope.searchKey;
if (key == '') {
$scope.apps = apps;
return;
}
var result = [];
apps.forEach(function (item) {
if (item.appId.indexOf(key) >= 0 || item.name.indexOf(key) >= 0) {
result.push(item);
}
});

$scope.apps = result;
};

}]);
Expand Up @@ -61,6 +61,14 @@ application_module.controller("AppConfigController",
toastr.error(result.status + result.data.message, "加载导航出错");
});

/////////// app info ////////////

AppService.load($scope.pageContext.appId).then(function (result) {
$scope.appInfo = result;
},function (result) {
toastr.error(result.status + result.data.message, "加载App信息出错");
});

/////////// namespace ////////////

var namespace_view_type = {
Expand Down Expand Up @@ -90,7 +98,6 @@ application_module.controller("AppConfigController",
item.viewType = namespace_view_type.TABLE;
}


item.isTextEditing = false;
})
}
Expand Down Expand Up @@ -160,6 +167,12 @@ application_module.controller("AppConfigController",
);
};

$scope.isItemsViewOpened = true;
$scope.toggleItemView = function (isOpened) {
$scope.isItemsViewOpened = isOpened;

};

//文本编辑框状态切换
$scope.toggleTextEditStatus = function (namespace) {
namespace.isTextEditing = !namespace.isTextEditing;
Expand Down
Expand Up @@ -3,7 +3,7 @@ appService.service('AppService', ['$resource', '$q', function ($resource, $q) {
find_all_app:{
method: 'GET',
isArray: true,
url:'/apps'
url:'/apps/env/:env'
},
load_navtree:{
methode: 'GET',
Expand All @@ -20,11 +20,11 @@ appService.service('AppService', ['$resource', '$q', function ($resource, $q) {
}
});
return {
find_all_app: function () {
find_all_app: function (env) {
var d = $q.defer();
app_resource.find_all_app({
},
function (result) {
env: env
}, function (result) {
d.resolve(result);
}, function (result) {
d.reject(result);
Expand Down
@@ -0,0 +1,22 @@
appService.service('EnvService', ['$resource', '$q', function ($resource, $q) {
var env_resource = $resource('/envs', {}, {
find_all_envs:{
method: 'GET',
isArray: true,
url:'/envs'
}
});
return {
find_all_envs: function () {
var d = $q.defer();
env_resource.find_all_envs({
},
function (result) {
d.resolve(result);
}, function (result) {
d.reject(result);
});
return d.promise;
}
}
}]);

0 comments on commit 0f8aa33

Please sign in to comment.