Skip to content

Commit

Permalink
Merge branch 'dev' into monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
Magmose authored Mar 14, 2023
2 parents c281b71 + 68eddb6 commit 5854e5e
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 7 deletions.
105 changes: 105 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
name: Continuous Development

on:
push:
# Run workflow every time something is pushed to the main branch
branches:
- dev
# allow manual triggers for now too
# Remember to set the following secrets in your repository's settings:
# https://github.com/your_username/itu-minitwit-ci/settings/secrets/actions
# DOCKER_USERNAME
# DOCKER_PASSWORD
# SSH_USER
# SSH_KEY
# SSH_HOST

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push minitwitimage
uses: docker/build-push-action@v4
with:
context: ./MiniTwit
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/minitwitimagedev:latest

# - name: Build and push mysqlimage
# uses: docker/build-push-action@v2
# with:
# context: .
# file: ./Dockerfile-mysql
# push: true
# tags: ${{ secrets.DOCKER_USERNAME }}/mysqlimage:latest
# cache-from: type=registry,ref=${{ secrets.DOCKER_USERNAME }}/mysqlimage:mysqlbuildcache
# cache-to: type=registry,ref=${{ secrets.DOCKER_USERNAME }}/mysqlimage:mysqlbuildcache,mode=max

# - name: Build and push flagtoolimage
# uses: docker/build-push-action@v2
# with:
# context: .
# file: ./Dockerfile-flagtool
# push: true
# tags: ${{ secrets.DOCKER_USERNAME }}/flagtoolimage:latest
# cache-from: type=registry,ref=${{ secrets.DOCKER_USERNAME }}/flagtoolimage:flagtoolbuildcache
# cache-to: type=registry,ref=${{ secrets.DOCKER_USERNAME }}/flagtoolimage:flagtoolbuildcache,mode=max

# - name: Test minitwit
# run: |
# docker build -t $DOCKER_USERNAME/minitwittestimage -f Dockerfile-minitwit-tests .
# yes 2>/dev/null | docker-compose up -d
# docker run --rm --network=itu-minitwit-network $DOCKER_USERNAME/minitwittestimage
# env:
# DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}

# - name: Configure SSH
# run: |
# mkdir -p ~/.ssh/
# echo "$SSH_KEY" > ~/.ssh/minitwit.key
# chmod 600 ~/.ssh/minitwit.key
# env:
# SSH_KEY: ${{ secrets.SSH_KEY }}

- name: DEVployment
uses: appleboy/[email protected]
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_KEY }}
script: |
docker pull magmose1/minitwitimagedev:latest
docker stop minidev || true && docker rm minidev || true
docker run -d -p 8082:8080 --name minidev magmose1/minitwitimagedev:latest
docker ps
# - name: Deploy to server
# # Configure the ~./bash_profile and deploy.sh file on the Vagrantfile
# run: |
# ssh -i ~/.ssh/minitwit.key $SSH_USER@$SSH_HOST -o StrictHostKeyChecking=no
# whoami
# docker ps -a
# docker images
# docker pull magmose1/minitwitimage:latest
# docker ps
# docker stop minitwit
# docker rm minitwit
# docker run -d -p 8082:8080 --name mini3 magmose1/minitwitimage:latest
# env:
# SSH_USER: ${{ secrets.SSH_USER }}
# SSH_HOST: ${{ secrets.SSH_HOST }}
2 changes: 1 addition & 1 deletion MiniTwit/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ WORKDIR /app-service


EXPOSE 8080
ENTRYPOINT ["java","-jar","MiniTwit-0.0.5.war"]
ENTRYPOINT ["java","-jar","MiniTwit-0.0.5.war"]
Binary file modified MiniTwit/minitwit.db
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ public Object publicTimeline(Model model, HttpServletRequest request) {
return "timeline.html";
}

@GetMapping("/favourites")
public String favourites (@PathVariable("username") String username, HttpServletRequest request, Model model) throws SQLException {
HttpSession session = request.getSession(false);
model.addAttribute("public", "false");
model.addAttribute("username", username);
Boolean loggedIn = addUserToModel(model, session);

if (loggedIn) {
if (username.equals("favicon.ico")) return "redirect:/public"; // Unsure if this if is necessary, but we've kept it in.
}

List<Map<String, Object>> messages;
try {
List<Object> args = new ArrayList<>();
args.add(getUserID(username));
args.add(PER_PAGE);
messages = sqLite.queryDb("select message.*, user.* from message, user inner join favourite on message.id = favourite.message_id where favourite.user_id = ? order by message.pub_date desc limit ?", args);
System.out.println(messages);
} catch (SQLException | ClassNotFoundException e) {
System.out.println("ERROR_" + e);
return "Error";
}
addDatesAndGravatarURLs(messages);

model.addAttribute("messages", messages);
model.addAttribute("messagesSize", messages.size());

return "favourites.html";
}


