From dc4202ba3aba1e444dd943efba04ba85b4e01bca Mon Sep 17 00:00:00 2001 From: liugq Date: Tue, 2 Nov 2021 16:22:42 +0800 Subject: [PATCH] console and cors --- config/config.go | 25 ++++++++++--- main.go | 18 +++++++--- ui.go | 18 +++++++++- web/config/config.js | 4 +-- web/src/assets/elasticsearch.ico | Bin 0 -> 1529 bytes .../components/GlobalHeader/RightContent.js | 5 ++- web/src/components/GlobalHeader/index.js | 7 ++-- .../kibana/console/components/Console.tsx | 2 +- web/src/pages/DevTool/Console.tsx | 10 ++++-- web/src/pages/DevTool/console_tab_title.tsx | 4 +-- web/src/services/common.js | 33 ++++++++++++++++-- 11 files changed, 100 insertions(+), 26 deletions(-) create mode 100644 web/src/assets/elasticsearch.ico diff --git a/config/config.go b/config/config.go index b942f7d1..94d1e6d8 100644 --- a/config/config.go +++ b/config/config.go @@ -1,8 +1,25 @@ package config +import "infini.sh/framework/core/config" + type AppConfig struct { - Elasticsearch string `config:"elasticsearch"` - UILocalPath string `config:"ui_path"` - UILocalEnabled bool `config:"ui_local"` - UIVFSEnabled bool `config:"ui_vfs"` + Elasticsearch string `config:"elasticsearch"` + UI UIConfig `config:"ui"` + Network config.NetworkConfig `config:"network"` + TLSConfig config.TLSConfig `config:"tls"` +} + +type UIConfig struct { + Enabled bool `config:"enabled"` + LocalPath string `config:"path"` + LocalEnabled bool `config:"local"` + VFSEnabled bool `config:"vfs"` + APIEndpoint string `config:"api_endpoint"` +} + +func (config *AppConfig) GetSchema() string { + if config.TLSConfig.TLSEnabled { + return "https" + } + return "http" } diff --git a/main.go b/main.go index eb8033fa..14c9b7ff 100644 --- a/main.go +++ b/main.go @@ -3,9 +3,11 @@ package main import ( "errors" _ "expvar" + "fmt" "infini.sh/framework" "infini.sh/framework/core/elastic" "infini.sh/framework/core/env" + "infini.sh/framework/core/global" "infini.sh/framework/core/module" "infini.sh/framework/core/orm" "infini.sh/framework/modules" @@ -50,12 +52,14 @@ func main() { appConfig = &config.AppConfig{ Elasticsearch: "default", - UILocalPath: ".public", - UIVFSEnabled: true, - UILocalEnabled: true, + UI: config.UIConfig{ + LocalPath: ".public", + VFSEnabled: true, + LocalEnabled: true, + }, } - ok, err := env.ParseConfig("search-center", appConfig) + ok, err := env.ParseConfig("web", appConfig) if err != nil { panic(err) } @@ -74,6 +78,12 @@ func main() { // global.Env().SystemConfig.APIConfig.CrossDomain.AllowedOrigins= // append(global.Env().SystemConfig.APIConfig.CrossDomain.AllowedOrigins,uiConfig.NetworkConfig.GetBindingAddr()) //} + apiConfig := global.Env().SystemConfig.APIConfig + if len(apiConfig.CrossDomain.AllowedOrigins) == 0 { + apiConfig.CrossDomain.AllowedOrigins = []string{ + fmt.Sprintf("%s://%s", appConfig.GetSchema(), apiConfig.NetworkConfig.GetPublishAddr()), + } + } //start each module, with enabled provider module.Start() diff --git a/ui.go b/ui.go index 57935a9b..460cf2d1 100644 --- a/ui.go +++ b/ui.go @@ -2,7 +2,10 @@ package main import ( "fmt" + "infini.sh/framework/core/global" "net/http" + "src/github.com/segmentio/encoding/json" + "strings" public "infini.sh/search-center/.public" @@ -22,12 +25,25 @@ type UI struct { func (h UI) InitUI() { - vfs.RegisterFS(public.StaticFS{StaticFolder: h.Config.UILocalPath, TrimLeftPath: h.Config.UILocalPath, CheckLocalFirst: h.Config.UILocalEnabled, SkipVFS: !h.Config.UIVFSEnabled}) + vfs.RegisterFS(public.StaticFS{StaticFolder: h.Config.UI.LocalPath, TrimLeftPath: h.Config.UI.LocalPath, CheckLocalFirst: h.Config.UI.LocalEnabled, SkipVFS: !h.Config.UI.VFSEnabled}) ui.HandleUI("/", vfs.FileServer(vfs.VFS())) uiapi.Init(h.Config) + var apiEndpoint = h.Config.UI.APIEndpoint + if strings.TrimSpace(apiEndpoint) == "" { + apiConfig := &global.Env().SystemConfig.APIConfig + apiEndpoint = fmt.Sprintf("%s://%s", apiConfig.GetSchema(), apiConfig.NetworkConfig.GetPublishAddr()) + } + + ui.HandleUIFunc("/config", func(w http.ResponseWriter, req *http.Request){ + buf, _ := json.Marshal(util.MapStr{ + "api_endpoint": apiEndpoint, + }) + w.Write(buf) + }) + ui.HandleUIFunc("/api/", func(w http.ResponseWriter, req *http.Request) { log.Warn("api: ", req.URL, " not implemented") request, err := h.GetRawBody(req) diff --git a/web/config/config.js b/web/config/config.js index 1f1ea155..b5990902 100644 --- a/web/config/config.js +++ b/web/config/config.js @@ -50,7 +50,7 @@ export default { define: { APP_TYPE: process.env.APP_TYPE || '', ENV: process.env.NODE_ENV, - API_ENDPOINT: 'http://localhost:2900', + API_ENDPOINT: process.env.API_ENDPOINT || '', }, // 路由配置 routes: pageRoutes, @@ -125,7 +125,7 @@ export default { }, copy:[ - './src/assets/favicon.ico' + './src/assets/favicon.ico', ], history: 'hash', // exportStatic: { diff --git a/web/src/assets/elasticsearch.ico b/web/src/assets/elasticsearch.ico new file mode 100644 index 0000000000000000000000000000000000000000..41320478b5fef17af31f3261c6617d22cca65d01 GIT binary patch literal 1529 zcmVREi0WcRiT#RWTjTOu{z)4^*8c)r-^~F^H!=K;f3r8m!f!BdYfHi=0Yjg<; zxCDF+90Sfp<0<8|v*Q3R9FNrtq1&n>liLEp8+G3PyP}K$oj`juo*H%1p{N`ET`{R4 zf#wjfSx7B|p&JCNrw~Gy=&vmbI1B6n&KCPO3I@>MwZ2kou@l$_tXk+KAP|}&6kfI< z!1_1#0bPamM&1DWyJA&Zvln<1E2HG}3@(2Vfj5AvLZfRf$c(Sx##BW)oXtl7D~RoO z4|4)gngp;H2ss&DDJzI1Dd>8n7r<1X2$73?3gt09?%+%r~2#~+_b30W?&m0 zPgbP_*E4=1E3>6!C50t3MzLo7L*SQaJT;ZC!!1)L1cw(5k6ZOAz7DA77jxts@>okm{y6F1(^QIDlQ57c%K+3PPGq}FRrSqHvhMY8%z!k4nY!Lz#U4=G0A1xW6eXO-%+(~08gc$H@$rd3|HT57p z1uzX9!K%_ZLL$Uztm12VFe*^yTB~^E90d#mZNQD&C*EbO#9K04a0 zwSKJB0aw=4s9<%qR~>N`IFEG>j3g5+MbD_kxo#BLilxgvAq|5{zu#;3d8V;8c3)F( z&-=i)$wW)u3(mrMZy3Jcg%DkoaIIVs2;i$IxlUS%HHnV{$ARvq-kweQ8ZQJevSXJL zLL3Et_F~mA3_{^B!d?C^MgVvcIMLMGldx-Wi{iTU{O(a9@fiDe8pzk3IT;6TtVtP@%P+kCN9wQ*V!n6-*3x2y36zVgI?!2-f=k5x4*h fCKD|seQxs)FPFt*lz)b<00000NkvXXu0mjf2}sJe literal 0 HcmV?d00001 diff --git a/web/src/components/GlobalHeader/RightContent.js b/web/src/components/GlobalHeader/RightContent.js index 576d3117..b80d1c35 100644 --- a/web/src/components/GlobalHeader/RightContent.js +++ b/web/src/components/GlobalHeader/RightContent.js @@ -185,7 +185,7 @@ export default class GlobalHeaderRight extends PureComponent { bottomLeft: false, topLeft: false, }}> */} - 0 && - {/* */} + />} ); diff --git a/web/src/components/GlobalHeader/index.js b/web/src/components/GlobalHeader/index.js index ec015901..67f889ca 100644 --- a/web/src/components/GlobalHeader/index.js +++ b/web/src/components/GlobalHeader/index.js @@ -40,9 +40,6 @@ export default class GlobalHeader extends PureComponent { }; render() { const { collapsed, isMobile, logo, clusterVisible, clusterList, selectedCluster } = this.props; - if(clusterList.length == 0){ - return null - } return (
{isMobile && ( @@ -55,7 +52,8 @@ export default class GlobalHeader extends PureComponent { type={collapsed ? 'menu-unfold' : 'menu-fold'} onClick={this.toggle} /> - 0 && + }
); diff --git a/web/src/components/kibana/console/components/Console.tsx b/web/src/components/kibana/console/components/Console.tsx index e21af7ec..489c09d5 100644 --- a/web/src/components/kibana/console/components/Console.tsx +++ b/web/src/components/kibana/console/components/Console.tsx @@ -87,7 +87,7 @@ const ConsoleWrapper = ({ - + {lastDatum?.request.header} diff --git a/web/src/pages/DevTool/Console.tsx b/web/src/pages/DevTool/Console.tsx index 823573b4..c4d08384 100644 --- a/web/src/pages/DevTool/Console.tsx +++ b/web/src/pages/DevTool/Console.tsx @@ -139,10 +139,14 @@ export const ConsoleUI = ({selectedCluster, return cm; }, [clusterList, clusterStatus]) const initialDefaultState = ()=>{ - const defaultActiveKey = `${selectedCluster.id}:${new Date().valueOf()}`; - const defaultState = selectedCluster? { + let defaultCluster = selectedCluster; + if(!defaultCluster.id){ + defaultCluster = clusterList[0] ; + } + const defaultActiveKey = `${defaultCluster.id || ''}:${new Date().valueOf()}`; + const defaultState = defaultCluster? { panes:[{ - key: defaultActiveKey, cluster_id: selectedCluster.id, title: selectedCluster.name + key: defaultActiveKey, cluster_id: defaultCluster.id, title: defaultCluster.name }], activeKey: defaultActiveKey, }: {panes:[],activeKey:''}; diff --git a/web/src/pages/DevTool/console_tab_title.tsx b/web/src/pages/DevTool/console_tab_title.tsx index b92b2890..99121e07 100644 --- a/web/src/pages/DevTool/console_tab_title.tsx +++ b/web/src/pages/DevTool/console_tab_title.tsx @@ -1,10 +1,10 @@ import {useState, useRef, useEffect} from 'react'; import './console_tab_title.scss'; -import ElasticSvg from '@/assets/elasticsearch.svg'; +import ElasticImg from '@/assets/elasticsearch.ico'; import {Icon} from 'antd'; const ElasticIcon = () => ( - + ); interface TabTitleProps { diff --git a/web/src/services/common.js b/web/src/services/common.js index 16d80725..a8f15517 100644 --- a/web/src/services/common.js +++ b/web/src/services/common.js @@ -1,5 +1,34 @@ -export const pathPrefix = (API_ENDPOINT || '') + '/_search-center'; +import $ from 'jquery'; +function getConfig(){ + const options = { + url: "/config", + cache: false, + type: 'GET', + dataType: 'json', // disable automatic guessing + async: false, + }; + let result = {} + try{ + const text = $.ajax(options).responseText; + result = JSON.parse(text); + }catch(e){ + console.warn('failed get config data') + } + return result; + +} +const {api_endpoint} = getConfig(); + +let apiEndpoint = api_endpoint; +if(!apiEndpoint){ + apiEndpoint = API_ENDPOINT; + if(!API_ENDPOINT){ + apiEndpoint = `${location.protocol}//${location.hostname}:2900` + } +} + +export const pathPrefix = (apiEndpoint || '') + '/_search-center'; export function buildQueryArgs(params){ let argsStr = ''; for(let key in params){ @@ -14,4 +43,4 @@ export function buildQueryArgs(params){ return argsStr; } -export const ESPrefix = (API_ENDPOINT || '') + '/elasticsearch'; \ No newline at end of file +export const ESPrefix = (apiEndpoint || '') + '/elasticsearch'; \ No newline at end of file