Skip to content

Commit

Permalink
Merge pull request #1 from USACE/feature/refactor
Browse files Browse the repository at this point in the history
Feature/refactor
  • Loading branch information
ShaneMPutnam authored Jan 4, 2021
2 parents 9970945 + fe7f62b commit 8439447
Show file tree
Hide file tree
Showing 24 changed files with 2,855 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Environments
*.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

go.sum
main
.vscode/
111 changes: 111 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
FROM osgeo/gdal:alpine-small-latest

RUN apk add --no-cache \
ca-certificates

# set up nsswitch.conf for Go's "netgo" implementation
# - https://github.com/golang/go/blob/go1.9.1/src/net/conf.go#L194-L275
# - docker run --rm debian:stretch grep '^hosts:' /etc/nsswitch.conf
RUN [ ! -e /etc/nsswitch.conf ] && echo 'hosts: files dns' > /etc/nsswitch.conf

ENV PATH /usr/local/go/bin:$PATH

ENV GOLANG_VERSION 1.15.3

RUN set -eux; \
apk add --no-cache --virtual .build-deps \
bash \
gcc \
gnupg \
go \
musl-dev \
openssl \
; \
export \
# set GOROOT_BOOTSTRAP such that we can actually build Go
GOROOT_BOOTSTRAP="$(go env GOROOT)" \
# ... and set "cross-building" related vars to the installed system's values so that we create a build targeting the proper arch
# (for example, if our build host is GOARCH=amd64, but our build env/image is GOARCH=386, our build needs GOARCH=386)
GOOS="$(go env GOOS)" \
GOARCH="$(go env GOARCH)" \
GOHOSTOS="$(go env GOHOSTOS)" \
GOHOSTARCH="$(go env GOHOSTARCH)" \
; \
# also explicitly set GO386 and GOARM if appropriate
# https://github.com/docker-library/golang/issues/184
apkArch="$(apk --print-arch)"; \
case "$apkArch" in \
armhf) export GOARM='6' ;; \
armv7) export GOARM='7' ;; \
x86) export GO386='387' ;; \
esac; \
\
# https://github.com/golang/go/issues/38536#issuecomment-616897960
url='https://storage.googleapis.com/golang/go1.15.3.src.tar.gz'; \
sha256='896a602570e54c8cdfc2c1348abd4ffd1016758d0bd086ccd9787dbfc9b64888'; \
\
wget -O go.tgz.asc "$url.asc"; \
wget -O go.tgz "$url"; \
echo "$sha256 *go.tgz" | sha256sum -c -; \
\
# https://github.com/golang/go/issues/14739#issuecomment-324767697
export GNUPGHOME="$(mktemp -d)"; \
# https://www.google.com/linuxrepositories/
gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys 'EB4C 1BFD 4F04 2F6D DDCC EC91 7721 F63B D38B 4796'; \
gpg --batch --verify go.tgz.asc go.tgz; \
gpgconf --kill all; \
rm -rf "$GNUPGHOME" go.tgz.asc; \
\
tar -C /usr/local -xzf go.tgz; \
rm go.tgz; \
\
goEnv="$(go env | sed -rn -e '/^GO(OS|ARCH|ARM|386)=/s//export \0/p')"; \
eval "$goEnv"; \
[ -n "$GOOS" ]; \
[ -n "$GOARCH" ]; \
( \
cd /usr/local/go/src; \
./make.bash; \
); \
\
apk del --no-network .build-deps; \
\
# pre-compile the standard library, just like the official binary release tarballs do
go install std; \
# go install: -race is only supported on linux/amd64, linux/ppc64le, linux/arm64, freebsd/amd64, netbsd/amd64, darwin/amd64 and windows/amd64
# go install -race std; \
\
# remove a few intermediate / bootstrapping files the official binary release tarballs do not contain
rm -rf \
/usr/local/go/pkg/*/cmd \
/usr/local/go/pkg/bootstrap \
/usr/local/go/pkg/obj \
/usr/local/go/pkg/tool/*/api \
/usr/local/go/pkg/tool/*/go_bootstrap \
/usr/local/go/src/cmd/dist/dist \
; \
\
go version

ENV GOPATH /go
ENV PATH $GOPATH/bin:$PATH
RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"

#---------------------------------------------------------------#

RUN apk add --no-cache \
pkgconfig \
gcc \
libc-dev \
git

# Hot-Reloader
RUN go get github.com/githubnemo/CompileDaemon

# Swagger
RUN go get github.com/swaggo/swag/cmd/swag
RUN go get github.com/swaggo/http-swagger
RUN go get github.com/alecthomas/template

COPY ./ /app
WORKDIR /app
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 USACE

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
65 changes: 65 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
# mcat-ras
Contains the HEC-RAS model content and analysis tool (MCAT). Given a .prj file, this MCAT identifies paths to the plan, forcing, and geometry files and extracts the model's metadata and geospatial data.

The MCAT includes:
- a standard set of methods to evaluate a model's content:
- modeltype
- modelversion
- isamodel
- isgeospatial
- index
- geospatialdata
- an API for executing the above methods.
- a docker container for running the methods and API.


## Contents
- `/config`: contains the data structure that holds the config information for the API.
- `/docs`: contains the auto-generated swagger files.
- `/handlers`: contains the handler function for each API endpoint.
- `/tools`: the core code used to extract information from the various HEC-RAS files.
- `docker-compose.yml`: options for building the dockerfile.
- `main.go` : API Server.


### Getting Started
---

- Add a .env file to the root level of this directory with the following structure:
```
AWS_ACCESS_KEY_ID='**************'
AWS_SECRET_ACCESS_KEY='**************'
AWS_DEFAULT_REGION='us-east-1'
S3_BUCKET='******'
```
- Run `docker-compose up`
- To teardown, run `docker-compose down`
### MCAT REST Specification
---
The following requests can be used to interrogate a model whose storage location is defined by the s3_key parameter:
`GET /isamodel?definition_file=<s3_key>`
`GET /isgeospatial?definition_file=<s3_key>`
`GET /modeltype?definition_file=<s3_key>`
`GET /modelversion?definition_file=<s3_key>`
`GET /index?definition_file=<s3_key>`
`GET /geospatialdata?definition_file=<s3_key>`
*For example: `http://mcat-ras:5600/isamodel?definition_file=models/ras/ex_model.prj`*
### Swagger Documentation:
---
To view docs goto: http://localhost:5600/swagger/index.html
![](docs/swagger_image.png)
55 changes: 55 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package config

import (
"fmt"
"os"

"github.com/USACE/filestore"
)

type APIConfig struct {
Host string
Port int
FileStore *filestore.FileStore
}

// Address tells the application where to run the api out of
func (app *APIConfig) Address() string {
return fmt.Sprintf("%s:%d", app.Host, app.Port)
}

// Init initializes the API's configuration
func Init() *APIConfig {
config := new(APIConfig)
config.Host = "" // 0.0.0.0
config.Port = 5600
config.FileStore = FileStoreInit(false)
return config
}

// FileStoreInit initializes the filestore object
func FileStoreInit(local bool) *filestore.FileStore {

var fs filestore.FileStore
var err error
switch local {
case true:
fs, err = filestore.NewFileStore(filestore.BlockFSConfig{})
if err != nil {
panic(err)
}
case false:
config := filestore.S3FSConfig{
S3Id: os.Getenv("AWS_ACCESS_KEY_ID"),
S3Key: os.Getenv("AWS_SECRET_ACCESS_KEY"),
S3Region: os.Getenv("AWS_DEFAULT_REGION"),
S3Bucket: os.Getenv("S3_BUCKET"),
}

fs, err = filestore.NewFileStore(config)
if err != nil {
panic(err)
}
}
return &fs
}
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "3.4"

services:
mcat-ras:
build:
context: ./
volumes:
- ./:/app
ports:
- 5600:5600
env_file:
- ./.env
entrypoint: CompileDaemon --build="go build main.go" --command=./main
Loading

0 comments on commit 8439447

Please sign in to comment.