@GetMapping("/{username}")
public String userTimeLine(@PathVariable("username") String username, HttpServletRequest request, Model model) throws SQLException, ClassNotFoundException {
Expand Down Expand Up @@ -205,6 +235,41 @@ public String addMessage(AddMessage text, HttpServletRequest request, Model mode

return "redirect:/public";
}

@PostMapping("/addMessageToFavourites")
public String addMessageToFavourites(@PathVariable("messageID") String messageID, HttpServletRequest request, Model model) throws SQLException, ClassNotFoundException {
HttpSession session = request.getSession(false);

if (session != null && session.getAttribute("user_id") == null) {
return "401";
}

List<Object> args = new ArrayList<>();
args.add(session.getAttribute("user_id"));
args.add(messageID);
int result = sqLite.updateDb("insert into favourite (user_id, message_id) values (?, ?)", args);
// CREATE TABLE favourite (user_id integer REFERENCES user(id),message_id integer REFERENCES message(id))

return "redirect:/public";
}

@PostMapping("/removeMessageToFavourites")
public String removeMessageToFavourites(@PathVariable("messageID") String messageID, HttpServletRequest request, Model model) throws SQLException, ClassNotFoundException {
HttpSession session = request.getSession(false);

if (session != null && session.getAttribute("user_id") == null) {
return "401";
}

List<Object> args = new ArrayList<>();
args.add(session.getAttribute("user_id"));
args.add(messageID);
int result = sqLite.updateDb("delete from favourite (user_id, message_id) values (?, ?)", args);
//int result = sqLite.updateDb("delete from favourite where user_id = ? and message_id = ?", args);

return "redirect:/favourites";
}


@RequestMapping(value = "/login", method = {RequestMethod.GET, RequestMethod.POST})
public String login(@ModelAttribute Login login, Model model, HttpServletRequest request) throws SQLException, ClassNotFoundException {
Expand Down
14 changes: 9 additions & 5 deletions MiniTwit/src/main/java/dk/itu/minitwit/database/SQLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
@Component
public class SQLite {

// private final String DATABASE_URL = "/tmp/minitwit.db";
// private final String DATABASE_URL = "minitwit.db";

private final String DATABASE_URL = "mysql://db-minitwit-do-user-13625042-0.b.db.ondigitalocean.com:25060/defaultdb?ssl-mode=REQUIRED";
private final boolean DEBUG = true;
private final String SECRET_KEY = "development key";
Expand All @@ -28,12 +29,15 @@ public class SQLite {
private PasswordEncoder passwordEncoder;


private Connection connectDb() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
// private Connection connectDb() throws SQLException, ClassNotFoundException {
// return DriverManager.getConnection("jdbc:sqlite:" + DATABASE_URL);
// }

return DriverManager.getConnection("jdbc:" + DATABASE_URL, "doadmin", "AVNS_W7vkzWZhBrw3fUsgp71");
}
private Connection connectDb() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");

return DriverManager.getConnection("jdbc:" + DATABASE_URL, "doadmin", "AVNS_W7vkzWZhBrw3fUsgp71");
}


public void initDb() throws SQLException {
Expand Down
52 changes: 52 additions & 0 deletions MiniTwit/src/main/resources/templates/favourites.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link th:href="@{/styles/style.css}" rel="stylesheet" type="text/css" href="/styles/style.css" media="all"/>
</head>

<body>
<div class="page" th:fragment="header">
<h1>MiniTwit</h1>
<div class="navigation">
<div th:if="${user != 'false'}">
<a th:href="@{/{user}(user=${user})}">my timeline</a> |
<a th:href="@{/favourites}">my favourites</a> |
<a th:href="@{/public}">public</a> |
<a th:href="@{/logout}">sign out [<span th:text="${user}">Username</span>]</a>
</div>
<div th:if="${user == 'false'}">
<a th:href="@{/public}">public</a> |
<a th:href="@{/register}">sign up</a> |
<a th:href="@{/login}">sign in</a>
</div>
</div>
<div class="body">
<div th:if="${public == 'true' && user != 'false'}" class="twitbox">
<h3>These are your favourite twits, <span th:text="${username}"></span>!</h3>
</div>
<ul class="messages">
<li th:each="message : ${messages}" th:if="${messagesSize > 0}">
<img th:src="${message.gravatar_url}" width="40" height="40"/>
<p>
<strong>
<a th:href="@{/{username}(username=${message.username})}"
th:text="${message.username}">
</a>
</strong>
<span th:text="${message.text}"></span>
<small>&mdash; <span th:text="${message.date_time}"></span></small><br>
<a class="favourite" th:href="@{/removeMessageToFavourites(messageID=${message.id})}">
Remove from favourites
</a>
</p>
</li>
<li th:if="${messagesSize == 0}">
<em>There's no favourite messages so far.</em>
</li>
</ul>
</div>
</div>
</body>

</html>
5 changes: 4 additions & 1 deletion MiniTwit/src/main/resources/templates/timeline.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ <h3>What's on your mind <span th:text="${username}"></span>?</h3>
</a>
</strong>
<span th:text="${message.text}"></span>
<small>&mdash; <span th:text="${message.date_time}"></span></small>
<small>&mdash; <span th:text="${message.date_time}"></span></small><br>
<a class="favourite" th:href="@{/addMessageToFavourites/{messageID}(messageID=${message.message_id})}">
Add to favourites
</a>
</p>
</li>
<li th:if="${messagesSize == 0}">
Expand Down

0 comments on commit 5854e5e

Please sign in to comment.