http-service: add session service

This commit is contained in:
Adam
2018-03-02 13:19:17 -05:00
parent 03bf1da654
commit f633ce604a
4 changed files with 298 additions and 0 deletions

View File

@@ -0,0 +1,95 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.service.session;
import java.time.Instant;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/session")
public class SessionController
{
private final SessionService sessionService;
@Autowired
public SessionController(SessionService sessionService)
{
this.sessionService = sessionService;
}
@RequestMapping
public UUID get(HttpServletRequest request)
{
UUID uuid = UUID.randomUUID();
String addr = request.getRemoteAddr();
Instant now = Instant.now();
SessionEntry sessionEntry = new SessionEntry();
sessionEntry.setUuid(uuid);
sessionEntry.setIp(addr);
sessionEntry.setStart(now);
sessionEntry.setLast(now);
sessionService.createSession(sessionEntry);
return uuid;
}
@RequestMapping("/ping")
public ResponseEntity ping(@RequestParam("session") UUID uuid)
{
SessionEntry sessionEntry = sessionService.findSessionByUUID(uuid);
if (sessionEntry == null)
{
return ResponseEntity.notFound().build();
}
sessionService.updateLast(uuid);
return ResponseEntity.ok().build();
}
@DeleteMapping
public ResponseEntity delete(@RequestParam("session") UUID uuid, HttpServletRequest request)
{
SessionEntry sessionEntry = sessionService.findSessionByUUID(uuid);
if (sessionEntry == null)
{
return ResponseEntity.notFound().build();
}
sessionService.deleteSession(sessionEntry);
return ResponseEntity.ok().build();
}
@RequestMapping("/count")
public int count()
{
return sessionService.getCount();
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.service.session;
import java.time.Instant;
import java.util.UUID;
import lombok.Data;
@Data
public class SessionEntry
{
private int id;
private UUID uuid;
private String ip;
private Instant start;
private Instant last;
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.http.service.session;
import java.time.Instant;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.sql2o.Connection;
import org.sql2o.Sql2o;
@Service
public class SessionService
{
private final Sql2o sql2o;
@Autowired
public SessionService(
@Qualifier("Runelite SQL2O") Sql2o sql2o
)
{
this.sql2o = sql2o;
}
public void createSession(SessionEntry session)
{
try (Connection con = sql2o.open())
{
con.createQuery("insert into session (uuid, ip, start, last) "
+ "values (:uuid, :ip, :start, :last)")
.addParameter("uuid", session.getUuid().toString())
.addParameter("ip", session.getIp())
.addParameter("start", session.getStart())
.addParameter("last", session.getLast())
.executeUpdate();
}
}
public SessionEntry findSessionByUUID(UUID id)
{
try (Connection con = sql2o.open())
{
return con.createQuery("select uuid, ip, start, last from session where uuid = :uuid")
.addParameter("uuid", id.toString())
.executeAndFetchFirst(SessionEntry.class);
}
}
public void deleteSession(SessionEntry session)
{
try (Connection con = sql2o.open())
{
con.createQuery("delete from session where uuid = :uuid")
.addParameter("uuid", session.getUuid().toString())
.executeUpdate();
}
}
public void updateLast(UUID session)
{
try (Connection con = sql2o.open())
{
Instant last = Instant.now();
con.createQuery("update session set last = :last where uuid = :uuid")
.addParameter("last", last)
.addParameter("uuid", session.toString())
.executeUpdate();
}
}
private void deleteExpired()
{
try (Connection con = sql2o.open())
{
con.createQuery("delete from session where last + interval 2 minute < current_timestamp()")
.executeUpdate();
}
}
public int getCount()
{
try (Connection con = sql2o.open())
{
return con.createQuery("select count(*) from session")
.executeScalar(Integer.class);
}
}
@Scheduled(fixedDelay = 60000)
public void expire()
{
deleteExpired();
}
}

View File

@@ -0,0 +1,46 @@
-- MySQL dump 10.16 Distrib 10.2.9-MariaDB, for Linux (x86_64)
--
-- Host: localhost Database: runelite
-- ------------------------------------------------------
-- Server version 10.2.9-MariaDB
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `session`
--
DROP TABLE IF EXISTS `session`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `session` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uuid` varchar(36) NOT NULL,
`ip` varchar(39) NOT NULL,
`start` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`last` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `uuid` (`uuid`),
KEY `last` (`last`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2018-03-02 13:23:39