Mixed Windows and *nix environment - 'local' suffix issue
Stupid question - apologies in advance.
I have a mixed Windows and Linux dev environment, and the server is Linux (Ubuntu). Let's say the server is called 'myserver'.
I develop on both Windows and *nix (Ubuntu and OS X), with references to myserver as 'myserver.local' on *nix, and 'myserver' on Windows.
I have shared code references (e.g. MySQL connection strings, HTTP remoting references etc) which I have to change depending on which dev env I'm in, which is n开发者_运维问答uts. For the life of me I can't work out how to force Windows to alias 'myserver.local' to 'myserver' and make the world a better place.
Any ideas?
Many thanks, Ned
somewhere in /system32 is a file called hosts where you can enter host names to your heart's content, those names will be respected by the resolver. Should fix your problem.
Windows has a hosts
file you can change to make myserver.local
resolve to 127.0.0.1
, just the same as Linux. It's just hidden in Windows\System32\drivers\etc\HOSTS
. But if you always want to use the local server for your references, why not simply write localhost
?
In general though you should aim to keep your deployment settings apart from your application rather than sharing them. It would depend on what language/platform you're talking about of course, but in my case using Python I use a script to invoke the application something like:
hostname= socket.gethostname()
if hostname=='devbox':
def dbfactory():
return MySQLdb.connect(db= 'myapp', user= 'foo', passwd= 'bar')
myapp= mymodule.Application(dbfactory, debug= True)
elif hostname=='www':
def dbfactory():
return MySQLdb.connect(db= 'myapplive', unix_socket= '/usr/local/var/mysql/socket', user= 'baz')
myapp= mymodule.Application(dbfactory, debug= False)
else:
raise NotImplementedError('No known deployment config for machine %r' % hostname)
myapp.run()
Alternate answer:
I once had similar problems and got tired of having to reconfigure my app all the time. My solution there was similar to what bonince is suggesting:
The idea is to have your app configure itself differently depending on where it is. I wouldn't recommend "sniffing," though. I'd prefer something more "official" and "specific." To wit, there should be some data found in the same place on both machines but with different contents. Two possibilities come to mind:
Environment variables. They're deprecated in Java, but they work fine under Linux and Windows. If you have access to to the startup scripts, you can set whatever you like, and query the values from Java.
Properties/configuration files outside the project itself. I used a directory called /local (linux) and C:\Local (Windows) to contain bits and pieces of configuration that I wanted to be host specific. The home directory also works fine for this kind of thing; every host has one, and Java generally knows how/where to find it.
精彩评论