开发者

How do I have sbt's Jetty use a local domain name?

开发者 https://www.devze.com 2023-02-14 06:39 出处:网络
I\'d like the version of Je开发者_StackOverflowtty launched by sbt> ~jetty to listen on my.name.local, which I\'ve set to 127.0.0.1 in /etc/hosts. It seems to be possible to change Jetty\'s setting

I'd like the version of Je开发者_StackOverflowtty launched by sbt> ~jetty to listen on my.name.local, which I've set to 127.0.0.1 in /etc/hosts. It seems to be possible to change Jetty's settings from within sbt.

Here's what I have for my project:

import sbt._

class LiftProject(info: ProjectInfo) extends DefaultWebProject(info) {
  // ...

  val jetty = "org.eclipse.jetty" % "jetty-webapp" % "7.3.0.v20110203" % "test"

  override lazy val jettyInstance = new JettyRunner(customJettyConfiguration) 

  def customJettyConfiguration = { 
    val myLog = log 
    val myJettyClasspath = jettyClasspath 
    val myScanDirectories = scanDirectories
    val myScanInterval = scanInterval

    new CustomJettyConfiguration { 
      def classpath = jettyRunClasspath
      def jettyClasspath = myJettyClasspath
      def war = jettyWebappPath
      def contextPath = jettyContextPath
      def classpathName = "test"
      def parentLoader = buildScalaInstance.loader
      def scanDirectories = Path.getFiles(myScanDirectories).toSeq
      def scanInterval = myScanInterval
      def port = jettyPort
      def log = myLog      
      override def jettyConfigurationXML = 
        <Configure class="org.eclipse.jetty.webapp.WebAppContext">
          <Set name="virtualHosts">
            <Array type="java.lang.String">
              <Item>my.name.local</Item>
            </Array>
          </Set>
        </Configure>
    }
  }
}

While it seems to launch without complaints, visiting my.name.local doesn't hit Jetty as far as I can tell.


Rather than running sbt as root (dangerous), I personally prefer rerouting port 80 to 8080 using iptables on Linux :

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

Which works only until the next reboot. To make the setting persistent on Ubuntu 10.04, I use :

sudo bash -c "iptables-save > /etc/iptables.rules"
echo "#!/bin/sh
iptables-restore < /etc/iptables.rules
exit 0
" > /etc/network/if-pre-up.d/iptablesload
echo "#!/bin/sh
iptables-save -c > /etc/iptables.rules
if [ -f /etc/iptables.downrules ]; then
   iptables-restore < /etc/iptables.downrules
fi
exit 0
" > /etc/network/if-post-down.d/iptablessave
chmod +x /etc/network/if-post-down.d/iptablessave
chmod +x /etc/network/if-pre-up.d/iptablesload

(see this Ubuntu iptables wiki)


I posted too soon. All I need to do is override jettyPort:

override def jettyPort = 80

And run sbt via sudo.

0

精彩评论

暂无评论...
验证码 换一张
取 消