Skip to content

Commit e5738a6

Browse files
authored
Merge pull request #2 from USACE/feature/port-mcat
Feature/port mcat
2 parents b9cdfca + c8c07be commit e5738a6

25 files changed

+2539
-1
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Environments
2+
*.env
3+
.venv
4+
env/
5+
venv/
6+
ENV/
7+
env.bak/
8+
venv.bak/
9+
10+
go.sum
11+
main
12+
.vscode/

Dockerfile

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
FROM osgeo/gdal:alpine-small-latest
2+
3+
RUN apk add --no-cache \
4+
ca-certificates
5+
6+
# set up nsswitch.conf for Go's "netgo" implementation
7+
# - https://github.com/golang/go/blob/go1.9.1/src/net/conf.go#L194-L275
8+
# - docker run --rm debian:stretch grep '^hosts:' /etc/nsswitch.conf
9+
RUN [ ! -e /etc/nsswitch.conf ] && echo 'hosts: files dns' > /etc/nsswitch.conf
10+
11+
ENV PATH /usr/local/go/bin:$PATH
12+
13+
ENV GOLANG_VERSION 1.15.3
14+
15+
RUN set -eux; \
16+
apk add --no-cache --virtual .build-deps \
17+
bash \
18+
gcc \
19+
gnupg \
20+
go \
21+
musl-dev \
22+
openssl \
23+
; \
24+
export \
25+
# set GOROOT_BOOTSTRAP such that we can actually build Go
26+
GOROOT_BOOTSTRAP="$(go env GOROOT)" \
27+
# ... and set "cross-building" related vars to the installed system's values so that we create a build targeting the proper arch
28+
# (for example, if our build host is GOARCH=amd64, but our build env/image is GOARCH=386, our build needs GOARCH=386)
29+
GOOS="$(go env GOOS)" \
30+
GOARCH="$(go env GOARCH)" \
31+
GOHOSTOS="$(go env GOHOSTOS)" \
32+
GOHOSTARCH="$(go env GOHOSTARCH)" \
33+
; \
34+
# also explicitly set GO386 and GOARM if appropriate
35+
# https://github.com/docker-library/golang/issues/184
36+
apkArch="$(apk --print-arch)"; \
37+
case "$apkArch" in \
38+
armhf) export GOARM='6' ;; \
39+
armv7) export GOARM='7' ;; \
40+
x86) export GO386='387' ;; \
41+
esac; \
42+
\
43+
# https://github.com/golang/go/issues/38536#issuecomment-616897960
44+
url='https://storage.googleapis.com/golang/go1.15.3.src.tar.gz'; \
45+
sha256='896a602570e54c8cdfc2c1348abd4ffd1016758d0bd086ccd9787dbfc9b64888'; \
46+
\
47+
wget -O go.tgz.asc "$url.asc"; \
48+
wget -O go.tgz "$url"; \
49+
echo "$sha256 *go.tgz" | sha256sum -c -; \
50+
\
51+
# https://github.com/golang/go/issues/14739#issuecomment-324767697
52+
export GNUPGHOME="$(mktemp -d)"; \
53+
# https://www.google.com/linuxrepositories/
54+
gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys 'EB4C 1BFD 4F04 2F6D DDCC EC91 7721 F63B D38B 4796'; \
55+
gpg --batch --verify go.tgz.asc go.tgz; \
56+
gpgconf --kill all; \
57+
rm -rf "$GNUPGHOME" go.tgz.asc; \
58+
\
59+
tar -C /usr/local -xzf go.tgz; \
60+
rm go.tgz; \
61+
\
62+
goEnv="$(go env | sed -rn -e '/^GO(OS|ARCH|ARM|386)=/s//export \0/p')"; \
63+
eval "$goEnv"; \
64+
[ -n "$GOOS" ]; \
65+
[ -n "$GOARCH" ]; \
66+
( \
67+
cd /usr/local/go/src; \
68+
./make.bash; \
69+
); \
70+
\
71+
apk del --no-network .build-deps; \
72+
\
73+
# pre-compile the standard library, just like the official binary release tarballs do
74+
go install std; \
75+
# go install: -race is only supported on linux/amd64, linux/ppc64le, linux/arm64, freebsd/amd64, netbsd/amd64, darwin/amd64 and windows/amd64
76+
# go install -race std; \
77+
\
78+
# remove a few intermediate / bootstrapping files the official binary release tarballs do not contain
79+
rm -rf \
80+
/usr/local/go/pkg/*/cmd \
81+
/usr/local/go/pkg/bootstrap \
82+
/usr/local/go/pkg/obj \
83+
/usr/local/go/pkg/tool/*/api \
84+
/usr/local/go/pkg/tool/*/go_bootstrap \
85+
/usr/local/go/src/cmd/dist/dist \
86+
; \
87+
\
88+
go version
89+
90+
ENV GOPATH /go
91+
ENV PATH $GOPATH/bin:$PATH
92+
RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
93+
94+
#---------------------------------------------------------------#
95+
96+
RUN apk add --no-cache \
97+
pkgconfig \
98+
gcc \
99+
libc-dev \
100+
git
101+
102+
# Hot-Reloader
103+
RUN go get github.com/githubnemo/CompileDaemon
104+
105+
# Swagger
106+
RUN go get github.com/swaggo/swag/cmd/swag
107+
RUN go get github.com/swaggo/http-swagger
108+
RUN go get github.com/alecthomas/template
109+
110+
COPY ./ /app
111+
WORKDIR /app

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 USACE
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+65-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,66 @@
11
# mcat-hms
2-
Contains the HEC-HMS model content and analysis tool (MCAT).
2+
Contains the HEC-HMS model content and analysis tool (MCAT). Given a .hms file, this MCAT identifies paths to .control, .met, .basin, .grid, .dss, .sqlite, and .shp files and extracts the model's metadata.
3+
4+
The MCAT includes:
5+
- a standard set of methods to evaluate a model's content:
6+
- isamodel
7+
- modeltype
8+
- modelversion
9+
- index
10+
- isgeospatial
11+
- geospatialdata
12+
- an API for executing the above methods.
13+
- a docker container for running the methods and API.
14+
15+
16+
## Contents
17+
- `/config`: contains the data structure that holds the config information for the API.
18+
- `/docs`: contains the auto-generated swagger files.
19+
- `/handlers`: contains the handler function for each API endpoint.
20+
- `/tools`: the core code used to extract information from the various HEC-HMS files.
21+
- `docker-compose.yml`: options for building the dockerfile.
22+
- `main.go` : API Server.
23+
24+
25+
### Getting Started
26+
---
27+
28+
- Add a .env file to the root level of this directory with the following structure:
29+
```
30+
AWS_ACCESS_KEY_ID='**************'
31+
AWS_SECRET_ACCESS_KEY='**************'
32+
AWS_DEFAULT_REGION='us-east-1'
33+
S3_BUCKET='******'
34+
```
35+
- Run `docker-compose up`
36+
- To teardown, run `docker-compose down`
37+
38+
39+
### MCAT REST Specification
40+
---
41+
The following requests can be used to interrogate a model whose storage location is defined by the s3_key parameter:
42+
43+
`GET /isamodel?definition_file=<s3_key>`
44+
45+
`GET /modeltype?definition_file=<s3_key>`
46+
47+
`GET /modelversion?definition_file=<s3_key>`
48+
49+
`GET /index?definition_file=<s3_key>`
50+
51+
`GET /isgeospatial?definition_file=<s3_key>`
52+
53+
`GET /geospatialdata?definition_file=<s3_key>`
54+
55+
56+
*For example: `http://mcat-hms:5900/isamodel?definition_file=models/hms/Truckee_River/Truckee_River.hms`*
57+
58+
59+
### Swagger Documentation:
60+
61+
---
62+
63+
To view docs goto: http://localhost:5900/swagger/index.html
64+
65+
66+
![](docs/swagger_image.png)

config/config.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/USACE/filestore"
8+
)
9+
10+
type APIConfig struct {
11+
Host string
12+
Port int
13+
FileStore *filestore.FileStore
14+
}
15+
16+
// Address tells the application where to run the api out of
17+
func (app *APIConfig) Address() string {
18+
return fmt.Sprintf("%s:%d", app.Host, app.Port)
19+
}
20+
21+
// Init initializes the API's configuration
22+
func Init() *APIConfig {
23+
config := new(APIConfig)
24+
config.Host = "" // 0.0.0.0
25+
config.Port = 5900
26+
config.FileStore = FileStoreInit(false)
27+
return config
28+
}
29+
30+
// FileStoreInit initializes the filestore object
31+
func FileStoreInit(local bool) *filestore.FileStore {
32+
33+
var fs filestore.FileStore
34+
var err error
35+
switch local {
36+
case true:
37+
fs, err = filestore.NewFileStore(filestore.BlockFSConfig{})
38+
if err != nil {
39+
panic(err)
40+
}
41+
case false:
42+
config := filestore.S3FSConfig{
43+
S3Id: os.Getenv("AWS_ACCESS_KEY_ID"),
44+
S3Key: os.Getenv("AWS_SECRET_ACCESS_KEY"),
45+
S3Region: os.Getenv("AWS_DEFAULT_REGION"),
46+
S3Bucket: os.Getenv("S3_BUCKET"),
47+
}
48+
49+
fs, err = filestore.NewFileStore(config)
50+
if err != nil {
51+
panic(err)
52+
}
53+
}
54+
return &fs
55+
}

docker-compose.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: "3.4"
2+
3+
services:
4+
mcat-hms:
5+
build:
6+
context: ./
7+
volumes:
8+
- ./:/app
9+
ports:
10+
- 5900:5900
11+
env_file:
12+
- ./.env
13+
entrypoint: CompileDaemon --build="go build main.go" --command=./main

0 commit comments

Comments
 (0)