Giter Site home page Giter Site logo

gitter-badger / sans-server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bclemenzi/sans-server

0.0 1.0 0.0 329 KB

SansServer is a small web application implemented with a server-less architecture. It uses Amazon API Gateway to expose the Lambda function as HTTP endpoints and uses Identity and Access Management (IAM) and Amazon Cognito to retrieve temporary credentials for a user and authorize access to its APIs with. Amazon DynamoDB is used for the application's persistent storage system.

License: GNU General Public License v2.0

Java 76.86% HTML 20.75% CSS 2.40%

sans-server's Introduction

SansServer

SansServer is a small web application implemented with a server-less architecture. It uses Amazon API Gateway to expose the Lambda functions as HTTP endpoints and uses Identity and Access Management (IAM) with Amazon Cognito to retrieve temporary credentials for a user and authorize access to its APIs with. Amazon DynamoDB is used for the application's persistent storage system.

The goal of this project was to create a prototype that would allow you to work towards providing a cost-efficient solution that is scalable and highly available. Please keep in mind, this project has been written as an example and therefore should not be considered production ready. It has; however, tried to follow a few best practices to give you a fairly clean development framework to build on top of.

Features

  • Java-based Lambda Functions
  • SAP Web application front-end with Unauthenticated and Authenticated pages
  • Project properties file used at runtime
  • Configuration schema to allow for the isolation of multiple deployments: Multi-Engineer Development, QA, Production
  • JUnit test harness for locally testing your Lambda functions before deployment
  • Uses SansServer-Plugin (https://github.com/bclemenzi/sans-server-plugin) to build and deploy application to Amazon Web Services

Demo Site

http://sansserver.nfbsotware.com :: COMING SOON

Getting started

The SansServer application bases itself off the source code layout defined by the sans-server-plugin (https://github.com/bclemenzi/sans-server-plugin). The plugin provides a number of wrapper classes and utilities that were developed to make working with Amazon Web Services easier.

The SansServer application uses the sans-server-plugin to build, test, provision, and deploy the webapp to AWS. The following are recommended Maven goals to use during your development:

  • mvn clean package
  • Clean and package should be used to build our source code and execute our JUnit test cases.
  • mvn clean package shade:shade install
  • Adding shade:shade will package (JAR) our Java class files for Lambda deployment within Amazon Web Services. The install goal performs the AWS configurations and deploys our code for use in the cloud. Please see the sans-server-plugin project (https://github.com/bclemenzi/sans-server-plugin) to learn more about the available features found inside the plugin.

SansServer Framework

This project contains a number of java classes that try to help simplify the authoring of lambda functions, including a base handler (BaseLambdaHandler) to configure your system and collect your request parameters. The BaseLambdaHandler also defines a simple response format to allow the client-side webapp to determine the status of any given transaction. Below is an example of a basic Lambda function that utilizes this framework to view a user record within the system:

package com.nfbsoftware.sansserver.user.lambda;

import com.nfbsoftware.sansserverplugin.sdk.annotation.AwsLambda;
import com.nfbsoftware.sansserverplugin.sdk.lambda.BaseLambdaHandler;
import com.nfbsoftware.sansserverplugin.sdk.lambda.model.HandlerResponse;
import com.nfbsoftware.sansserverplugin.sdk.util.StringUtil;
import com.nfbsoftware.sansserver.user.dao.UserDao;
import com.nfbsoftware.sansserver.user.model.User;

/**
 * The ViewUser function simply returns the user record requested.  The defined "handlerMethod" within the 
 * class annotation is found in the BaseLambdaHandler that this class extends.  The BaseLambdaHandler class 
 * provides a number of convenience systems to make authoring Java-base Lambda functions easier.
 * 
 * @author Brendan Clemenzi
 */
@AwsLambda(name="ViewUser", desc="Function to view a given user record", handlerMethod="handleRequest")
public class ViewUser extends BaseLambdaHandler
{
    /**
     * 
     * @return
     * @throws Exception
     */
    @Override
    public HandlerResponse processRequest() throws Exception
    {
        HandlerResponse handlerResponse = new HandlerResponse();
        
        try
        {
        	//  Get our request parameter
            String username = StringUtil.emptyIfNull(this.getParameter("username"));
            
            // Init out User DAO for talking with DynamoDB
            UserDao userDao = new UserDao(this.m_properties);
            
            m_logger.log("Get user record by username: " + username);
            User user = userDao.getUser(username);
            
            if(user != null)
            {
                // Add the model to the response map
                handlerResponse.getData().put("user", user);
                
                // Set our process status
                handlerResponse.setStatus(HandlerResponse.StatusKeys.SUCCESS);
                handlerResponse.setStatusMessage("");
            }
            else
            {
                throw new Exception("User record not found");
            }
        }
        catch (Exception e)
        {
            m_logger.log("Error processing request: " + e.getMessage());
            
            handlerResponse.setStatus(HandlerResponse.StatusKeys.FAILURE);
            handlerResponse.setStatusMessage(e.getMessage());
        }
        
        return handlerResponse;
    }
}

The JUnit class for testing the ViewUser Lambda function looks as follows:

package com.nfbsoftware.sansserver.user.lambda;

import java.io.IOException;
import java.util.LinkedHashMap;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import com.amazonaws.services.lambda.runtime.Context;
import com.nfbsoftware.sansserverplugin.sdk.junit.TestContext;
import com.nfbsoftware.sansserverplugin.sdk.lambda.model.HandlerResponse;
import com.nfbsoftware.sansserver.user.lambda.ViewUser;
import com.nfbsoftware.sansserver.user.model.User;

/**
 * A simple test harness for locally invoking your Lambda function handler.
 * 
 * @author Brendan Clemenzi
 */
public class ViewUserTest
{
    private static LinkedHashMap<String, String> input;

    @BeforeClass
    public static void createInput() throws IOException
    {
        input = new LinkedHashMap<String, String>();
        input.put("username", "[email protected]");
    }

    private Context createContext()
    {
        TestContext ctx = new TestContext();

        ctx.setFunctionName("View User");

        return ctx;
    }

    @Test
    public void testViewUser()
    {
        ViewUser handler = new ViewUser();
        Context ctx = createContext();

        HandlerResponse output = (HandlerResponse)handler.handleRequest(input, ctx);

        if(output.getStatus().equalsIgnoreCase("SUCCESS")) 
        {
            User userValue = (User)output.getData().get("user");
            
            System.out.println("Full Name: " + userValue.getFullName());
            
            System.out.println("ViewUser JUnit Test Passed");
        }
        else
        {
            Assert.fail("ViewUser JUnit Test Failed");
        }
    }
}

sans-server's People

Contributors

bclemenzi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.