commit fa3286c8e20149fd22edc89be26b512a5398875c Author: Thomas Peetz Date: Mon Jun 17 18:49:23 2019 +0200 Initial setup diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6cc95d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.gradle/ +docs/build/ +bin/ +cover.out +coverage.html +coverage.xml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..7287531 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,64 @@ +stages: +- build +- test +- deploy +- notify + +before_script: + - go get -u github.com/jstemmer/go-junit-report + - go get -u github.com/t-yuki/gocover-cobertura + +Create Documentation: + stage: build + script: + - chmod +x docs/gradlew + - cd docs; ./gradlew --no-daemon asciidoctor + artifacts: + paths: + - docs/build/asciidoc/pdf/kalorienmanager.pdf + +Compile Go Application: + stage: build + script: make build + +Test Go Application: + stage: test + script: + - make test + - GOPATH=$HOME/go go test -v ./... 2>&1 | /home/gitlab-runner/go/bin/go-junit-report > report.xml + - GOPATH=$HOME/go go vet ./... + artifacts: + reports: + junit: report.xml + +Deploy To Staging: + stage: deploy + script: + - make build + - ssh kalorienmanager /home/kalorienmanager/app-test_service stop + - rsync -av templates kalorienmanager:/home/kalorienmanager/staging + - rsync -av bin/kalorienmanager kalorienmanager:/home/kalorienmanager/staging + - ssh kalorienmanager /home/kalorienmanager/app-test_service start + environment: + name: staging + only: + - master + +Deploy to Production: + stage: deploy + script: + - make build + - ssh kalorienmanager /home/kalorienmanager/app_service stop + - rsync -av templates kalorienmanager:/home/kalorienmanager/production + - rsync -av bin/kalorienmanager kalorienmanager:/home/kalorienmanager/production + - ssh kalorienmanager /home/kalorienmanager/app_service start + environment: + name: production + only: + - master + when: manual + +Notify Telegram: + stage: notify + script: + - sh .ci-notify.sh ✅ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7aae1cd --- /dev/null +++ b/Makefile @@ -0,0 +1,71 @@ +# Go parameters +GOPATH=$(HOME)/go +GOCMD=go +GOGET=$(GOCMD) get +GOBUILD=$(GOCMD) build +GOCLEAN=$(GOCMD) clean +GOTEST=$(GOCMD) test +GOCOVER=$(GOCMD) tool cover +COBERTURA=$(GOBIN)/gocover-cobertura +GOLINT=$(GOBIN)/golint +GOVET=$(GOCMD) vet + +# Project parameters +GONAME=kalorienmanager +GOFILE=cmd/kalorienmanager/main.go + +#.PHONY: all deps clean clean-bin clean-doc + +all: deps build + +deps: + GOPATH=$(GOPATH) go get -u -v github.com/jstemmer/go-junit-report + GOPATH=$(GOPATH) go get -u -v github.com/tebeka/go2xunit + GOPATH=$(GOPATH) go get -u -v github.com/t-yuki/gocover-cobertura + GOPATH=$(GOPATH) go get -u -v github.com/spf13/cobra/cobra + GOPATH=$(GOPATH) go get -u -v github.com/inconshreveable/mousetrap + GOPATH=$(GOPATH) go get -u -v github.com/mitchellh/go-homedir + GOPATH=$(GOPATH) go get -u -v github.com/golang/protobuf/proto + GOPATH=$(GOPATH) go get -u -v github.com/gin-gonic/gin + GOPATH=$(GOPATH) go get -u -v github.com/gin-gonic/gin/binding + GOPATH=$(GOPATH) go get -u -v github.com/gin-gonic/gin/render + GOPATH=$(GOPATH) go get -u -v github.com/gin-contrib/sse + GOPATH=$(GOPATH) go get -u -v github.com/mattn/go-isatty + GOPATH=$(GOPATH) go get -u -v github.com/ugorji/go/codec + GOPATH=$(GOPATH) go get -u -v golang.org/x/crypto/bcrypt + GOPATH=$(GOPATH) go get -u -v golang.org/x/crypto/blowfish + GOPATH=$(GOPATH) go get -u -v gopkg.in/yaml.v2 + GOPATH=$(GOPATH) go get -u -v gopkg.in/mgo.v2 + GOPATH=$(GOPATH) go get -u -v gopkg.in/mgo.v2/bson + +build: bin/$(GONAME) + +bin/$(GONAME): $(GOFILE) + @echo "Building $(GOFILE) to ./bin" + @GOPATH=$(GOPATH) $(GOBUILD) -v -ldflags="-X main.version=$(shell git describe --always --long --dirty)" -o bin/$(GONAME) $(GOFILE) + +install: + @echo using $(GOPATH) + GOPATH=$(GOPATH) $(GOCMD) install -v -ldflags="-X main.version=$(shell git describe --always --long --dirty)" ./... + +test: + GOPATH=$(GOPATH) $(GOTEST) -v ./... + GOPATH=$(GOPATH) $(GOTEST) -coverprofile=cover.out ./... + GOPATH=$(GOPATH) $(GOCOVER) -html=cover.out -o coverage.html + GOPATH=$(GOPATH) $(COBERTURA) < cover.out > coverage.xml + GOPATH=$(GOPATH) $(GOLINT) ./... + GOPATH=$(GOPATH) $(GOVET) -v ./... + +doc: + cd docs; ./gradlew build + +clean: clean-doc clean-bin + + +clean-doc: + @echo "Cleaning Gradle build" + cd docs; ./gradlew clean + +clean-bin: + @echo "Cleaning Go build" + rm -rf bin/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..6fd7a8f --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +[![pipeline status](https://gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager/badges/master/pipeline.svg)](https://gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager/commits/master) +[![coverage report](https://gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager/badges/master/coverage.svg)](https://gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager/commits/master) + +# Kalorien Manager + diff --git a/cmd/kalorienmanager/main.go b/cmd/kalorienmanager/main.go new file mode 100644 index 0000000..138e161 --- /dev/null +++ b/cmd/kalorienmanager/main.go @@ -0,0 +1,11 @@ +package main + +import ( + "gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/cmd" +) + +var version = "undefined" + +func main() { + cmd.Execute(version) +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..6efe149 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,81 @@ +package cmd + +import ( + "fmt" + "log" + "os" + + "gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/properties" + "gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git/pkg/setup" + + "github.com/gin-gonic/gin" + "github.com/spf13/cobra" +) + +var ( + // Verbose defines the parameter verbose. + Verbose bool + // Version defines the version of the web application. + Version string + // Debug defines the debug parameter. + Debug bool + // Port defines the parameter port. + Port int + // TemplatesDir defines the root directory of template files. + TemplatesDir string + router *gin.Engine +) + +var rootCmd = &cobra.Command{ + Use: "kontor", + Short: "kontor", + Long: `kontor`, + Run: func(cmd *cobra.Command, args []string) { + // Set Gin to production mode + if Debug { + gin.SetMode(gin.DebugMode) + } else { + gin.SetMode(gin.ReleaseMode) + } + log.SetOutput(gin.DefaultWriter) + + // Set the router as the default one provided by Gin + router = gin.Default() + + // Process the templates at the start so that they don't have to be loaded + // from the disk again. This makes serving HTML pages very fast. + templatesDir := fmt.Sprintf("%s/**/*", TemplatesDir) + log.Printf("load template files from %v", templatesDir) + router.LoadHTMLGlob(templatesDir) + + // Initialize the routes + setup.InitializeRoutes(router) + + // Clean up SetSessionStatus + setup.CleanupSessions() + + // Check if at least one user configured + setup.CheckUserList() + + // Start serving the application + server := fmt.Sprintf("127.0.0.1:%d", Port) + router.Run(server) + }, +} + +// Execute parses the commandline and calls Execute on rootCmd. +func Execute(version string) { + rootCmd.Version = version + properties.SetVersion(version) + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} + +func init() { + rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output") + rootCmd.PersistentFlags().BoolVarP(&Debug, "debug", "d", false, "debug modus") + rootCmd.PersistentFlags().IntVarP(&Port, "port", "p", 8700, "port number") + rootCmd.PersistentFlags().StringVarP(&TemplatesDir, "templates", "t", "templates", "path for template files") +} diff --git a/docs/build.gradle b/docs/build.gradle new file mode 100644 index 0000000..72c738c --- /dev/null +++ b/docs/build.gradle @@ -0,0 +1,51 @@ +buildscript { + repositories { + jcenter() + maven { + url "https://plugins.gradle.org/m2/" + } + } + dependencies { + classpath 'org.asciidoctor:asciidoctor-gradle-plugin:1.5.7' + //classpath 'org.asciidoctor:asciidoctorj-epub3:1.5.0-alpha.6' + classpath ('org.asciidoctor:asciidoctorj-pdf:1.5.0-alpha.16') { + exclude group: 'org.jruby' + } + classpath 'org.jruby:jruby:9.1.17.0' + } +} + +apply plugin: 'org.asciidoctor.convert' + +version = '0.0.1-SNAPSHOT' +description = 'Kalorien Manager Application' + +final BUILD_DATE = new Date().format('dd.MM.yyyy').toString() + +asciidoctorj { + version = '1.6.0-alpha.7' +} + +asciidoctor { + backends 'html5', 'pdf' + attributes 'build-gradle': file('build.gradle'), + 'source-highlighter': 'coderay', + 'toc': 'left', + 'toc-title': 'Inhaltsverzeichnis', + 'revdate': BUILD_DATE, + 'revnumber': version, + 'idprefix': 'id_', + 'chapter-label': '', + 'icons': 'font', + 'setanchors': '', + 'idseparator': '-', + 'endpoint-url': 'http://www.ingenieurbuero-peetz.de', + 'imagesdir': './images', + 'docinfo1': '' +} + +build.dependsOn(['asciidoctor']) + +wrapper { + gradleVersion = "5.2" +} diff --git a/docs/gradle/wrapper/gradle-wrapper.jar b/docs/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..87b738c Binary files /dev/null and b/docs/gradle/wrapper/gradle-wrapper.jar differ diff --git a/docs/gradle/wrapper/gradle-wrapper.properties b/docs/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..5a7a6a1 --- /dev/null +++ b/docs/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/docs/gradlew b/docs/gradlew new file mode 100755 index 0000000..af6708f --- /dev/null +++ b/docs/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/docs/gradlew.bat b/docs/gradlew.bat new file mode 100644 index 0000000..0f8d593 --- /dev/null +++ b/docs/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/docs/src/docs/asciidoc/kalorienmanager.adoc b/docs/src/docs/asciidoc/kalorienmanager.adoc new file mode 100644 index 0000000..5334cfb --- /dev/null +++ b/docs/src/docs/asciidoc/kalorienmanager.adoc @@ -0,0 +1,31 @@ += Projektbeschreibung Kontor +:author: Thomas Peetz +:email: +:doctype: article +:toc: left +:sectnums: + +== Einführung + +=== Zweck + +== Anforderungen + +== Implementierung + +=== Datenmodell + +== Betrieb + +[bibliography] +== Referenzen + +- [[[1]]] Thomas Peetz, Betriebshandbuch IBTP + +[index] +== Index + +== Tabellenverzeichnis + +[glossary] +== Glossar diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7e7177b --- /dev/null +++ b/go.mod @@ -0,0 +1 @@ +module gitlab.ingenieurbuero-peetz.de/tpeetz/kalorienmanager.git