Commit e8b7508b authored by Sonja Happ's avatar Sonja Happ
Browse files

Merge branch 'master' of git.rwth-aachen.de:acs/public/villas/villasweb-backend-go

# Conflicts:
#	database/database.go
parents 9f29c8a5 5ce4a46e
services:
- postgres:latest
- rabbitmq:latest
variables:
DEPLOY_USER: deploy
DEPLOY_HOST: acs-os-fein-website
DEPLOY_PATH: /var/www/villas/api/web/
TEST_FOLDER: database
DOCKER_IMAGE: ${CI_REGISTRY}/acs/public/villas/web-backend-go
DOCKER_IMAGE_DEV: ${CI_REGISTRY}/acs/public/villas/web-backend-go/dev
POSTGRES_DB: testvillasdb
POSTGRES_USER: villas
POSTGRES_PASSWORD: villas
POSTGRES_HOST: postgres
RABBITMQ_DEFAULT_USER: villas
RABBITMQ_DEFAULT_PASS: villas
AMQP_URL: 'amqp://villas:villas@rabbitmq:5672'
stages:
- prepare
......@@ -22,14 +32,6 @@ prepare:redoc:
- shell
- linux
prepare:ubuntu:
stage: prepare
script:
- docker build -f Dockerfile.ubuntu -t villaswebbackendgo:ubuntu .
tags:
- shell
- linux
# Stage: build
##############################################################################
......@@ -37,19 +39,25 @@ build:backend:
stage: build
tags:
- docker
image: villaswebbackendgo:ubuntu
image: golang:1.12.9-buster
script:
- go mod tidy
- go get -u github.com/swaggo/swag/cmd/swag
- ~/go/bin/swag init -p pascalcase -g "start.go" -o "./doc/api/"
- go install github.com/swaggo/swag/cmd/swag
- swag init -p pascalcase -g "start.go" -o "./doc/api/"
- go build
dependencies:
- prepare:ubuntu
artifacts:
paths:
- doc/api/swagger.json
- doc/api/docs.go
build:docker:
stage: build
tags:
- shell
- linux
script:
- docker build -t ${DOCKER_IMAGE} .
# Stage: test
##############################################################################
......@@ -73,11 +81,18 @@ test:all:
stage: test
tags:
- docker
image: villaswebbackendgo:ubuntu
image: golang:1.12.9-buster
script:
- /etc/init.d/postgresql start
- go mod tidy
- go test $(go list ./... ) -p 1 -covermode=count -coverprofile ./testcover.txt
- go test $(go list ./... )
-p 1
-covermode=count
-coverprofile ./testcover.txt
-dbname ${POSTGRES_DB}
-dbhost ${POSTGRES_HOST}
-dbuser ${POSTGRES_USER}
-dbpass ${POSTGRES_PASSWORD}
-amqp ${AMQP_URL}
- go tool cover -func=testcover.txt
dependencies:
- build:backend
......@@ -86,12 +101,16 @@ test:database:
stage: test
tags:
- docker
image: villaswebbackendgo:ubuntu
image: golang:1.12.9-buster
script:
- /etc/init.d/postgresql start
- go mod tidy
- cd ${TEST_FOLDER}
- go test -v
- go test -v -args
-dbname ${POSTGRES_DB}
-dbhost ${POSTGRES_HOST}
-dbuser ${POSTGRES_USER}
-dbpass ${POSTGRES_PASSWORD}
-amqp ${AMQP_URL}
dependencies:
- build:backend
......@@ -138,14 +157,28 @@ test:user:
# Stage: deploy
##############################################################################
deploy:upload:
deploy:docker:
stage: deploy
before_script:
- docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY}
script:
- cd doc/api
- rsync --copy-links --chown ${DEPLOY_USER}:${DEPLOY_USER} index.html swagger.json ${DEPLOY_USER}@${DEPLOY_HOST}:${DEPLOY_PATH}
- docker push ${DOCKER_IMAGE}
dependencies:
- test:apidoc
- build:docker
only:
- master
tags:
- shell
- linux
# deploy:upload:
# stage: deploy
# script:
# - cd doc/api
# - rsync --copy-links --chown ${DEPLOY_USER}:${DEPLOY_USER} index.html swagger.json ${DEPLOY_USER}@${DEPLOY_HOST}:${DEPLOY_PATH}
# dependencies:
# - test:apidoc
# only:
# - master
# tags:
# - shell
FROM golang:1.12.9-buster AS builder
RUN mkdir /build
WORKDIR /build
ADD . /build
RUN go build -o villasweb-backend
FROM debian:buster
COPY --from=builder /build/villasweb-backend /usr/bin
EXPOSE 4000
CMD [ "villasweb-backend" ]
FROM ubuntu:16.04
ARG GIT_REV=unknown
ARG GIT_BRANCH=unknown
ARG VERSION=unknown
ARG VARIANT=unknown
# Update and Upgrade
RUN apt-get update && apt-get upgrade -y
# Install dependencies
RUN apt-get install -y git ca-certificates g++ gcc libc6 libc6-dev pkg-config wget tar lsb-release
# Install Go 1.12.9
RUN cd /tmp && mkdir golang && cd golang && \
wget https://dl.google.com/go/go1.12.9.linux-amd64.tar.gz && \
tar -xvf go1.12.9.linux-amd64.tar.gz && \
mv go /usr/local && \
rm -rf /tmp/*
ENV GOROOT=/usr/local/go
ENV PATH=/usr/local/go/bin:${PATH}
RUN go version
# Add PostgreSQL Repository to Ubuntu
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -sc)-pgdg main" > /etc/apt/sources.list.d/PostgreSQL.list'
RUN apt-get update && apt-get install -y postgresql-11
# Run the following commands as the ``postgres`` user
USER postgres
RUN /etc/init.d/postgresql start &&\
psql --command "CREATE USER villasuser WITH SUPERUSER PASSWORD 'villasuser';" && \
createdb -O villasuser testvillasdb && \
createdb -O villasuser villasdb
# Add ``listen_addresses`` to ``/etc/postgresql/11/main/postgresql.conf``
RUN echo "listen_addresses='*'" >> /etc/postgresql/11/main/postgresql.conf
# Expose the PostgreSQL port
EXPOSE 5432
WORKDIR /villasweb
ENTRYPOINT bash
LABEL \
org.label-schema.schema-version="1.0" \
org.label-schema.name="VILLASweb" \
org.label-schema.license="GPL-3.0" \
org.label-schema.vcs-ref="$GIT_REV" \
org.label-schema.vcs-branch="$GIT_BRANCH" \
org.label-schema.version="$VERSION" \
org.label-schema.variant="$VARIANT" \
org.label-schema.vendor="Institute for Automation of Complex Power Systems, RWTH Aachen University" \
org.label-schema.author.name="Sonja Happ" \
org.label-schema.author.email="sonja.happ@eonerc.rwth-aachen.de" \
org.label-schema.description="A image containing all build-time dependencies for VILLASweb-backend-go based on Fedora" \
org.label-schema.url="http://fein-aachen.org/projects/villas-framework/" \
org.label-schema.vcs-url="https://git.rwth-aachen.de/VILLASframework/VILLASweb-backend-go"
# <img src="doc/pictures/villas_web.png" width=40 /> VILLASweb-backend-go
[![pipeline status](https://git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/badges/master/pipeline.svg)](https://git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/commits/master)
[![coverage report](https://git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/badges/master/coverage.svg)](https://git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/commits/master)
[![pipeline status](https://git.rwth-aachen.de/acs/public/villas/web-backend-go/badges/master/pipeline.svg)](https://git.rwth-aachen.de/acs/public/villas/web-backend-go/commits/master)
[![coverage report](https://git.rwth-aachen.de/acs/public/villas/web-backend-go/badges/master/coverage.svg)](https://git.rwth-aachen.de/acs/public/villas/web-backend-go/commits/master)
## Description
This is a rewrite of the backend for the VILLASweb
......@@ -13,17 +13,17 @@ The backend is build upon [gin-gonic](https://github.com/gin-gonic/gin) and [GOR
## Quick start
### Docker
You can use the `Dockerfile.ubuntu` for development and testing.
You can use the `Dockerfile.dev` for development and testing.
To build an tag the image call the following in the top level of the repository:
```bash
$ docker build -f Dockerfile.ubuntu -t villaswebbackendgo:ubuntu .
```
$ docker build -f Dockerfile.dev -t registry.git.rwth-aachen.de/acs/public/villas/web-backend-go/dev .
```dev
To run an interactive container based on the image call:
```bash
$ docker run --rm -it villaswebbackendgo:ubuntu
$ docker run --rm -it registry.git.rwth-aachen.de/acs/public/villas/web-backend-go/dev
```
By using the `--rm` option of docker, the container will be removed upon exit.
......
package amqp
import (
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulator"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/simulator"
"github.com/gin-gonic/gin"
"net/http"
"time"
......
......@@ -3,7 +3,7 @@ package amqp
import (
"encoding/json"
"fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"github.com/streadway/amqp"
"github.com/tidwall/gjson"
"strings"
......
......@@ -10,9 +10,10 @@ import (
var DB_HOST string // host of the database system
var DB_NAME string // name of the production database
var DB_TEST string // name of the test database
var DB_USER string // name of the database user
var DB_PASS string // database password
var DB_SSLMODE string // set to enable if database uses SSL
var WITH_AMQP bool // set to true if backend shall be used with AMQP client
var AMQP_URL string // if set connect to AMQP broker using this URL
var DBpool *gorm.DB // database used by backend
......@@ -23,13 +24,13 @@ func init() {
flag.StringVar(&DB_USER, "dbuser", "", "Username of database connection (default is <empty>)")
flag.StringVar(&DB_PASS, "dbpass", "", "Password of database connection (default is <empty>)")
flag.StringVar(&DB_SSLMODE, "dbsslmode", "disable", "SSL mode of DB (default is disable)") // TODO: change default for production
flag.BoolVar(&WITH_AMQP, "amqp", false, "If AMQP client for simulators shall be enabled, set this option to true (default is false)")
flag.StringVar(&AMQP_URL, "amqp", "", "If set, use this url to connect to an AMQP broker (default is disabled)")
flag.Parse()
fmt.Println("DB_HOST has value ", DB_HOST)
fmt.Println("DB_USER has value ", DB_USER)
fmt.Println("DB_NAME has value ", DB_NAME)
fmt.Println("DB_TEST has value ", DB_TEST)
fmt.Println("DB_SSLMODE has value ", DB_SSLMODE)
fmt.Println("WITH_AMQP has value ", WITH_AMQP)
fmt.Println("AMQP_URL has value ", AMQP_URL)
}
// Initialize connection to the database
......@@ -48,9 +49,8 @@ func InitDB(dbname string, isTest bool) *gorm.DB {
}
DBpool = db
if dbname == DB_TEST {
// if we are using the test DB
// drop tables from previous tests
if isTest {
// drop tables for testing case
DropTables(db)
}
......
......@@ -12,7 +12,7 @@ import (
var db *gorm.DB
func TestMain(m *testing.M) {
db = InitDB(DB_TEST)
db = InitDB(DB_NAME, true)
// Verify that you can connect to the database
err := db.DB().Ping()
......
package docs
import "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
import "git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
// This file defines the responses to any endpoint in the backend
// The defined structures are only used for documentation purposes with swaggo and are NOT used in the code
......
package dashboard
import (
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"net/http"
"github.com/gin-gonic/gin"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/scenario"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario"
)
func RegisterDashboardEndpoints(r *gin.RouterGroup) {
......
package dashboard
import (
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/scenario"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario"
)
type Dashboard struct {
......
......@@ -2,11 +2,11 @@ package dashboard
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/scenario"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario"
"github.com/gin-gonic/gin"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
)
func CheckPermissions(c *gin.Context, operation database.CRUD, dabIDSource string, dabIDBody int) (bool, Dashboard) {
......
......@@ -2,10 +2,10 @@ package dashboard
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/scenario"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/user"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/dialects/postgres"
......@@ -55,7 +55,7 @@ func addScenario(token string) (scenarioID uint) {
func TestMain(m *testing.M) {
db = database.InitDB(database.DB_TEST)
db = database.InitDB(database.DB_NAME, true)
defer db.Close()
router = gin.Default()
......
......@@ -2,15 +2,15 @@ package file
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulationmodel"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/widget"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/simulationmodel"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/widget"
)
func RegisterFileEndpoints(r *gin.RouterGroup) {
......
......@@ -9,9 +9,9 @@ import (
"path/filepath"
"time"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulationmodel"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/widget"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/simulationmodel"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/widget"
)
type File struct {
......
......@@ -2,10 +2,10 @@ package file
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulationmodel"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/widget"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/simulationmodel"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/widget"
"github.com/gin-gonic/gin"
)
......
......@@ -3,14 +3,14 @@ package file
import (
"bytes"
"fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/dashboard"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/scenario"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulationmodel"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/simulator"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/widget"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/dashboard"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/simulationmodel"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/simulator"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/user"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/widget"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/dialects/postgres"
......@@ -160,7 +160,7 @@ func addScenarioAndSimulatorAndSimulationModelAndDashboardAndWidget() (scenarioI
func TestMain(m *testing.M) {
db = database.InitDB(database.DB_TEST)
db = database.InitDB(database.DB_NAME, true)
defer db.Close()
router = gin.Default()
......
package scenario
import (
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"net/http"
"github.com/gin-gonic/gin"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/user"
)
func RegisterScenarioEndpoints(r *gin.RouterGroup) {
......
......@@ -2,8 +2,8 @@ package scenario
import (
"fmt"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/user"
"github.com/jinzhu/gorm"
)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment