~skiqqy/apiCosmic

c0dfae40ac6a0a369b8b2cce3ef4f0bc32df839b — Stephen Cochrane 9 months ago adadc26
Very good prog on base, have todosql integrated nicely with axes
M .gitignore => .gitignore +1 -0
@@ 5,3 5,4 @@ target/
.project
.classpath
.factorypath
/cosmic/

M pom.xml => pom.xml +53 -0
@@ 23,9 23,18 @@
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>17</java.version>

        <jackson.version>2.14.2</jackson.version>
    </properties>

    <dependencies>
        <!-- Skiqqy Modules -->
        <dependency>
            <groupId>xyz.skiqqy.todosql</groupId>
            <artifactId>todo.sql</artifactId>
            <version>1.0.0</version>
        </dependency>

        <!-- Skiqqy deps -->
        <dependency>
            <groupId>xyz.skiqqy.axes</groupId>


@@ 33,11 42,37 @@
            <version>2.0.0-alpha</version>
        </dependency>

        <dependency>
            <groupId>xyz.skiqqy.cosmic</groupId>
            <artifactId>lib</artifactId>
            <version>1.0.0</version>
        </dependency>

        <dependency>
            <groupId>xyz.skiqqy.comfy</groupId>
            <artifactId>comfy</artifactId>
            <version>1.1.0</version>
        </dependency>

        <!-- Spring deps -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>3.0.3</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-to-slf4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-slf4j-impl</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-classic</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>


@@ 48,6 83,24 @@
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.19.0</version>

M scripts/init-test-container.sh => scripts/init-test-container.sh +5 -4
@@ 20,7 20,7 @@ then
    exit 1
fi

port=8199
port=${1:-8199}
pgpass=localhost:$port:test:test:test
export PGPASSFILE="$HOME/.pgpass"
if [ ! -f "$HOME/.pgpass" ]


@@ 33,17 33,18 @@ fi
chmod 600 "$HOME/.pgpass"

echo "Creating container, using $contd"
$contd stop xyz.skiqqy.axes 2> /dev/null
name=xyz.skiqqy.cosmic.$port
$contd stop "$name" 2> /dev/null
$contd run \
    -p "$port:5432" \
    --rm \
    --name xyz.skiqqy.axes \
    --name "$name" \
    -e POSTGRES_USER=test \
    -e POSTGRES_PASSWORD=test \
    -d postgres

# Wait for the container to start
while ! psql -p 8199 -h localhost -c 'select 1;' test test > /dev/null 2>&1
while ! psql -p "$port" -h localhost -c 'select 1;' test test > /dev/null 2>&1
do
    echo 'Waiting for container...'
    sleep 1

A scripts/init-test-env.sh => scripts/init-test-env.sh +14 -0
@@ 0,0 1,14 @@
#!/usr/bin/env bash
# Used to setup a manual testing environment

ROOT=$(realpath "$(dirname "$0")/..") # This sets the ROOT to root of the repo

cd "$ROOT" || return 1

bash scripts/init-test-container.sh 8199 # Create axes database
bash scripts/init-test-container.sh 8200 # Create todosql database

# Create test root user
printf 'y\nuser\npass\n' | \
    java -cp target/lib/*:target/lib/axes-2.0.0-alpha.jar \
    xyz.skiqqy.axes.Main jdbc:postgresql://localhost:8199/test test test

A src/main/java/xyz/skiqqy/cosmic/config/Modules.java => src/main/java/xyz/skiqqy/cosmic/config/Modules.java +38 -0
@@ 0,0 1,38 @@
package xyz.skiqqy.cosmic.config;

import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Configuration;

import xyz.skiqqy.comfy.Manager;
import xyz.skiqqy.cosmic.lib.module.ModuleConf;
import xyz.skiqqy.cosmic.provider.DirectoryProvider;

// Provides the config beans for cosmic modules

@Configuration
public class Modules {
    private Logger log = LogManager.getLogger(Modules.class);
    private Manager<ModuleConf> cache;

    @Autowired private DirectoryProvider directoryProvider;
    @Autowired private ConfigurableListableBeanFactory beanFactory;

    @PostConstruct
    private void init() {
        cache = new Manager(directoryProvider.getConfigPath(), ModuleConf.class);
        cache.refresh(); // Setup the module conf cache

        // Register the beans
        List<ModuleConf> confs = cache.get(ModuleConf.class); // Return all confs
        for (ModuleConf conf : confs) {
            beanFactory.registerSingleton(conf.getName(), conf);
        }
    }
}

A src/main/java/xyz/skiqqy/cosmic/config/Providers.java => src/main/java/xyz/skiqqy/cosmic/config/Providers.java +23 -0
@@ 0,0 1,23 @@
package xyz.skiqqy.cosmic.config;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import xyz.skiqqy.cosmic.provider.DirectoryProvider;
import xyz.skiqqy.cosmic.provider.impl.DirectoryProviderImpl;

@Configuration
public class Providers {
    @Autowired Environment env;

    @Bean
    public DirectoryProvider directoryProvider() throws IOException {
        String dir  = env.getProperty("cosmic.dir");
        // If no user dir is specified, default to root at current working dir, under cosmic dir
        return new DirectoryProviderImpl(dir  == null ? "cosmic" : dir, true);
    }
}

A src/main/java/xyz/skiqqy/cosmic/provider/DirectoryProvider.java => src/main/java/xyz/skiqqy/cosmic/provider/DirectoryProvider.java +13 -0
@@ 0,0 1,13 @@
package xyz.skiqqy.cosmic.provider;

import java.nio.file.Path;

/* Interface for getting information about the working directory
 *
 */

public interface DirectoryProvider {
    Path getRootPath(); // Returns the path to the configs directory

    Path getConfigPath(); // Returns the path to the configs directory
}

A src/main/java/xyz/skiqqy/cosmic/provider/impl/DirectoryProviderImpl.java => src/main/java/xyz/skiqqy/cosmic/provider/impl/DirectoryProviderImpl.java +53 -0
@@ 0,0 1,53 @@
package xyz.skiqqy.cosmic.provider.impl;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import xyz.skiqqy.cosmic.provider.DirectoryProvider;

public class DirectoryProviderImpl implements DirectoryProvider {
    private Path root;
    private Path conf;

    // Returns a string path, substitues $HOME with /home/$USER, with $USER being the current user
    private static Path stringToPath(String path) {
        return Paths.get(path.replaceAll("\\$HOME", System.getenv("HOME")));
    }

    public DirectoryProviderImpl(String root, boolean make) throws IOException {
        this(stringToPath(root), make);
    }

    public DirectoryProviderImpl(String root) throws IOException {
        this(stringToPath(root), false);
    }

    public DirectoryProviderImpl(Path root) throws IOException {
        this(root, false);
    }

    public DirectoryProviderImpl(Path root, boolean make) throws IOException {
        root = root.toAbsolutePath();
        System.setProperty("user.dir", root.toString()); // Update the working directory

        this.root = root;
        this.conf = root.resolve("conf");

        if (make) {
            Files.createDirectories(root);
            Files.createDirectories(conf);
        }
    }

    @Override
    public Path getRootPath() {
        return root;
    }

    @Override
    public Path getConfigPath() {
        return conf;
    }
}

M src/main/resources/application.properties => src/main/resources/application.properties +5 -1
@@ 1,1 1,5 @@
debug=true
#cosmic.dir=$HOME/.config/cosmic # Specify the working directory for cosmic
debug=false

# I want all my apps to log
logging.level.xyz.skiqqy=debug

M src/main/resources/log4j2.properties => src/main/resources/log4j2.properties +9 -7
@@ 1,17 1,19 @@
# This is just a starter config, and should be changed where needed.
# Extra logging related to initialization of Log4j
# Set to debug or trace if log4j initialization is failing
status = debug
#status = warn
# Name of the configuration
name = ConsoleLogConfigDemo
#name = ConsoleLogConfigDemo

# Root logger level
rootLogger.level = warn
# Root logger referring to console appender
rootLogger.appenderRef.stdout.ref = consoleLogger

log4j.catagory.xyz.skiqqy=debug

# Console appender configuration
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p [%t :: %c] - %m%n

# Root logger level
rootLogger.level = debug
# Root logger referring to console appender
rootLogger.appenderRef.stdout.ref = consoleLogger