Skip to content
Snippets Groups Projects
Commit 3391eb93 authored by Petr Ježek's avatar Petr Ježek
Browse files

main docker compose, pdftex fastapi, algos

parent a0324533
No related branches found
No related tags found
No related merge requests found
Showing
with 276 additions and 306 deletions
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### VS Code ###
.vscode/
### etmt specific ###
/out
File moved
version: "3.8"
services:
java-17:
jvm:
container_name: jvm
build: etmt-be
ports:
- "5050:5050"
depends_on:
- mongodb
environment:
- DB_USER=rootuser
- DB_PSW=rootpass
- DB_HOST=mongodb
volumes:
- "./tex:/tmp/pdftex"
fa-pdflatex:
container_name: fa-pdflatex
build: fa-pdflatex
ports:
- "5100:8000"
volumes:
- "./tex:/tmp/pdftex"
environment:
- LC_ALL=C.UTF-8
- LANG=C.UTF-8
mongodb:
container_name: mongodb
image : mongo
image: mongo
ports:
- "27017:27017"
volumes:
- "data:/data"
- "db:/db"
environment:
- MONGO_INITDB_ROOT_USERNAME=rootuser
- MONGO_INITDB_ROOT_PASSWORD=rootpass
......@@ -23,8 +44,8 @@ services:
- ME_CONFIG_MONGODB_SERVER=mongodb
restart: always
volumes:
data: {}
tex:
db: {}
networks:
default:
name: mongodb_network
......@@ -14,7 +14,7 @@ COPY src ./src
RUN --mount=type=cache,target=/root/.m2 mvn clean package -Dmaven.test.skip
# split the built app into multiple layers to improve layer rebuild
RUN mkdir -p target/docker-packaging && cd target/docker-packaging && jar -xf ../my-app*.jar
RUN mkdir -p target/docker-packaging && cd target/docker-packaging && jar -xf ../etmt-0.0.1-SNAPSHOT*.jar
########JRE run stage########
FROM openjdk:17.0-slim
......@@ -29,4 +29,4 @@ COPY --from=maven_build ${DOCKER_PACKAGING_DIR}/META-INF /app/META-INF
#run the app
CMD java -cp .:classes:lib/* \
-Djava.security.egd=file:/dev/./urandom \
foo.bar.MySpringBootApplication
\ No newline at end of file
cz.cvut.fel.pro.emtt.App
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.6</version>
<relativePath>pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>cz.cvut.fel.pro</groupId>
<artifactId>etmt</artifactId>
<name>etmt</name>
<version>0.0.1-SNAPSHOT</version>
<description>etmt</description>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
<excludeDevtools>false</excludeDevtools>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.5.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.5.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.5.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.5.6</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>spring-boot-test</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
<exclusion>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
<exclusion>
<artifactId>json-path</artifactId>
<groupId>com.jayway.jsonpath</groupId>
</exclusion>
<exclusion>
<artifactId>assertj-core</artifactId>
<groupId>org.assertj</groupId>
</exclusion>
<exclusion>
<artifactId>hamcrest</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
<exclusion>
<artifactId>junit-jupiter</artifactId>
<groupId>org.junit.jupiter</groupId>
</exclusion>
<exclusion>
<artifactId>mockito-core</artifactId>
<groupId>org.mockito</groupId>
</exclusion>
<exclusion>
<artifactId>mockito-junit-jupiter</artifactId>
<groupId>org.mockito</groupId>
</exclusion>
<exclusion>
<artifactId>jsonassert</artifactId>
<groupId>org.skyscreamer</groupId>
</exclusion>
<exclusion>
<artifactId>spring-test</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>xmlunit-core</artifactId>
<groupId>org.xmlunit</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>5.5.3</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>spring-test</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.5.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.0-alpha4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.26</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
......@@ -28,7 +28,7 @@ public class App {
log.info("question with id: {} is: {}", q.getId(), repository.findById(q.getId()));
log.info("number of questions with title: {} is: {}", q.getTitle(), repository.findAllQuestionsByTitle(q.getTitle()).size());
latexService.build();
// latexService.build();
};
}
}
package cz.cvut.fel.pro.emtt.service;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfName;
import com.lowagie.text.pdf.PdfString;
import com.lowagie.text.pdf.PdfWriter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.FileOutputStream;
import java.io.IOException;
@Service
@Slf4j
public class PDFService {
public void createDocument() {
log.info("creating a document ...");
var document = new Document();
try {
final PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("myDocument.pdf"));
document.open();
pdfWriter.getInfo().put(PdfName.CREATOR, new PdfString(Document.getVersion()));
document.add(new Paragraph("This is my first document!"));
Paragraph p1 = new Paragraph(new Chunk(
"This is my first paragraph. ",
FontFactory.getFont(FontFactory.HELVETICA, 10)));
p1.add("The leading of this paragraph is calculated automagically. ");
p1.add("The default leading is 1.5 times the fontsize. ");
p1.add(new Chunk("You can add chunks "));
p1.add(new Phrase("or you can add phrases. "));
p1.add(new Phrase(
"Unless you change the leading with the method setLeading, the leading doesn't change if you add text with another leading. This can lead to some problems.",
FontFactory.getFont(FontFactory.HELVETICA, 18)));
document.add(p1);
Paragraph p2 = new Paragraph(new Phrase(
"This is my second paragraph. ", FontFactory.getFont(
FontFactory.HELVETICA, 12)));
p2.add("As you can see, it started on a new line.");
document.add(p2);
Paragraph p3 = new Paragraph("This is my third paragraph.",
FontFactory.getFont(FontFactory.HELVETICA, 12));
document.add(p3);
} catch (DocumentException | IOException e) {
log.error(e.getMessage());
}
document.close();
log.info("document created successfully");
}
}
from random import randrange
# generates all possible combinations of questions for a given sum of points
def subset_sum_iter(array, target):
array = sorted(array, key=lambda tup: tup[1])
last_index = {0: [-1]}
for i in range(len(array)):
for s in list(last_index.keys()):
new_s = s + array[i][1]
if 0 < (new_s - target):
pass # Cannot lead to target
elif new_s in last_index:
last_index[new_s].append(i)
else:
last_index[new_s] = [i]
# Now yield up the answers.
def recur(new_target, max_i):
for i in last_index[new_target]:
if i == -1:
yield [] # Empty sum.
elif max_i <= i:
break # Not our solution.
else:
for answer in recur(new_target - array[i][1], i):
answer.append(array[i])
yield answer
for answer in recur(target, len(array)):
yield answer
def gen_questions(n):
questions = []
for i in range(n):
questions.append(tuple((f"q{i}", randrange(5, 20))))
return questions
if __name__ == "__main__":
questions = gen_questions(20)
print(questions)
for answer in subset_sum_iter(questions, 50):
print(answer)
def uniformKnapsack(items, W):
import random
def subProcedure(remaining, new_W):
# base case
if remaining == [] or min(i[0] for i in remaining) > new_W:
return []
# sample random item
random_item = random.choice(remaining)
# split input into larger and smaller subsets based on the item
smaller = [i for i in remaining if i < random_item]
not_smaller = [i for i in remaining if random_item <= i]
# compute the total cost of all smaller items
smaller_cost = sum(i[0] for i in smaller)
if smaller_cost <= new_W:
# include all smaller elements and recurse on the rest with a smaller W
return smaller + subProcedure(not_smaller, new_W - smaller_cost)
else:
# recurse on the smaller subset with the same W
return subProcedure(smaller, new_W)
# convert a list of items (a, b, c) to a list of the form ((a, 0), (b, 1), (c, 2)) for
# consistent tie breaking in comparison operations
augmented_items = [(j, i) for (i, j) in enumerate(items)]
# compute the solution over the augmented items
solution = subProcedure(augmented_items, W)
# return the un-augmented items from the solution
return [i[0] for i in solution]
from random import randrange
# Function to check if all subsets are filled or not
def checkSum(sumLeft, k):
r = True
for i in range(k):
if sumLeft[i]:
r = False
return r
# Helper function for solving `k` partition problem.
# It returns true if there exist `k` subsets with the given sum
def subsetSum(S, n, sumLeft, A, k):
# return true if a subset is found
if checkSum(sumLeft, k):
return True
# base case: no items left
if n < 0:
return False
result = False
# consider current item `S[n]` and explore all possibilities
# using backtracking
for i in range(k):
if not result and (sumLeft[i] - S[n][1]) >= 0:
# mark the current element subset
A[n] = i + 1
# add the current item to the i'th subset
sumLeft[i] = sumLeft[i] - S[n][1]
# recur for remaining items
result = subsetSum(S, n - 1, sumLeft, A, k)
# backtrack: remove the current item from the i'th subset
sumLeft[i] = sumLeft[i] + S[n][1]
# return true if we get a solution
return result
# Function for solving k–partition problem. It prints the subsets if
# set `S[0…n-1]` can be divided into `k` subsets with equal sum
def partition(S, k):
# get the total number of items in `S`
n = len(S)
# base case
if n < k:
print("k-partition of set S is not possible")
return
# get the sum of all elements in the set
total = 0
for t in S:
total += t[1]
print(f"total: {total}")
A = [None] * n
# create a list of size `k` for each subset and initialize it
# by their expected sum, i.e., `sum/k`
sumLeft = [total // k] * k
# return true if the sum is divisible by `k` and set `S` can
# be divided into `k` subsets with equal sum
result = (total % k) == 0 and subsetSum(S, n - 1, sumLeft, A, k)
if not result:
print("k-partition of set S is not possible")
return
# print all k–partitions
for i in range(k):
print(f"Partition {i} is", [S[j] for j in range(n) if A[j] == i + 1])
def gen_questions(n):
questions = []
for i in range(n):
questions.append(tuple((f"q{i}", randrange(5, 10))))
return questions
if __name__ == '__main__':
S = gen_questions(50) # set of 10 questions
k = 4 # total of 4 tests
print(S)
partition(S, k)
spring.data.mongodb.uri=mongodb://rootuser:rootpass@localhost:27017/etmt
spring.data.mongodb.uri=mongodb://${DB_USER}:${DB_PSW}@${DB_HOST}:27017/etmt
spring.data.mongodb.authentication-database=admin
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
spring.application.name=etmt-be
server.port=5050
package cz.cvut.fel.pro.emtt.service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class FileServiceTest {
@Autowired
private FileService fileService;
@Test
public void testBasic() throws IOException {
fileService.createFile("testFile.tex", true);
fileService.writeToFile("testFile.tex", "\\title{Hello World}");
}
}
\ No newline at end of file
package cz.cvut.fel.pro.emtt.service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class LatexServiceTest {
@Autowired
public LatexService latexService;
@Test
public void test() throws IOException, InterruptedException {
latexService.build();
}
}
\ No newline at end of file
package cz.cvut.fel.pro.emtt.service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class PDFServiceTest {
@Autowired
private PDFService service;
@Test
void test_createDocument_shouldCreateFile() {
service.createDocument();
}
}
\ No newline at end of file
__pycache__
# Dockerfile
# pull the official docker image
FROM ubuntu:18.04
RUN apt update && apt upgrade -y && apt-get install -y python3.10 && apt-get install -y python3-pip
# set work directory
WORKDIR /app
# install dependencies
COPY requirements.txt .
RUN /usr/bin/python3 -m pip install -r requirements.txt
# copy project
COPY . .
RUN apt-get install -y --no-install-recommends texlive-latex-recommended texlive-fonts-recommended && \
# apt-get install -y --no-install-recommends texlive-latex-extra texlive-fonts-extra texlive-lang-all && \
rm -rf /var/lib/apt/lists/*
CMD ["uvicorn", "app.main:app", "--host=0.0.0.0"]
# app/main.py
from fastapi import FastAPI
from subprocess import Popen, PIPE
app = FastAPI(title="pdflatex FastAPI")
@app.get("/")
async def read_root():
return "pdflatex FastAPI is ready"
@app.post("/upload")
async def upload(filename: str):
path = "/tmp/pdftex"
cmd = f"pdflatex -interaction=nonstopmode -output-directory {path}/out {path}/{filename}"
print(f"running: {cmd}")
p = Popen(cmd, shell=True, stdout=PIPE).stdout.read()
@app.on_event("startup")
async def startup():
# setup
pass
@app.on_event("shutdown")
async def shutdown():
# shutdown
pass
fastapi==0.70.1
uvicorn==0.16.0
/out
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment