Skip to content

Commit

Permalink
fix docker env API_BASE
Browse files Browse the repository at this point in the history
  • Loading branch information
thundernet8 committed Dec 26, 2017
1 parent 85cf7f8 commit 1a9cdc7
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion env.js
Expand Up @@ -16,7 +16,7 @@ exports.API_SERVER_HOST = exports.IS_PROD ? "127.0.0.1" : "127.0.0.1";
exports.API_SERVER_PORT = exports.IS_PROD ? 8000 : 9000;
exports.API_BASE = exports.IS_PROD && !exports.IS_NODE
? "" + (location.protocol === "https:" ? "https://" : "http://") + location.host + "/api/"
: "http://" + exports.API_SERVER_HOST + ":" + exports.API_SERVER_PORT + "/api/";
: "http://" + (exports.IS_DOCKER ? "apinode" : exports.API_SERVER_HOST) + ":" + exports.API_SERVER_PORT + "/api/";
// WebSocket Server(Ratelimit realtime notify)
exports.WS_SERVER_HOST = exports.IS_PROD ? "127.0.0.1" : "127.0.0.1";
exports.WS_SERVER_PORT = exports.IS_PROD ? 8999 : 8999;
Expand Down
2 changes: 1 addition & 1 deletion env.ts
Expand Up @@ -23,7 +23,7 @@ export const API_SERVER_PORT = IS_PROD ? 8000 : 9000;
export const API_BASE =
IS_PROD && !IS_NODE
? `${location.protocol === "https:" ? "https://" : "http://"}${location.host}/api/`
: `http://${API_SERVER_HOST}:${API_SERVER_PORT}/api/`;
: `http://${IS_DOCKER ? "apinode" : API_SERVER_HOST}:${API_SERVER_PORT}/api/`;

// WebSocket Server(Ratelimit realtime notify)
export const WS_SERVER_HOST = IS_PROD ? "127.0.0.1" : "127.0.0.1";
Expand Down
3 changes: 2 additions & 1 deletion src/shared/api/WebApi.ts
Expand Up @@ -2,6 +2,7 @@ import { API_BASE, IS_PROD } from "../../../env";
import axios from "axios";
import https from "https";
import qs from "qs";
import GlobalStore from "../store/GlobalStore";

// axios.defaults.withCredentials = true;
export function webApi<T>(
Expand All @@ -21,7 +22,7 @@ export function webApi<T>(
};

const ax = axios.create({
baseURL: API_BASE,
baseURL: GlobalStore.API_BASE || API_BASE, // get docker ssr render api_base from GlobalStore
timeout: 300000,
withCredentials: true,
httpsAgent: new https.Agent({
Expand Down
2 changes: 2 additions & 0 deletions src/shared/store/GlobalStore.ts
Expand Up @@ -11,6 +11,8 @@ declare var window;
* 全局Store(单例)
*/
export default class GlobalStore extends AbstractStore {
public static API_BASE: string = "";

private static instance: GlobalStore;

public static get Instance() {
Expand Down
7 changes: 7 additions & 0 deletions ssr/render.ts
Expand Up @@ -8,6 +8,8 @@ import { URL } from "url";
import * as send from "koa-send";
import { lowerCaseFirst } from "../src/shared/utils/TextKit";
import { IStoreArgument } from "../src/shared/interface/IStoreArgument";
import { API_BASE } from "../env";

const App = require("../dist/assets/js/server").default;
const routes = require("../dist/assets/js/server").Routes;
const DocumentMeta = require("../dist/assets/js/server").SSRDocumentMeta;
Expand All @@ -34,6 +36,11 @@ export default async (ctx, next) => {
if (match) {
storeArg.match = match;
const storeClasses = route.component["STORE_CLASSES"];
storeClasses.forEach(clazz => {
if (clazz.name === "GlobalStore") {
clazz.API_BASE = API_BASE;
}
});
storeClasses &&
storeClasses.forEach((clazz: any) => {
if (clazz.instance) {
Expand Down
3 changes: 2 additions & 1 deletion webpack/ssr.conf.babel.js
Expand Up @@ -99,7 +99,8 @@ const loaders = [
let config = {
node: {
__filename: false,
__dirname: false
__dirname: false,
process: false
},
entry: {
server: [path.resolve(__dirname, "../src/app.tsx")]
Expand Down

0 comments on commit 1a9cdc7

Please sign in to comment.