From e6dc73c57fcdf3a596c44fc0f1ccd4c823ba7be1 Mon Sep 17 00:00:00 2001 From: cedricve Date: Sun, 27 Dec 2020 20:37:01 +0100 Subject: [PATCH] initial commit backend --- Dockerfile | 110 ++++++++++++++++++++++++++++++++ backend/data/config/config.json | 86 +++++++++++++++++++++++++ backend/go.mod | 3 + backend/main.go | 10 +++ backend/src/models/config.go | 66 +++++++++++++++++++ scripts/run.sh | 20 ++++++ scripts/supervisor.conf | 39 +++++++++++ 7 files changed, 334 insertions(+) create mode 100644 Dockerfile create mode 100644 backend/data/config/config.json create mode 100644 backend/go.mod create mode 100644 backend/main.go create mode 100644 backend/src/models/config.go create mode 100644 scripts/run.sh create mode 100644 scripts/supervisor.conf diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fdf6bdc --- /dev/null +++ b/Dockerfile @@ -0,0 +1,110 @@ +FROM debian:buster AS builder +MAINTAINER Kerberos.io + +################################# +# Surpress Upstart errors/warning + +RUN dpkg-divert --local --rename --add /sbin/initctl +RUN ln -sf /bin/true /sbin/initctl + +############################################# +# Let the container know that there is no tty + +ENV DEBIAN_FRONTEND noninteractive + +################################# +# Clone and build FFMpeg & OpenCV + +RUN apt-get update && apt-get upgrade -y && apt-get -y --no-install-recommends install git cmake wget dh-autoreconf autotools-dev autoconf automake gcc build-essential libtool make ca-certificates supervisor nasm zlib1g-dev tar libx264. unzip wget pkg-config libavresample-dev && \ + git clone https://github.com/FFmpeg/FFmpeg && \ + cd FFmpeg && git checkout remotes/origin/release/4.0 && \ + ./configure --prefix=/usr/local --target-os=linux --enable-nonfree --enable-libx264 --enable-gpl --enable-shared && \ + make -j8 && \ + make install && \ + cd .. && rm -rf FFmpeg +RUN wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.0.zip && \ + unzip opencv.zip && mv opencv-4.0.0 opencv && cd opencv && mkdir build && cd build && \ + cmake -D CMAKE_BUILD_TYPE=RELEASE \ + -D CMAKE_INSTALL_PREFIX=/usr/ \ + -D OPENCV_GENERATE_PKGCONFIG=YES \ + -D BUILD_TESTS=OFF \ + -D OPENCV_ENABLE_NONFREE=ON \ + #-D BUILD_opencv_dnn=OFF \ + -D BUILD_opencv_ml=OFF \ + -D BUILD_opencv_stitching=OFF \ + -D BUILD_opencv_ts=OFF \ + -D BUILD_opencv_java_bindings_generator=OFF \ + -D BUILD_opencv_python_bindings_generator=OFF \ + -D INSTALL_PYTHON_EXAMPLES=OFF \ + -D BUILD_EXAMPLES=OFF .. && make -j8 && make install && cd ../.. && rm -rf opencv* + +############################ +# Build gGlang + +ENV GOROOT=/usr/local/go +ENV GOPATH=/go +ENV PATH=$GOPATH/bin:$GOROOT/bin:$PATH + +RUN apt-get install -y git +RUN ARCH=$([ "$(uname -m)" = "armv7l" ] && echo "armv6l" || echo "amd64") && wget "https://dl.google.com/go/go1.14.2.linux-$ARCH.tar.gz" && \ + tar -xvf "go1.14.2.linux-$ARCH.tar.gz" && \ + mv go /usr/local + +ENV GO111MODULE=on \ + CGO_ENABLED=1 + +RUN mkdir -p /go/src/github.com/kerberos-io/opensource +COPY backend /go/src/github.com/kerberos-io/opensource +RUN cd /go/src/github.com/kerberos-io/opensource/backend && \ + go mod download && \ + go build -o main && \ + mkdir -p /opensource && \ + mv main /opensource && \ + mkdir -p /opensource/data/cloud && \ + mkdir -p /opensource/data/snapshots && \ + mkdir -p /opensource/data/log && \ + mkdir -p /opensource/data/recordings && \ + mkdir -p /opensource/data/config && \ + rm -rf /go/src/gitlab.com/ + + # Let's create a /dist folder containing just the files necessary for runtime. + # Later, it will be copied as the / (root) of the output image. + WORKDIR /dist + RUN cp -r /opensource ./ + + # Optional: in case your application uses dynamic linking (often the case with CGO), + # this will collect dependent libraries so they're later copied to the final image + # NOTE: make sure you honor the license terms of the libraries you copy and distribute + RUN ldd /opensource/main | tr -s '[:blank:]' '\n' | grep '^/' | \ + xargs -I % sh -c 'mkdir -p $(dirname ./%); cp % ./%;' + RUN mkdir -p lib64 && cp /lib64/ld-linux-x86-64.so.2 lib64/ + RUN cp -r /usr/local/lib/libavcodec* ./usr/lib && \ + cp -r /usr/local/lib/libavformat* ./usr/lib && \ + cp -r /usr/local/lib/libswscale* ./usr/lib && \ + cp -r /usr/local/lib/libswresample* ./usr/lib + RUN ldd /opensource/main + + FROM alpine:latest + + ################################# + # Copy files from previous images + + COPY --chown=0:0 --from=builder /dist / + COPY --chown=0:0 --from=builder /usr/local/go/lib/time/zoneinfo.zip /zoneinfo.zip + + ENV ZONEINFO=/zoneinfo.zip + + RUN apk update && apk add ca-certificates && \ + apk add --no-cache tzdata && rm -rf /var/cache/apk/* + + #################################### + # ADD supervisor and STARTUP script + + RUN apk add supervisor && mkdir -p /var/log/supervisor/ + ADD ./scripts/supervisor.conf /etc/supervisord.conf + ADD ./scripts/run.sh /run.sh + RUN chmod 755 /run.sh && chmod +x /run.sh + + EXPOSE 8080 + + CMD ["sh", "/run.sh"] diff --git a/backend/data/config/config.json b/backend/data/config/config.json new file mode 100644 index 0000000..a06b465 --- /dev/null +++ b/backend/data/config/config.json @@ -0,0 +1,86 @@ +{ + "type": "template", + "key": "", + "name": "camera", + "capture": { + "id": "ipcamera", + "ipcamera": { + "rtsp": "", + "fps": "" + }, + "usbcamera": { + "rtsp": "", + "fps": "" + }, + "raspicamera": { + "rtsp": "", + "fps": "" + }, + "continuous": "false", + "postrecording": 10, + "prerecording": 5, + "maxlengthrecording": 10 + }, + "timetable": [ + { + "start1": 0, + "end1": 43199, + "start2": 43200, + "end2": 86400 + }, + { + "start1": 0, + "end1": 69840, + "start2": 70080, + "end2": 86400 + }, + { + "start1": 0, + "end1": 43199, + "start2": 43200, + "end2": 86400 + }, + { + "start1": 0, + "end1": 43199, + "start2": 43200, + "end2": 86400 + }, + { + "start1": 0, + "end1": 43199, + "start2": 43200, + "end2": 86400 + }, + { + "start1": 0, + "end1": 43199, + "start2": 43200, + "end2": 86400 + }, + { + "start1": 0, + "end1": 43199, + "start2": 43200, + "end2": 86400 + } + ], + "region": { + "rectangle": { + "x1": 0, + "y1": 0, + "x2": 800, + "y2": 640 + }, + "polygon": [] + }, + "cloud": "kstorage", + "kstorage": { + "uri": "", + "cloud_key": "", + "access_key": "", + "secret_access_key": "", + "provider": "", + "directory": "" + } +} diff --git a/backend/go.mod b/backend/go.mod new file mode 100644 index 0000000..e6211ac --- /dev/null +++ b/backend/go.mod @@ -0,0 +1,3 @@ +module github.com/kerberos-io/opensource/backend + +go 1.15 diff --git a/backend/main.go b/backend/main.go new file mode 100644 index 0000000..ce7435c --- /dev/null +++ b/backend/main.go @@ -0,0 +1,10 @@ +package main + +import ( + "fmt" +) + +func main(){ + fmt.Println("So is the first line of Kerberos Open Source v3") + return +} diff --git a/backend/src/models/config.go b/backend/src/models/config.go new file mode 100644 index 0000000..4bb4ba9 --- /dev/null +++ b/backend/src/models/config.go @@ -0,0 +1,66 @@ +package models + +type Config struct { + Type string `json:"type" binding:"required"` + Key string `json:"key"` + Name string `json:"name"` + Timezone string `json:"timezone,omitempty" bson:"timezone,omitempty"` + Capture Capture `json:"capture"` + Timetable []*Timetable `json:"timetable"` + Region *Region `json:"region"` + Cloud string `json:"cloud,omitempty" bson:"cloud,omitempty"` + KStorage *KStorage `json:"kstorage,omitempty" bson:"kstorage,omitempty"` + MQTTURI string `json:"mqtturi,omitempty" bson:"mqtturi,omitempty"` +} + +type Capture struct { + Id string `json:"id"` + IPCamera IPCamera `json:"ipcamera"` + Continuous string `json:"continuous,omitempty"` + PostRecording int64 `json:"postrecording"` + PreRecording int `json:"prerecording"` + MaxLengthRecording int64 `json:"maxlengthrecording"` +} + +type IPCamera struct { + RTSP string `json:"rtsp"` + FPS string `json:"fps"` +} + +type Region struct { + Rectangle Rectangle `json:"rectangle"` + Polygon []Polygon `json:"polygon"` +} + +type Rectangle struct { + X1 int `json:"x1"` + Y1 int `json:"y1"` + X2 int `json:"x2"` + Y2 int `json:"y2"` +} + +type Polygon struct { + Id string `json:"id"` + Coordinates []Coordinate `json:"coordinates"` +} + +type Coordinate struct { + X float64 `json:"x"` + Y float64 `json:"y"` +} + +type Timetable struct { + Start1 int `json:"start1"` + End1 int `json:"end1"` + Start2 int `json:"start2"` + End2 int `json:"end2"` +} + +type KStorage struct { + URI string `json:"uri,omitempty" bson:"uri,omitempty"` + CloudKey string `json:"cloud_key,omitempty" bson:"cloud_key,omitempty"` + AccessKey string `json:"access_key,omitempty" bson:"access_key,omitempty"` + SecretAccessKey string `json:"secret_access_key,omitempty" bson:"secret_access_key,omitempty"` + Provider string `json:"provider,omitempty" bson:"provider,omitempty"` + Directory string `json:"directory,omitempty" bson:"directory,omitempty"` +} diff --git a/scripts/run.sh b/scripts/run.sh new file mode 100644 index 0000000..755a07d --- /dev/null +++ b/scripts/run.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +autoremoval() { + partition=/dev/ + imagedir=/opensource/data/recordings + clouddir=/opensource/data/cloud + + while : + do + #find $imagedir -type f -mtime +2 -exec rm {} \; + #find $clouddir -type f -mtime +2 -exec rm {} \; + sleep 60 + done +} + +export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib && ldconfig + +autoremoval & + +/usr/bin/supervisord -n -c /etc/supervisord.conf diff --git a/scripts/supervisor.conf b/scripts/supervisor.conf new file mode 100644 index 0000000..6efd3af --- /dev/null +++ b/scripts/supervisor.conf @@ -0,0 +1,39 @@ +; supervisor config file + +[unix_http_server] +file=/var/run/supervisor.sock ; (the path to the socket file) +chmod=0700 ; sockef file mode (default 0700) + +[supervisord] +logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) +pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) +childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP) + +; the below section must remain in the config file for RPC +; (supervisorctl/web interface) to work, additional interfaces may be +; added by defining them in separate rpcinterface: sections +[rpcinterface:supervisor] +supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface + +[supervisorctl] +serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket + +; The [include] section can just contain the "files" setting. This +; setting can list multiple files (separated by whitespace or +; newlines). It can also contain wildcards. The filenames are +; interpreted as relative to this file. Included files *cannot* +; include files themselves. + +[include] +files = /etc/supervisor/conf.d/*.conf + +[program:opensource] +command=/opensource/main run opensource 8080 +directory=/opensource +autostart = true +autorestart = true +user = root +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0