Compare commits
15 Commits
583126b0c0
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 95c7bfe933 | |||
| 9d3cec7cf6 | |||
| d96ba361fc | |||
| 64d8d629fe | |||
| 4fd5335bdb | |||
| 87af07fc60 | |||
| e9fb08f2ca | |||
| 91c2cb2c9e | |||
| a8db2d5452 | |||
| deb7cdbc35 | |||
| f329358149 | |||
| 8a0baeb596 | |||
| f23b37241e | |||
| f5ebf42d15 | |||
| 396f54ef0c |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -29,3 +29,7 @@ config/__pycache__
|
||||
|
||||
# Template generated files:
|
||||
src/docker-compose.yml
|
||||
src/clean-data.sh
|
||||
|
||||
# NGINX API Binary
|
||||
bin
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
## Database Variables
|
||||
db = {}
|
||||
#- db['root_pw'] = 'RootPassWord'
|
||||
db['root_pw'] = 'RootPassWord'
|
||||
db['enabled'] = True
|
||||
db['user'] = 'DBUser'
|
||||
db['user_pw'] = 'DBUserPassWord'
|
||||
@@ -15,3 +15,9 @@ db['db'] = 'Database Name'
|
||||
#- db['data_path_mysql'] = '/var/lib/mysql'
|
||||
#- db['data_path_mysql_backup'] = '/var/lib/mysql'
|
||||
#- db['port'] = 3306
|
||||
#- db['host'] = localhost
|
||||
|
||||
## Api Configuration
|
||||
api = {}
|
||||
#- api['ip'] = '0.0.0.0'
|
||||
#- api['port'] = 81
|
||||
|
||||
@@ -14,7 +14,7 @@ def template(filename, target_path):
|
||||
# Load Template
|
||||
template = env.get_template(filename)
|
||||
# Render Output
|
||||
content = template.render(db=config.db)
|
||||
content = template.render(db=config.db,api=config.api)
|
||||
# Write Template to file
|
||||
filename_without_ext = os.path.splitext(filename)[0]
|
||||
cooked_file = target_path+filename_without_ext
|
||||
@@ -25,3 +25,7 @@ def template(filename, target_path):
|
||||
# Render the templates:
|
||||
## Create Docker-Compose
|
||||
template("docker-compose.yml.j2", "./src/")
|
||||
## Create API Config
|
||||
template(".env.j2", "./bin/")
|
||||
## Create Cleanup script
|
||||
template("clean-data.sh.j2", "./src/")
|
||||
8
makefile
8
makefile
@@ -20,7 +20,9 @@ start: db-start api-start ui-start
|
||||
stop: ui-stop api-stop db-stop
|
||||
|
||||
# Clean everything
|
||||
clean: ui-clean api-clean db-clean
|
||||
clean:
|
||||
chmod +x ./src/clean-data.sh
|
||||
./src/clean-data.sh
|
||||
|
||||
# Setup Database
|
||||
db-init: config
|
||||
@@ -35,8 +37,10 @@ db-stop:
|
||||
# Setup API
|
||||
api: api-start
|
||||
|
||||
api-init:
|
||||
cd src/api; go build -o ../../bin/engine_x_mgmt
|
||||
api-start:
|
||||
|
||||
cd bin; ./engine_x_mgmt
|
||||
api-stop:
|
||||
|
||||
# Setup UI
|
||||
|
||||
8
src/api/go.mod
Normal file
8
src/api/go.mod
Normal file
@@ -0,0 +1,8 @@
|
||||
module api
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/go-sql-driver/mysql v1.6.0
|
||||
github.com/joho/godotenv v1.4.0
|
||||
)
|
||||
4
src/api/go.sum
Normal file
4
src/api/go.sum
Normal file
@@ -0,0 +1,4 @@
|
||||
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
|
||||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
|
||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
18
src/api/helper/10_ConnectDB.go
Normal file
18
src/api/helper/10_ConnectDB.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// docker run --name mysqldb -e MYSQL_ROOT_PASSWORD=rootpw -p 3307:3306 -d mysql:latest
|
||||
func ConnectDB() {
|
||||
db_string := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", Db_User, Db_Pass, Db_Host, Db_Port, Db_Default_Db)
|
||||
Db, err := sql.Open("mysql", db_string)
|
||||
if err != nil {
|
||||
LogErrorFatal(err)
|
||||
}
|
||||
defer Db.Close()
|
||||
}
|
||||
42
src/api/helper/10_LoadEnv.go
Normal file
42
src/api/helper/10_LoadEnv.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func Loadenv() {
|
||||
GetConfigFile()
|
||||
err := godotenv.Load()
|
||||
LogErrorWarning(err)
|
||||
}
|
||||
|
||||
func GetConfigFile() {
|
||||
dotenv_exists := FileExists(".env")
|
||||
if dotenv_exists {
|
||||
godotenv.Load(".env")
|
||||
LogInfo(".env file found, using prefered values")
|
||||
} else {
|
||||
LogInfo("No .env file found, using default values")
|
||||
}
|
||||
GetDefaults()
|
||||
}
|
||||
|
||||
func Set_logfile() {
|
||||
f, err := os.OpenFile(Log_file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
log.Fatalf("error opening file: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
log.SetOutput(f)
|
||||
}
|
||||
|
||||
func FileExists(filename string) bool {
|
||||
info, err := os.Stat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return !info.IsDir()
|
||||
}
|
||||
40
src/api/helper/11_QueryDB.go
Normal file
40
src/api/helper/11_QueryDB.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package helper
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func QueryString(queryStr string) []byte {
|
||||
db_string := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", Db_User, Db_Pass, Db_Host, Db_Port, Db_Default_Db)
|
||||
conn, err := sql.Open("mysql", db_string)
|
||||
if err != nil {
|
||||
LogErrorWarning(err)
|
||||
return []byte("Error while connecting to database")
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
qdata, err := conn.Query(queryStr)
|
||||
LogErrorWarning(err)
|
||||
defer qdata.Close()
|
||||
|
||||
kvp := make(map[string]string)
|
||||
|
||||
for qdata.Next() {
|
||||
var key string
|
||||
var value string
|
||||
|
||||
err = qdata.Scan(&key, &value)
|
||||
LogErrorWarning(err)
|
||||
kvp[key] = value
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(kvp)
|
||||
if err != nil {
|
||||
return []byte("Error while generating json")
|
||||
}
|
||||
return jsonData
|
||||
}
|
||||
21
src/api/helper/20_CheckError.go
Normal file
21
src/api/helper/20_CheckError.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package helper
|
||||
|
||||
import "log"
|
||||
|
||||
func LogErrorWarning(err error) {
|
||||
if err != nil {
|
||||
log.Printf("WARNING: %v \n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func LogErrorFatal(err error) {
|
||||
if err != nil {
|
||||
log.Fatalf("ERROR: %v \n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func LogInfo(info string) {
|
||||
if info != "" {
|
||||
log.Printf("INFO: %v \n", info)
|
||||
}
|
||||
}
|
||||
72
src/api/helper/20_GetDefaults.go
Normal file
72
src/api/helper/20_GetDefaults.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package helper
|
||||
|
||||
import "os"
|
||||
|
||||
func GetDefaults() {
|
||||
// Gin Variables
|
||||
// Set IP
|
||||
env_ip, present := os.LookupEnv("LISTEN_IP")
|
||||
if present {
|
||||
Ip = env_ip
|
||||
} else {
|
||||
Ip = default_ip
|
||||
}
|
||||
// Set Port
|
||||
env_port, present := os.LookupEnv("LISTEN_PORT")
|
||||
if present {
|
||||
Http_port = env_port
|
||||
} else {
|
||||
Http_port = default_port
|
||||
}
|
||||
// Log Variables
|
||||
// Set Logfile
|
||||
env_log_file, present := os.LookupEnv("LOG_FILE")
|
||||
if present {
|
||||
Log_file = env_log_file
|
||||
} else {
|
||||
Log_file = default_log_file
|
||||
}
|
||||
// Set Gin log file
|
||||
env_gin_log_file, present := os.LookupEnv("GIN_LOG_FILE")
|
||||
if present {
|
||||
Gin_log_file = env_gin_log_file
|
||||
} else {
|
||||
Gin_log_file = default_gin_log_file
|
||||
}
|
||||
// DB Variables
|
||||
// Set DB User
|
||||
env_db_user, present := os.LookupEnv("DB_USER")
|
||||
if present {
|
||||
Db_User = env_db_user
|
||||
} else {
|
||||
Db_User = default_DB_User
|
||||
}
|
||||
// Set DB Password
|
||||
env_db_pass, present := os.LookupEnv("DB_PASSWORD")
|
||||
if present {
|
||||
Db_Pass = env_db_pass
|
||||
} else {
|
||||
Db_Pass = default_DB_Pass
|
||||
}
|
||||
// Set DB Port
|
||||
env_db_port, present := os.LookupEnv("DB_PORT")
|
||||
if present {
|
||||
Db_Port = env_db_port
|
||||
} else {
|
||||
Db_Port = default_DB_Port
|
||||
}
|
||||
// Set DB Host
|
||||
env_db_host, present := os.LookupEnv("DB_HOST")
|
||||
if present {
|
||||
Db_Host = env_db_host
|
||||
} else {
|
||||
Db_Host = default_DB_Host
|
||||
}
|
||||
// Set DB Default Database
|
||||
env_db_default_db, present := os.LookupEnv("DB_DEFAULT_DATABASE")
|
||||
if present {
|
||||
Db_Default_Db = env_db_default_db
|
||||
} else {
|
||||
Db_Default_Db = default_DB_Default_db
|
||||
}
|
||||
}
|
||||
5
src/api/helper/90_vars.go
Normal file
5
src/api/helper/90_vars.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package helper
|
||||
|
||||
import "database/sql"
|
||||
|
||||
var Db *sql.DB
|
||||
33
src/api/helper/91_default_vars.go
Normal file
33
src/api/helper/91_default_vars.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package helper
|
||||
|
||||
// Default Vars
|
||||
// --- Default Gin Vars ---
|
||||
var default_port string = "8081"
|
||||
var default_ip string = "0.0.0.0"
|
||||
|
||||
// --- Default Log Vars ---
|
||||
var default_log_file string = "/var/log/gonodeinfo.log"
|
||||
var default_gin_log_file string = "/var/log/gonodeinfo.gin.log"
|
||||
|
||||
// --- Default DB Vars ---
|
||||
var default_DB_User string = "root"
|
||||
var default_DB_Pass string = "rootpw"
|
||||
var default_DB_Host string = "127.0.0.1"
|
||||
var default_DB_Port string = "3307"
|
||||
var default_DB_Default_db string = "sys"
|
||||
|
||||
// Vars
|
||||
// --- Gin Vars ---
|
||||
var Http_port string
|
||||
var Ip string
|
||||
|
||||
// --- Log Vars ---
|
||||
var Log_file string
|
||||
var Gin_log_file string
|
||||
|
||||
// --- DB Vars ---
|
||||
var Db_User string
|
||||
var Db_Pass string
|
||||
var Db_Host string
|
||||
var Db_Port string
|
||||
var Db_Default_Db string
|
||||
9
src/api/main.go
Normal file
9
src/api/main.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
helper "api/helper"
|
||||
)
|
||||
|
||||
func main() {
|
||||
helper.Loadenv()
|
||||
}
|
||||
5
src/api/test/main.go
Normal file
5
src/api/test/main.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
Test()
|
||||
}
|
||||
7
src/api/test/test.go
Normal file
7
src/api/test/test.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Test() {
|
||||
fmt.Println("test")
|
||||
}
|
||||
39
src/templates/.env.j2
Normal file
39
src/templates/.env.j2
Normal file
@@ -0,0 +1,39 @@
|
||||
# Nginx API
|
||||
## Listen IP
|
||||
{%- if api.ip is defined %}
|
||||
NGINX_API_LISTEN_IP={{ api.ip }}
|
||||
{%- else %}
|
||||
NGINX_API_LISTEN_IP=localhost
|
||||
{%- endif %}
|
||||
## Listen Port
|
||||
{%- if api.port is defined %}
|
||||
NGINX_API_LISTEN_PORT={{ api.port }}
|
||||
{%- else %}
|
||||
NGINX_API_LISTEN_PORT=79
|
||||
{%- endif %}
|
||||
|
||||
# Database
|
||||
## Database Host
|
||||
{%- if db.host is defined %}
|
||||
DB_HOST={{ db.host }}
|
||||
{%- else %}
|
||||
DB_HOST=localhost
|
||||
{%- endif %}
|
||||
## Database Port
|
||||
{%- if db.port is defined %}
|
||||
DB_PORT={{ db.port }}
|
||||
{%- else %}
|
||||
DB_PORT=3306
|
||||
{%- endif %}
|
||||
## Database User
|
||||
{%- if db.user is defined %}
|
||||
DB_USER={{ db.user }}
|
||||
{%- else %}
|
||||
DB_USER={{0/0}} # Throw error if not set / can be overwritten by envrionment variable
|
||||
{%- endif %}
|
||||
## Database Password
|
||||
{%- if db.user_pw is defined %}
|
||||
DB_USERPW={{ db.user_pw }}
|
||||
{%- else %}
|
||||
DB_USERPW={{0/0}} # Throw error if not set / can be overwritten by envrionment variable
|
||||
{%- endif %}
|
||||
33
src/templates/clean-data.sh.j2
Normal file
33
src/templates/clean-data.sh.j2
Normal file
@@ -0,0 +1,33 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Variables
|
||||
## Static
|
||||
CLEAN_ERROR_ID=0
|
||||
|
||||
# Pre Tasks
|
||||
## Stop Project
|
||||
make stop
|
||||
|
||||
# Clean Tasks
|
||||
echo "Executing Clean tasks"
|
||||
## Remove Generated docker-compose
|
||||
rm -rf src/docker-compose.yml || CLEAN_ERROR_ID=1
|
||||
|
||||
## Clean DB-Data
|
||||
sudo rm -rf {% if db.data_path_mysql is defined %}{{ data_path_mysql }}{% else %}./src/db/cache/mysql_data{% endif %} || CLEAN_ERROR_ID=3
|
||||
rm -rf {% if db.data_path_mysql_backup is defined %}{{ data_path_mysql_backup }}{% else %}./src/db/cache/mysql_backup{% endif %} || CLEAN_ERROR_ID=4
|
||||
|
||||
## Remove everything in binary directory
|
||||
rm -rf bin/* || CLEAN_ERROR_ID=5
|
||||
rm -rf bin/.env || CLEAN_ERROR_ID=6
|
||||
# Post Task
|
||||
## Remove Clean script itself
|
||||
rm -rf src/clean-data.sh || CLEAN_ERROR_ID=2
|
||||
|
||||
## Output
|
||||
if [ $CLEAN_ERROR_ID -eq 0 ]
|
||||
then
|
||||
echo "Cleaning Tasks were Successfull"
|
||||
else
|
||||
echo "There where some Issues while cleaning, check ID: $CLEAN_ERROR_ID"
|
||||
fi
|
||||
@@ -2,32 +2,30 @@
|
||||
version: '3.1'
|
||||
|
||||
services:
|
||||
{% if db.enabled == true %}
|
||||
{%- if db.enabled == true %}
|
||||
ngx_mgmt_db:
|
||||
image: mariadb:latest
|
||||
restart: always
|
||||
environment:
|
||||
{% if db.root_pw is defined %}
|
||||
MARIADB_ROOT_PASSWORD: {{ db.root_pw }}
|
||||
{% endif %}
|
||||
MARIADB_USER: {{ TMPL_DB_USER }}
|
||||
MARIADB_PASSWORD: {{ TMPL_DB_USER_PW }}
|
||||
MARIADB_DATABASE: {{ TMPL_DB_DB }}
|
||||
MARIADB_USER: {{ db.user }}
|
||||
MARIADB_PASSWORD: {{ db.user_pw }}
|
||||
MARIADB_DATABASE: {{ db.db }}
|
||||
volumes:
|
||||
{% if db.data_path_mysql is defined %}
|
||||
- {{ data_path_mysql }}:/var/lib/mysql
|
||||
{% else %}
|
||||
- ./src/db/cache/mysql_data:/var/lib/mysql
|
||||
{% endif %}
|
||||
{% if db.data_path_mysql_backup is defined %}
|
||||
- {{ data_path_mysql_backup }}:/var/lib/mysql
|
||||
{% else %}
|
||||
- ./src/db/cache/mysql_backup:/var/lib/mysql
|
||||
{% endif %}
|
||||
{%- if db.data_path_mysql is defined %}
|
||||
- {{ db.data_path_mysql }}:/var/lib/mysql
|
||||
{%- else %}
|
||||
- ./db/cache/mysql_data:/var/lib/mysql
|
||||
{%- endif %}
|
||||
{%- if db.data_path_mysql_backup is defined %}
|
||||
- {{ db.data_path_mysql_backup }}:/var/lib/mysql
|
||||
{%- else %}
|
||||
- ./db/cache/mysql_backup:/var/lib/mysql
|
||||
{%- endif %}
|
||||
ports:
|
||||
{% if db.port is defined %}
|
||||
- {{ port }}:3306
|
||||
{% else %}
|
||||
{%- if db.port is defined %}
|
||||
- {{ db.port }}:3306
|
||||
{%- else %}
|
||||
- 3306:3306
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{%- endif %}
|
||||
{%- endif %}
|
||||
Reference in New Issue
Block a user