Skip to content

Commit

Permalink
add urls section for quick service access.
Browse files Browse the repository at this point in the history
  • Loading branch information
wildone committed Aug 12, 2024
1 parent d09efa4 commit a71f409
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 40 deletions.
107 changes: 69 additions & 38 deletions electron/app/main/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export interface ExecConfig {
serviceportdebug?: number
servicehost?: string
portmapping?: { [key: string]: number }
urls?: { [key: string]: string }
healthcheck?: HealthCheck
debuglog?: boolean
outputvarregex?: { [key: string]: string } // used to check values of console output stdout key is variable, value is regex
Expand Down Expand Up @@ -252,6 +253,7 @@ export class Service extends EventEmitter<ServiceEvent> {
#servicehost: string = DEFAULT_SERVICE_HOST
#serviceManager: ServiceManager
#portmapping: { [key: string]: number } = {}
#urls: { [key: string]: string } = {}
#stdout: WriteStream
#stdoutLogFile: string
#stderr: WriteStream
Expand Down Expand Up @@ -414,6 +416,8 @@ export class Service extends EventEmitter<ServiceEvent> {
//get port mapping from config
this.#portmapping = this.#options.execconfig?.portmapping || {}

//get service urls from config
this.#urls = this.#options.execconfig?.urls || {}

const config = options.execconfig
if (config) {
Expand Down Expand Up @@ -655,53 +659,68 @@ export class Service extends EventEmitter<ServiceEvent> {
command = command.replaceAll(`\${${key}}`, value)
}

//quick exit if no variables in command
if (!this.isStringHasVariables(command)) {
return command
}

for (const [key, value] of Object.entries(this.#processEnv)) {
command = command.replaceAll(`\${${key}}`, value)
}

//quick exit if no variables in command
if (!this.isStringHasVariables(command)) {
return command
}


for (const [key, value] of Object.entries(this.#portmapping)) {
command = command.replaceAll(`\${${key}}`, value + "")
}

//quick exit if no variables in command
if (!this.isStringHasVariables(command)) {
return command
}

const serviceCommandParams = this.#getServiceCommandParams(service)
for (const [key, value] of Object.entries(serviceCommandParams)) {
command = command.replaceAll(`\${${key}}`, value)
//quick exit if no variables in command
if (!this.isStringHasVariables(command)) {
break
}
}

return command
.replaceAll("${PS}", this.isWindows ? ";" : ":") //path separator
.replaceAll("${SERVICE_HOME}", service.#servicehome)
.replaceAll(
"${SERVICE_HOME_ESC}",
this.isWindows
? service.#servicehome.replaceAll("\\", "\\\\")
: service.#servicehome
) // escape backslashes for windows
.replaceAll("${SERVICE_EXECUTABLE}", service.getServiceExecutable())
.replaceAll(
"${SERVICE_EXECUTABLE_HOME}",
service.getServiceExecutable(true)
)
.replaceAll(
"${SERVICE_EXECUTABLE_CLI}",
service.getServiceExecutableCli()
)
.replaceAll("${SERVICE_PATH}", service.#servicepath)
.replaceAll("${SERVICE_BIN_PATH}", service.#servicebinpath)
.replaceAll("${EXEC_SERVICE_PATH}", service.#execservicepath)
.replaceAll("${SERVICE_DATA_PATH}", service.#servicedatapath)
.replaceAll(
"${SERVICE_DATA_PATH_ESC}",
this.isWindows
? service.#servicedatapath.replaceAll("\\", "\\\\")
: service.#servicedatapath
) // escape backslashes for windows
.replaceAll("${SERVICE_HOST}", service.#servicehost + "")
.replaceAll("${SERVICE_LOG_PATH}", service.#logsDir + "")
.replaceAll(
"${SERVICE_LOG_PATH_ESC}",
this.isWindows
? (service.#logsDir + "").replaceAll("\\", "\\\\")
: service.#logsDir + ""
)
.replaceAll("${SERVICE_AUTH_USERNAME}", service.username)
.replaceAll("${SERVICE_AUTH_PASSWORD}", service.password)
.replaceAll("${SERVICE_PID_FILE}", service.#servicepidfile)
}

#getServiceCommandParams(service: Service): { [key: string]: string } {
return {
"PS": this.isWindows ? ";" : ":",
"SERVICE_HOME": service.#servicehome,
"SERVICE_HOME_ESC": this.isWindows
? service.#servicehome.replaceAll("\\", "\\\\")
: service.#servicehome,
"SERVICE_EXECUTABLE": service.getServiceExecutable(),
"SERVICE_EXECUTABLE_HOME": service.getServiceExecutable(true),
"SERVICE_EXECUTABLE_CLI": service.getServiceExecutableCli(),
"SERVICE_PATH": service.#servicepath,
"SERVICE_BIN_PATH": service.#servicebinpath,
"EXEC_SERVICE_PATH": service.#execservicepath,
"SERVICE_DATA_PATH": service.#servicedatapath,
"SERVICE_DATA_PATH_ESC": this.isWindows
? service.#servicedatapath.replaceAll("\\", "\\\\")
: service.#servicedatapath,
"SERVICE_HOST": service.#servicehost + "",
"SERVICE_LOG_PATH": service.#logsDir + "",
"SERVICE_LOG_PATH_ESC": this.isWindows
? (service.#logsDir + "").replaceAll("\\", "\\\\")
: service.#logsDir + "",
"SERVICE_AUTH_USERNAME": service.username,
"SERVICE_AUTH_PASSWORD": service.password,
"SERVICE_PID_FILE": service.#servicepidfile,
}
}

get environmentVariables(): { [key: string]: string } {
Expand Down Expand Up @@ -2761,6 +2780,18 @@ export class Service extends EventEmitter<ServiceEvent> {
return this.#portmapping
}

getURLs() {
// for each url in urls, compile the url
let urls = {}
if (this.#urls && Object.keys(this.#urls).length > 0) {
//for each url in urls, compile the url
Object.keys(this.#urls).forEach((url) => {
urls[url] = this.#getServiceCommand(this.#urls[url], this)
})
}
return urls
}

/**
* check if current PID active and matches service executable
*/
Expand Down
12 changes: 12 additions & 0 deletions electron/app/main/ServiceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,18 @@ class ServiceManager {
return this.#servicePorts
}

getURLs(): { [key: string]: string } {
//for each service get service urls
const urls: { [key: string]: string } = {}
this.#services.forEach((service: Service) => {
const serviceUrls = service.getURLs()
Object.keys(serviceUrls).forEach((key: string) => {
urls[key] = serviceUrls[key]
})
})
return urls
}

// get list of all seervices
getServices(): Service[] {
return this.#services
Expand Down
41 changes: 39 additions & 2 deletions electron/app/main/Services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ function getServicePage(service: Service) {
</tr>`
}

const urls = service.getURLs()
let urlsList = ""
if (Object.keys(urls).length < 0) {
urlsList = "No service URLs configured."
} else {
for (const url in urls) {
const urlData = urls[url]
urlsList += `<a class="btn btn-primary" role="button" target="_blank" href="${urlData}">${url.toUpperCase()}</a>`
}
}

const execservice = service.options.execconfig?.execservice?.id
? " (" + service.options.execconfig?.execservice.id + ")"
: ""
Expand Down Expand Up @@ -185,6 +196,13 @@ function getServicePage(service: Service) {
</div>
</div>
<div class="my-3 p-3 bg-body rounded shadow-sm">
<h6 class="border-bottom pb-2 mb-3">Urls</h6>
<div class="input-group input-group-sm mb-1 gap-2">
${urlsList}
</div>
</div>
<div class="my-3 p-3 bg-body rounded shadow-sm">
<h6 class="border-bottom pb-2 mb-3">Config</h6>
<div class="input-group input-group-sm mb-1">
Expand Down Expand Up @@ -542,7 +560,7 @@ function getServiceDependencies(filterServices: string[] = []) {
return serviceDependencies
}

function getServicesPage(services: Service[], ports: { [key: string]: ReservedPort } ) {
function getServicesPage(services: Service[], ports: { [key: string]: ReservedPort }, urls: { [key: string]: string }) {
const serviceDependencies = getServiceDependencies()
// console.log("ports", JSON.stringify(ports))
let portsList = ""
Expand All @@ -557,6 +575,17 @@ function getServicesPage(services: Service[], ports: { [key: string]: ReservedPo
</tr>`
}

//map urls into string buttons
let urlsList = ""
if (Object.keys(urls).length < 0) {
urlsList = "No service URLs configured."
} else {
for (const url in urls) {
const urlData = urls[url]
urlsList += `<a class="btn btn-primary" role="button" target="_blank" href="${urlData}">${url.toUpperCase()}</a>`
}
}

let servicesList = services
.map((service) => {
const execservice = service.options.execconfig?.execservice?.id
Expand Down Expand Up @@ -659,6 +688,13 @@ function getServicesPage(services: Service[], ports: { [key: string]: ReservedPo
</div>
</div>
<div class="my-3 p-3 bg-body rounded shadow-sm">
<h6 class="border-bottom pb-2 mb-3">Urls</h6>
<div class="input-group input-group-sm mb-1 gap-2">
${urlsList}
</div>
</div>
<div class="my-3 p-3 bg-body rounded shadow-sm">
<table class="table table-hover">
<thead class="table-light">
Expand Down Expand Up @@ -923,7 +959,8 @@ app.post("/exit", function (req, res, next) {
app.get("/services", function (req, res) {
const services = serviceManager.getServices()
const ports = serviceManager.getPorts()
res.send(getServicesPage(services, ports))
const urls = serviceManager.getURLs()
res.send(getServicesPage(services, ports, urls))
})

app.post("/service/:serviceId/:serviceAction", (req, res, next) => {
Expand Down

0 comments on commit a71f409

Please sign in to comment.