Skip to content

Commit

Permalink
initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
rofrano committed Mar 5, 2020
1 parent 74a9f36 commit 7de920f
Show file tree
Hide file tree
Showing 13 changed files with 459 additions and 47 deletions.
6 changes: 6 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# .coveragerc
[run]
source=service

[report]
show_missing = True
69 changes: 22 additions & 47 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
# Local OS metadata
.DS_Store
Thumbs.db

# VS Code
.vscode/

# Vagrant
.vagrant

# databases
*.db
db/*
!db/.keep

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand All @@ -8,6 +23,7 @@ __pycache__/

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
Expand All @@ -19,13 +35,9 @@ lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
Expand All @@ -40,16 +52,13 @@ pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
*,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
Expand All @@ -58,8 +67,6 @@ coverage.xml
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
Expand All @@ -74,56 +81,24 @@ docs/_build/
# PyBuilder
target/

# Jupyter Notebook
# IPython Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
# celery beat schedule file
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
# dotenv
.env
.venv
env/

# virtualenv
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,42 @@
# project-template

This is a skeleton you can use to start your projects

## Overview

This project template contains starter code for your class project. The `/service` folder contains your `models.py` file for your model and a `service.py` file for your service. The `/tests` folder has test case starter code for testing the model and the service separately. All you need to do is add your functionality. You can use the [lab-flask-rest](https://github.com/nyu-devops/lab-flask-rest)
for code examples.

## Setup

You should clone this repository and and the copy and paste the starter code into your project repo folder on your local computer.

Assuming your repo is empty, you can use the following commands:

```shell
git clone https://github.com/nyu-devops/project-template.git
cp -R project-template/ <your_repo_folder>/
```

This will recursively copy the contents of the template to your repo folder.

## Contents

The project contains the following:

```text
dot-env-example - copy to .env to use environment variables
config.py - configuration parameters
service/ - service python package
├── __init__.py - package initializer
├── models.py - module with business models
└── service.py - module with service routes
tests/ - test cases package
├── __init__.py - package initializer
├── test_models.py - test suite for busines models
└── test_service.py - test suite for service routes
```

This repository is part of the NYU class **CSCI-GA.2810-001: DevOps and Agile Methodologies** taught by John Rofrano, Adjunct Instructor, NYU Curant Institute, Graduate Division, Computer Science.
81 changes: 81 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "ubuntu/bionic64"
config.vm.hostname = "project"

# accessing "localhost:8080" will access port 80 on the guest machine.
# config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 5000, host: 5000, host_ip: "127.0.0.1"

# Create a private network, which allows host-only access to the machine
# using a specific IP.
config.vm.network "private_network", ip: "192.168.33.10"

# Mac users can comment this next line out but
# Windows users need to change the permission of files and directories
# so that nosetests runs without extra arguments.
config.vm.synced_folder ".", "/vagrant", mount_options: ["dmode=775,fmode=664"]

# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
config.vm.provider "virtualbox" do |vb|
# Customize the amount of memory on the VM:
vb.memory = "1024"
vb.cpus = 2
# Fixes some DNS issues on some networks
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
end

# Copy your .gitconfig file so that your git credentials are correct
if File.exists?(File.expand_path("~/.gitconfig"))
config.vm.provision "file", source: "~/.gitconfig", destination: "~/.gitconfig"
end

# Copy the ssh keys into the vm for git access
if File.exists?(File.expand_path("~/.ssh/id_rsa"))
config.vm.provision "file", source: "~/.ssh/id_rsa", destination: "~/.ssh/id_rsa"
end

if File.exists?(File.expand_path("~/.ssh/id_rsa.pub"))
config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/id_rsa.pub"
end

# Copy your .vimrc file so that your vi looks like you expect
if File.exists?(File.expand_path("~/.vimrc"))
config.vm.provision "file", source: "~/.vimrc", destination: "~/.vimrc"
end

# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y git python3 python3-pip python3-venv
apt-get -y autoremove
# Install app dependencies
cd /vagrant
pip3 install -r requirements.txt
SHELL

######################################################################
# Add PostgreSQL docker container
######################################################################
# docker run -d --name postgres -p 5432:5432 -v psql_data:/var/lib/postgresql/data postgres
config.vm.provision :docker do |d|
d.pull_images "postgres:alpine"
d.run "postgres:alpine",
args: "-d --name postgres -p 5432:5432 -v psql_data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=postgres"
end

end
14 changes: 14 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
Global Configuration for Application
"""
import os

# Get configuration from environment
DATABASE_URI = os.getenv("DATABASE_URI", "sqlite:///../development.db")

# Configure SQLAlchemy
SQLALCHEMY_DATABASE_URI = DATABASE_URI
SQLALCHEMY_TRACK_MODIFICATIONS = False

# Secret for session management
SECRET_KEY = os.getenv("SECRET_KEY", "s3cr3t-key-shhhh")
2 changes: 2 additions & 0 deletions dot-env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copy this file to .env to expose these environment variables
FLASK_APP=service:app
12 changes: 12 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Runtime dependencies
Flask==1.1.1
Flask-API==1.1
Flask-SQLAlchemy==2.4.1
python-dotenv==0.10.3

# Testing
nose==1.3.7
rednose==1.3.0
pinocchio==0.4.2
coverage==4.5.4
pylint>=2.4.1
42 changes: 42 additions & 0 deletions service/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
Package: service
Package for the application models and service routes
This module creates and configures the Flask app and sets up the logging
and SQL database
"""
import os
import sys
import logging
from flask import Flask

# Create Flask application
app = Flask(__name__)
app.config.from_object('config')

# Import the rutes After the Flask app is created
from service import service, models

# Set up logging for production
if __name__ != '__main__':
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
app.logger.propagate = False
# Make all log formats consistent
formatter = logging.Formatter("[%(asctime)s] [%(levelname)s] [%(module)s] %(message)s", "%Y-%m-%d %H:%M:%S %z")
for handler in app.logger.handlers:
handler.setFormatter(formatter)
app.logger.info('Logging handler established')

app.logger.info(70 * "*")
app.logger.info(" M Y S E R V I C E R U N N I N G ".center(70, "*"))
app.logger.info(70 * "*")

try:
service.init_db() # make our sqlalchemy tables
except Exception as error:
app.logger.critical("%s: Cannot continue", error)
# gunicorn requires exit code 4 to stop spawning workers when they die
sys.exit(4)

app.logger.info("Service inititalized!")
Loading

0 comments on commit 7de920f

Please sign in to comment.