Skip to content
This repository was archived by the owner on Mar 5, 2020. It is now read-only.

Commit 6230f3e

Browse files
committed
Adding a container view pages
1 parent 247451d commit 6230f3e

File tree

12 files changed

+267
-259
lines changed

12 files changed

+267
-259
lines changed

src/main/java/pl/simplemethod/codebin/ContextWrapper.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/main/java/pl/simplemethod/codebin/SecurityConfig.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package pl.simplemethod.codebin;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
43
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.http.HttpMethod;
55
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6-
import org.springframework.security.config.annotation.web.builders.WebSecurity;
76
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
87
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
98
import org.springframework.security.oauth2.client.web.AuthorizationRequestRepository;
@@ -16,7 +15,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
1615

1716
@Override
1817
protected void configure(HttpSecurity http) throws Exception {
19-
http.authorizeRequests().antMatchers("/**").permitAll();
18+
http.authorizeRequests().antMatchers(HttpMethod.GET, "/**").permitAll();
19+
http.csrf().disable();
20+
http.cors().disable();
2021
/* http.authorizeRequests()
2122
.antMatchers("/", "/**", "/postlogin", "/v1.0/**", "/404", "/logowanie/github")
2223
.permitAll()

src/main/java/pl/simplemethod/codebin/githubOauth/GithubRestController.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import org.springframework.http.MediaType;
1010
import org.springframework.http.ResponseEntity;
1111
import org.springframework.web.bind.annotation.*;
12+
import pl.simplemethod.codebin.model.Containers;
1213
import pl.simplemethod.codebin.model.Users;
14+
import pl.simplemethod.codebin.repository.ContainersRepository;
1315
import pl.simplemethod.codebin.repository.UsersRepository;
1416

1517

@@ -23,6 +25,8 @@ public class GithubRestController {
2325
@Autowired
2426
UsersRepository usersRepository;
2527

28+
@Autowired
29+
private ContainersRepository containersRepository;
2630
/**
2731
* Returns the necessary information about the repository
2832
*
@@ -125,6 +129,7 @@ ResponseEntity getInfoAboutUser(@CookieValue("token") String token, @PathVariabl
125129
public @ResponseBody
126130
ResponseEntity checkToken(@RequestParam("token") String token) {
127131
HttpHeaders headers = new HttpHeaders();
132+
headers.setContentType(MediaType.APPLICATION_JSON);
128133
org.json.JSONObject body = new org.json.JSONObject();
129134
try {
130135
Users users = usersRepository.getFirstByToken(token);
@@ -157,6 +162,7 @@ ResponseEntity checkToken(@RequestParam("token") String token) {
157162
public @ResponseBody
158163
ResponseEntity getpublicrepos(@CookieValue("token") String token) {
159164
HttpHeaders headers = new HttpHeaders();
165+
headers.setContentType(MediaType.APPLICATION_JSON);
160166
JSONParser parser = new JSONParser();
161167
org.json.JSONObject body = new org.json.JSONObject();
162168
org.json.simple.JSONArray result = new org.json.simple.JSONArray();
@@ -183,6 +189,17 @@ ResponseEntity getpublicrepos(@CookieValue("token") String token) {
183189
helper.put("html_url", obj1.get("html_url"));
184190
helper.put("description", obj1.get("description"));
185191
helper.put("created_at", obj1.get("created_at"));
192+
193+
Containers containers = containersRepository.getFirstByName(obj1.get("id").toString());
194+
if(containers!=null)
195+
{
196+
helper.put("container_create", obj1.get("id"));
197+
198+
}
199+
else
200+
{
201+
helper.put("container_create", null);
202+
}
186203
result.add(helper);
187204
}
188205
});

src/main/java/pl/simplemethod/codebin/repository/ContainersRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public interface ContainersRepository extends JpaRepository<Containers, Long> {
1919

2020
Containers getFirstByShareUrl(String shareURL);
2121

22+
Containers getFirstByName(String name);
23+
2224
List<Containers> getByHostPorts(Integer ports);
2325

2426
List<Containers> getByIdDocker(String dockerId);

src/main/java/pl/simplemethod/codebin/srv/SrvClient.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,33 @@ protected org.json.JSONObject restartContainer(String id) {
247247
return body;
248248
}
249249
}
250+
251+
/**
252+
* The method returns top process inside container data
253+
*
254+
* @param id Container ID
255+
* @return Json object with status
256+
*/
257+
protected String topContainer(String id) {
258+
org.json.JSONObject body = new org.json.JSONObject();
259+
try {
260+
HttpResponse<String> topContainer = Unirest.get(SERVER_URL + "/v1.0/containers/" + id + "/top")
261+
.header("accept", "application/json").header("Content-Type", "application/json").queryString("ps_args","").asString();
262+
if (topContainer.getStatus() != 200) {
263+
body.put("message", "Something went wrong ");
264+
body.put("status", topContainer.getStatus());
265+
return body.toString();
266+
} else {
267+
return topContainer.getBody();
268+
}
269+
270+
} catch (NullPointerException | org.json.JSONException | UnirestException e) {
271+
body.put("message", "Something went wrong " + e);
272+
body.put("status", 510);
273+
return body.toString();
274+
}
275+
}
276+
250277
/**
251278
* The method returns container data
252279
*

src/main/java/pl/simplemethod/codebin/srv/SrvRestController.java

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.springframework.http.MediaType;
88
import org.springframework.http.ResponseEntity;
99
import org.springframework.web.bind.annotation.*;
10-
import org.springframework.web.util.WebUtils;
1110
import pl.simplemethod.codebin.linkDeploy.LinkClient;
1211
import pl.simplemethod.codebin.model.Containers;
1312
import pl.simplemethod.codebin.model.Images;
@@ -16,8 +15,6 @@
1615
import pl.simplemethod.codebin.repository.ImagesRepository;
1716
import pl.simplemethod.codebin.repository.UsersRepository;
1817

19-
import javax.servlet.http.Cookie;
20-
import javax.servlet.http.HttpServletResponse;
2118
import java.time.Instant;
2219
import java.util.ArrayList;
2320
import java.util.List;
@@ -41,21 +38,61 @@ public class SrvRestController {
4138
@Autowired
4239
private UsersRepository usersRepository;
4340

44-
/*
45-
@GetMapping("/createtest")
41+
/**
42+
* List of containers of any user
43+
*
44+
* @param id User ID
45+
* @return Object Json with data
46+
*/
47+
@GetMapping("users/{ID}/container")
4648
public @ResponseBody
47-
ResponseEntity kek() {
48-
49-
List<Containers> containers = new ArrayList<>();
50-
Images images = imagesRepository.getFirstByName("java");
51-
containers.add(new Containers("test","test",images,1010,4510,(long)1000,(long)1000,1, Instant.now().getEpochSecond()));
52-
containers.forEach(containersRepository::save);
49+
ResponseEntity usersContainer(@PathVariable(value = "ID") String id) {
50+
Users users = usersRepository.getFirstById(Integer.valueOf(id));
51+
HttpHeaders headers = new HttpHeaders();
52+
headers.setContentType(MediaType.APPLICATION_JSON);
53+
List<Containers> containers = users.getContainers();
54+
org.json.JSONObject result = new org.json.JSONObject();
55+
result.put("containers", containers);
56+
return new ResponseEntity<>(result.toString(), headers, HttpStatus.valueOf(200));
57+
}
5358

59+
/**
60+
* List of containers for login user
61+
*
62+
* @param id Current logged user
63+
* @return Object Json with data
64+
*/
65+
@GetMapping("user/container")
66+
public @ResponseBody
67+
ResponseEntity userContainer(@CookieValue("id") String id) {
5468
HttpHeaders headers = new HttpHeaders();
55-
return new ResponseEntity<>("xd", headers, HttpStatus.valueOf(200));
69+
headers.setContentType(MediaType.APPLICATION_JSON);
70+
Users users = usersRepository.getFirstById(Integer.valueOf(id));
71+
List<Containers> containers = users.getContainers();
72+
org.json.JSONObject result = new org.json.JSONObject();
73+
result.put("containers", containers);
74+
return new ResponseEntity<>(result.toString(), headers, HttpStatus.valueOf(200));
5675
}
5776

58-
*/
77+
/**
78+
* Returns container information from the database
79+
*
80+
* @param dockerGithubId Name of container
81+
* @return Object Json with data
82+
*/
83+
@GetMapping("user/container/info")
84+
public @ResponseBody
85+
ResponseEntity userContainerCheck(@RequestParam("dockergithubid") String dockerGithubId) {
86+
HttpHeaders headers = new HttpHeaders();
87+
headers.setContentType(MediaType.APPLICATION_JSON);
88+
Containers containers = containersRepository.getFirstByName(dockerGithubId);
89+
if (containers != null) {
90+
91+
return new ResponseEntity<>(containers, headers, HttpStatus.valueOf(200));
92+
} else {
93+
return new ResponseEntity<>("error", headers, HttpStatus.valueOf(400));
94+
}
95+
}
5996

6097
/**
6198
* REST for container creation
@@ -68,7 +105,7 @@ ResponseEntity kek() {
68105
* @param diskQuota Maximum amount of allocated memory on the disk
69106
* @return Json object with data
70107
*/
71-
@GetMapping("container/create")
108+
@PostMapping("container/create")
72109
public @ResponseBody
73110
ResponseEntity createContainerForUser(@RequestParam("dockerimage") String dockerImage, @RequestParam("exposedports") Integer exposedPorts, @RequestParam("hostports") Integer hostPorts, @RequestParam("name") String name, @RequestParam("rammemory") Long ramMemory, @RequestParam("diskquota") Long diskQuota, @CookieValue("id") String id) {
74111
HttpHeaders headers = new HttpHeaders();
@@ -142,6 +179,21 @@ ResponseEntity startContainer(@PathVariable(value = "ID") String containerId) {
142179
return new ResponseEntity<>(response.toString(), headers, HttpStatus.valueOf(response.getInt("status")));
143180
}
144181

182+
/**
183+
* The method returns information about processes inside container
184+
*
185+
* @param containerId Container ID
186+
* @return Json object as response
187+
*/
188+
@GetMapping("container/{ID}/top")
189+
public @ResponseBody
190+
ResponseEntity topContainer(@PathVariable(value = "ID") String containerId) {
191+
HttpHeaders headers = new HttpHeaders();
192+
headers.setContentType(MediaType.APPLICATION_JSON);
193+
String response = srvClient.topContainer(containerId);
194+
return new ResponseEntity<>(response, headers, HttpStatus.valueOf(200));
195+
}
196+
145197
/**
146198
* The method returns container logs
147199
*
@@ -152,11 +204,14 @@ ResponseEntity startContainer(@PathVariable(value = "ID") String containerId) {
152204
public @ResponseBody
153205
ResponseEntity logsContainer(@PathVariable(value = "ID") String containerId) {
154206
HttpHeaders headers = new HttpHeaders();
207+
headers.setContentType(MediaType.APPLICATION_JSON);
155208
String response = srvClient.logsContainer(containerId);
156209
String replaceString = response.replace('\u0001', '\n');
157210
replaceString = replaceString.replace('�', ' ');
158211

159-
return new ResponseEntity<>(replaceString, headers, HttpStatus.valueOf(201));
212+
org.json.JSONObject result = new org.json.JSONObject();
213+
result.put("logs",replaceString);
214+
return new ResponseEntity<>(result.toString(), headers, HttpStatus.valueOf(201));
160215
}
161216

162217
/**

0 commit comments

Comments
 (0)