Skip to content

Instantly share code, notes, and snippets.

@wisetc
Created December 19, 2017 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wisetc/eee4ce082696decce7efa572e28d5e97 to your computer and use it in GitHub Desktop.
Save wisetc/eee4ce082696decce7efa572e28d5e97 to your computer and use it in GitHub Desktop.
API service configuration of a vuejs project.
import settings from 'settings'
export const baseUrls = new ProjectSettings(settings, [
'ibasic-api', 'iqc-api'
]).baseUrls
class ProjectSettings {
constructor(settings, serviceNameList) {
this.settings = settings
this.serviceNameList = serviceNameList
this.generateBaseUrls()
}
getServiceBaseUrl(serviceName) {
let server = settings.SERVERS.find(item =>
item.SERVER_NAME === serviceName
)
return server ? `http://${server.HOST}:${server.PORT}/${serviceName}` : undefined
}
generateBaseUrls() {
let baseUrls = {}
this.serviceNameList.forEach(serviceName => {
baseUrls[serviceName] = this.getServiceBaseUrl((serviceName))
})
this.baseUrls = baseUrls
}
}
var settings = {
SERVERS: [
{
SERVER_NAME: 'ibasic-api',
HOST: '192.168.2.103',
PORT: 5555
},
{
SERVER_NAME: 'iqc-api',
HOST: '192.168.2.103',
PORT: 5555
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment