AddThis

Showing posts with label server. Show all posts
Showing posts with label server. Show all posts

Friday, October 7, 2016

The Rise of the Phoenix

Phoenix For the Rails People

I've been doing Rails for a long while now. Before Rails I was doing Enterprise Java but then my mentor at the time showed me this cool framework that took convention over configuration seriously and allowed really powerful things to be built quickly and easily. I was amazed and have been using tools like this ever since.

But the big argument against Rails is that it doesn't scale and is a bit bloated. Also there is a bit of a shift towards using more functional approaches. Enter my fascination with Elixir and Phoenix.

The next series of posts will be all about me learning Phoenix, but with a slant towards comparing and contrasting it against Rails. So strap in and take the journey with me.

I learn by doing, so I'll be building my normal 'hello world' type application, which is a library application. I used to spend a lot of time in libraries as a kid so I think thats the reason my default application is usually an application to track and catalog books. So let's get started.

Prerequisites

Before getting started, you will need to install a few things. I'll assume you are on mac and have homebrew installed. First is postgres.


brew update
brew install postgres
createuser -P -s -e root
# then setup your root user or whatever you want your user to be called

Next install Elixir and mix.


brew install elixir 
mix local.hex

Now install Phoenix.


mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez

Now we are ready to start building our app.

New Application

We will start first by creating the application. In rails we'd do something like:


rails new rails_library --database=postgresql

but in Phoenix we do:

mix phoenix.new phoenix_library

First thing to note here is `mix`, which is Elixir's build tool. Already a little different than the rails command. Also notice we run `phoenix.new` which really is nothing more than just a script that we run with mix. Lastly we pass it the name of the new project. Also notice that postgres is the default, so we don't have to tell it that we will be using it.

This command created a bunch of folders full of stuff, which we will get to as we need....which is right now. :) Open up the `dev.exs` file in the config dir and modify it as necessary to use the correct username and password to connect to your locally running postgres instance. Go ahead and do that for the `test.exs` file as well.

With that done, we can set up your db schemas by running:


mix ecto.create

# in rails we would do `rake db:create`

Very similar to rails, but instead of the rake command we use our Elixir build tool called mix, and we run the ecto.create script. Ecto is the database wrapper, so we will be seeing `Ecto` a lot.

The last thing we will do in our very gentle intro is to start the Phoenix server.


mix phoenix.server

# in rails we would do `rails server`

Then navigate to localhost:4000 and you will see the dummy page.

Next time we will be creating some models, views, and controllers. Until then!

Friday, August 26, 2011

Run Jetty Externally and Connect Remotely With Eclipse

There's another alternative to embedding Jetty inside Eclipse.  We can also use Eclipse to use Maven to start Jetty for us.  Then we can remotely connect and debug/manage it.  It's your choice as to which option you prefer.


Configure Maven to run Jetty

<plugin>
        <groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.24</version>
<configuration>
 <connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                  <!-- Use whatever you want the http port to be -->
 <port>8080</port>  
</connector>
 </connectors>
 <userRealms>
<userRealm implementation="org.mortbay.jetty.security.HashUserRealm">
 <name>default</name>
 <config>src/main/resources/jetty-realm.properties</config>
</userRealm>
 </userRealms>
          <!-- If you plan on changing this file during execution, turn the scan interval higher than 0 -->
 <scanIntervalSeconds>0</scanIntervalSeconds>
 <webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml>
</configuration>
</plugin>

You will need a jetty-realm.properties file and a webdefault.xml file.

jetty-realm.properties:
user1: pass1,role1
user2: pass2,role2

webdefault.xml:
Mine looks like the "JSP configuration" section



Setup Start Server Command
  1. Run -> External Tools -> External Tools Configuration 
  2. Right Click Program -> New
    1. Name: Whatever
    2. Location: Browse to your mvn executable
    3. Working Directory: click Browse Workspace button and select ${YOUR_WEB_PROJECT}
    4. Arguments: jetty:run
  3. Click Environment
    1. Click New
    2. Enter a variable names MAVEN_OPTS and set it to "-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"


Setup Remote Debugging
  1. Run -> Debug Configurations
  2. Right Click Remote Java Application -> New
    1. Name: Whatever
    2. Project: ${YOUR_WEB_PROJECT}
    3. Connection Type: Standard (Socket Attach)
    4. Host: localhost
    5. Port: 8787
  3. Also click "Allow termination of remote VM".  It's going to be the only way to elegantly kill your Jetty server without opening the task manager.


Usage
  1. Start the server using the external tools 
  2. Start the remote debugging once the server is up
  3. Open up the debug perspective -> Right click the VM and click terminate to kill the server

Setting up Jetty to run inside of Eclipse

Jetty is a great lightweight server that I'm using to do both my frontend and backend development.  For the focus of this tutorial, we will have a frontend slant.  This tutorial in particular will cover how to setup Eclipse (Helios) to deploy my webapp to Jetty so that I can quickly see my UI tweaks and changes.

Install Jetty
  1. Help -> Install New Software
  2. Work with: http://download.eclipse.org/jetty/updates/jetty-wtp
  3. Click Add
  4. Install Jetty and restart Eclipse

Prepare Your Web Project
  1. cd ${YOUR_WEB_PROJECT}
  2. mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true
  3. Import your project into eclipse as an existing project
  4. Right click your web project and go to Project Facets
  5. Click Dynamic Web Module
  6. Select Deployment Assembly
  7. Make sure your Deploy Path: / is the directory one above where WEB-INF lives.
    1. If not, go to Add... -> Folder and select WEB-INF's parent folder (It'll be something like webapp, or WebContent).
  8. Now click Add.. -> Java Build Path Entries
  9. Select them all using shift for multiselect and click ok


Prepare Jetty and Eclipse
  1. Click Window -> Show view -> Servers
  2. Right click the servers view -> New -> Server
  3. Select Jetty 8 or whatever you fancy
    1. Server's host name: localhost
    2. Server's name: Whatever you want
    3. Server runtime environment: click the add button on the right
      1. Name: Whatever
      2. Jetty installation directory: click the Download and Install button to the right -> Accept agreement -> select a location to download
      3. This is important....Wait a bit for the download to complete (look at the eclipse progress in the lower right and wait for it to complete.  
      4. When done, set the Jetty installation directory to the directory you just downloaded and installed jetty to
    4. JRE: Don't let this fool you, configure it to point to a jdk, not a jre.  
      1. If you don't have one, dl it and configure it using the Installed JREs... button
  4. Click Next
  5. Select your ${YOUR_WEB_PROJECT} to add as a deployment
  6. Click Finish


Setup Realms for Login
  1. Open up etc/realm.properties and add in your favorite user and passwords (USER1: password,role1,role2)
  2. Save and close
    1. If you edit this file later to add more users, remember to clean and publish for the changes to be picked up.  You do this by right clicking the server in the servers view and selecting clean.  After its finished, right click it again and select republish/publish.


Setup Ports
  1. Double click the server you just created to open up its configuration
  2. Select the Overview tab
  3. Look for the Ports section
  4. Click HTTP and change to whatever port you want


Usage
Right Click the server in the Servers view to manage the server.  You'll be able to start, stop, restart, and debug from here.


And thats's pretty much it.  Super easy and straightforward.