Skip to content

Commit

Permalink
Merge pull request #56 from lepdou/app_search
Browse files Browse the repository at this point in the history
portal 的环境信息从后端读取 & 支持切换环境
  • Loading branch information
yiming187 committed Mar 29, 2016
2 parents b271beb + 8f0a300 commit 46d6e00
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@ public class ConfigController {
public AppConfigVO detail(@PathVariable String appId, @PathVariable String env,
@PathVariable long versionId) {

if (Strings.isNullOrEmpty(appId)) {
throw new NotFoundException();
if (Strings.isNullOrEmpty(appId) || Strings.isNullOrEmpty(env)) {
throw new IllegalArgumentException(
String.format("app id and env can not be empty. app id:%s , env:%s", appId, env));
}

Apollo.Env e = Apollo.Env.valueOf(env);

if (versionId == PortalConstants.LASTEST_VERSION_ID) {
return configService.loadLatestConfig(Apollo.Env.DEV, appId);

return configService.loadLatestConfig(e, appId);

} else if (versionId > 0) {
return configService.loadReleaseConfig(Apollo.Env.DEV, appId, versionId);

return configService.loadReleaseConfig(e, appId, versionId);

} else {
throw new NotFoundException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.ctrip.apollo.portal.controller;

import com.ctrip.apollo.Apollo;
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.RestController;

import java.util.List;

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

@Autowired
private PortalSettings portalSettings;

@RequestMapping("")
public List<Apollo.Env> envs(){
return portalSettings.getEnvs();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ctrip.apollo.portal.controller;

import com.google.common.base.Strings;

import com.ctrip.apollo.Apollo;
import com.ctrip.apollo.core.dto.VersionDTO;
import com.ctrip.apollo.portal.service.VersionService;
Expand All @@ -9,6 +11,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@RestController
Expand All @@ -20,6 +23,12 @@ public class VersionController {

@RequestMapping("/{appId}/{env}")
public List<VersionDTO> versions(@PathVariable String appId, @PathVariable String env) {
return versionService.findVersionsByApp(Apollo.Env.DEV, appId);

if (Strings.isNullOrEmpty(appId) || Strings.isNullOrEmpty(env)) {
throw new IllegalArgumentException(
String.format("app id and env can not be empty. app id:%s , env:%s", appId, env));
}

return versionService.findVersionsByApp(Apollo.Env.valueOf(env), appId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ public AppConfigVO loadReleaseConfig(Env env, String appId, long versionId) {

long releaseId = getReleaseIdFromVersionId(env, versionId);
if (releaseId == -1) {
logger.warn("get release id error env:{}, app id:{}, version id:{}", env, appId, versionId);
return null;
}

ReleaseSnapshotDTO[] releaseSnapShots = configAPI.getConfigByReleaseId(Env.DEV, releaseId);
ReleaseSnapshotDTO[] releaseSnapShots = configAPI.getConfigByReleaseId(env, releaseId);
if (releaseSnapShots == null || releaseSnapShots.length == 0) {
return null;
}
Expand Down Expand Up @@ -83,7 +84,8 @@ private long getReleaseIdFromVersionId(Env env, long versionId) {
private void collectDefaultClusterConfigs(String appId, ReleaseSnapshotDTO snapShot,
AppConfigVO appConfigVO) {

Map<String, List<ConfigItemDTO>> groupedConfigs = groupConfigsByApp(snapShot.getConfigurations());
Map<String, List<ConfigItemDTO>> groupedConfigs =
groupConfigsByApp(appId, snapShot.getConfigurations());

List<AppConfigVO.OverrideAppConfig> overrideAppConfigs = appConfigVO.getOverrideAppConfigs();

Expand All @@ -107,7 +109,7 @@ private void collectDefaultClusterConfigs(String appId, ReleaseSnapshotDTO snapS
/**
* appId -> List<KV>
*/
private Map<String, List<ConfigItemDTO>> groupConfigsByApp(String configJson) {
private Map<String, List<ConfigItemDTO>> groupConfigsByApp(String selfAppId, String configJson) {
if (configJson == null || "".equals(configJson)) {
return Maps.newHashMap();
}
Expand All @@ -120,8 +122,10 @@ private Map<String, List<ConfigItemDTO>> groupConfigsByApp(String configJson) {
try {
kvMaps = objectMapper.readValue(configJson, Map.class);
} catch (IOException e) {
// todo log
logger.error("parse release snapshot json error. app id:{}", selfAppId);
return Maps.newHashMap();
}

for (Map.Entry<String, String> entry : kvMaps.entrySet()) {
key = entry.getKey();
value = entry.getValue();
Expand Down Expand Up @@ -151,7 +155,7 @@ private void collectSpecialClusterConfigs(String appId, ReleaseSnapshotDTO snapS
new AppConfigVO.OverrideClusterConfig();
overrideClusterConfig.setClusterName(snapShot.getClusterName());
// todo step1: cluster special config can't override other app config
overrideClusterConfig.setConfigs(groupConfigsByApp(snapShot.getConfigurations()).get(appId));
overrideClusterConfig.setConfigs(groupConfigsByApp(appId, snapShot.getConfigurations()).get(appId));
overrideClusterConfigs.add(overrideClusterConfig);
}

Expand Down Expand Up @@ -242,7 +246,7 @@ private void collectDefaultClusterConfigs(String appId, List<ConfigItemDTO> clus

for (ConfigItemDTO config : clusterConfigs) {
String targetAppId = config.getAppId();
if (appId == targetAppId) {// app self's configs
if (appId.equals(targetAppId)) {// app self's configs
defaultClusterConfigs.add(config);
} else {// override other app configs
if (appIdMapOverrideAppConfig == null) {
Expand Down
2 changes: 1 addition & 1 deletion apollo-portal/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ ctrip:

apollo:
portal:
env: dev,fws,uat
env: local,dev,fws,uat
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
application_module.controller("AppConfigController",
['$scope', '$rootScope', '$state', '$location', 'toastr',
'AppService', 'ConfigService', 'VersionService',
function ($scope, $rootScope, $state, $location, toastr, AppService, ConfigService, VersionService) {
'AppService', 'EnvService', 'ConfigService', 'VersionService',
function ($scope, $rootScope, $state, $location, toastr, AppService, EnvService, ConfigService, VersionService) {

var configLocation = {
appId: $rootScope.appId,
env: 'uat',
env: 'LOCAL',
versionId: -1
};

Expand All @@ -15,7 +15,11 @@ application_module.controller("AppConfigController",
$scope.configLocation = configLocation;

/**env*/
$scope.envs = ['dev', 'fws', 'fat', 'uat', 'lpt', 'prod', 'tools'];
EnvService.getAllEnvs().then(function(result){
$scope.envs = result;
}, function(result){
toastr.error("加载环境信息失败", result);
});

$scope.switchEnv = function (selectedEnv) {
configLocation.env = selectedEnv;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
appService.service('EnvService', ['$resource', '$q', function ($resource, $q) {
var env_resource = $resource('/envs', {}, {
all: {
method: 'GET',
isArray: true
}
});
return {
getAllEnvs: function getAllEnvs() {
var d = $q.defer();
env_resource.all({}, function (result) {
d.resolve(result);
}, function (result) {
d.reject(result);
});
return d.promise;
}
}
}]);
19 changes: 0 additions & 19 deletions apollo-portal/src/main/resources/static/views/app/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,25 +107,6 @@
</tr>
</tbody>
</table>
<nav class="text-right">
<ul class="pagination">
<li>
<a aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<li><a >1</a></li>
<li><a >2</a></li>
<li><a >3</a></li>
<li><a >4</a></li>
<li><a >5</a></li>
<li>
<a aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<!--biz script-->
<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/services/ConfigService.js"></script>
<script type="application/javascript" src="../../scripts/services/VersionService.js"></script>
<script type="application/javascript" src="../../scripts/controller/app/AppConfigController.js"></script>
Expand Down

0 comments on commit 46d6e00

Please sign in to comment.