77import org .springframework .http .MediaType ;
88import org .springframework .http .ResponseEntity ;
99import org .springframework .web .bind .annotation .*;
10- import org .springframework .web .util .WebUtils ;
1110import pl .simplemethod .codebin .linkDeploy .LinkClient ;
1211import pl .simplemethod .codebin .model .Containers ;
1312import pl .simplemethod .codebin .model .Images ;
1615import pl .simplemethod .codebin .repository .ImagesRepository ;
1716import pl .simplemethod .codebin .repository .UsersRepository ;
1817
19- import javax .servlet .http .Cookie ;
20- import javax .servlet .http .HttpServletResponse ;
2118import java .time .Instant ;
2219import java .util .ArrayList ;
2320import 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