Skip to content

Commit aa3f666

Browse files
committed
Merge branch 'feature/swagger-api' into develop
2 parents 200d088 + 1b87317 commit aa3f666

11 files changed

Lines changed: 134 additions & 24 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212
out
1313
Hyperspace.iml
1414

15+
.nrepl-port
16+
1517
hyperspace-server.*.db

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ Use `lein run client` to run the standalone game client.
1111

1212
## Game server
1313

14+
### Configuring
15+
All configuration data is stored in the `config.edn` file.
16+
1417
### Running
15-
Use `lein run server [port]` to run game server.
18+
Use `lein ring server` to run game server.
1619

1720
### Protocol
1821
Hyperspace protocol is very simple. All messages are transferred in the JSON format.

config.edn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{:database {:url "jdbc:h2:hyperspace-server;DB_CLOSE_DELAY=-1"
2+
:user "sa"
3+
:password ""}}

project.clj

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
(defproject hyperspace "1.0.0-SNAPSHOT"
22
:description "Simple 2D game written in Clojure."
3-
:dependencies [[com.googlecode.flyway/flyway-core "2.3"]
3+
:dependencies [[azql "0.2.0"]
4+
[clj-liquibase "0.5.2"]
45
[com.h2database/h2 "1.3.173"]
6+
[crypto-password "0.1.3"]
7+
[crypto-random "1.2.0"]
58
[log4j/log4j "1.2.17"]
9+
[metosin/ring-swagger "0.13.0"]
10+
[metosin/compojure-api "0.16.0"]
11+
[metosin/ring-http-response "0.5.0"]
12+
[metosin/ring-swagger-ui "2.0.17"]
613
[midje "1.5.0"]
7-
[org.clojure/clojure "1.5.1"]
14+
[org.clojure/clojure "1.6.0"]
815
[org.clojure/clojure-contrib "1.2.0"]
916
[org.clojure/data.json "0.2.2"]
1017
[org.clojure/tools.logging "0.2.3"]
1118
[org.clojars.rexim/jinput-platform-natives "2.0.5"]
1219
[org.ru.codingteam/lwjgl-platform-natives "2.9.1"]
1320
[org.lwjgl.lwjgl/lwjgl "2.9.1"]]
21+
:ring {:handler hyperspace.swagger.server/app}
22+
:uberjar-name "hyperspace-server.jar"
23+
:profiles {:uberjar {:resource-paths ["swagger-ui"]
24+
:aot :all}
25+
:dev {:dependencies [[javax.servlet/servlet-api "2.5"]]
26+
:plugins [[lein-ring "0.8.11"]]}}
1427
:main hyperspace.main)

src/hyperspace/server/config.clj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(ns hyperspace.server.config
2+
(:import (java.io PushbackReader))
3+
(:require [clojure.java.io :as io]))
4+
5+
(defn -from-edn
6+
[fname]
7+
(with-open [rdr (-> (io/reader fname)
8+
PushbackReader.)]
9+
(clojure.edn/read rdr)))
10+
11+
(def config (-from-edn "config.edn"))

src/hyperspace/server/database.clj

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(ns hyperspace.server.database.datasource
2+
(:require [azql.emit :as emit]
3+
[azql.dialect :as dialect]
4+
[clj-liquibase.cli :as cli]
5+
[hyperspace.server.database.migrations :as migrations])
6+
(:use [hyperspace.server.config :only [config]])
7+
(:import [org.h2.jdbcx JdbcConnectionPool]))
8+
9+
(def datasource
10+
(let [{url :url
11+
user :user
12+
password :password} (:database config)
13+
ds (JdbcConnectionPool/create url user password)]
14+
(cli/update {:datasource ds
15+
:changelog migrations/changelog})
16+
ds))
17+
18+
(dialect/register-dialect ::h2)
19+
(defmethod dialect/guess-dialect :h2 [_] ::h2)
20+
(defmethod emit/quote-name ::h2 [name] (str name))
21+
22+
(def db-spec {:datasource datasource})
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(ns hyperspace.server.database.migrations
2+
(:require [clj-liquibase.change :as ch])
3+
(:use [clj-liquibase.core :only (defchangelog)]))
4+
5+
(def add-users-table ["id=1"
6+
"author=ForNeVeR"
7+
[(ch/create-table :users
8+
[[:id :int :null false :pk true :autoinc true]
9+
[:login [:varchar 128] :unique true :null false]
10+
[:password [:varchar 128] :null false]
11+
[:session [:varchar 16] :null true :unique true]])
12+
(ch/create-index :users [:login])]])
13+
14+
(defchangelog changelog "hyperspace-server" [add-users-table])
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
(ns hyperspace.server.database.user
2+
(:import (java.util UUID))
3+
(:require [crypto.password.scrypt :as password]
4+
[crypto.random :as random])
5+
(:use [azql.core]
6+
[hyperspace.server.database.datasource :only [db-spec]]))
7+
8+
(defn create [{login :login password :password}]
9+
(insert! db-spec :users
10+
(values [{:login login
11+
:password (password/encrypt password)}])))
12+
13+
(defn- generate-session []
14+
;; 8 random bytes = 16 hex digits should be enough for duplicates to be almost impossible with probability of about
15+
;; 10^-12
16+
(random/hex 8))
17+
18+
(defn login [{login :login password :password}]
19+
(with-connection [c db-spec]
20+
(transaction c
21+
(let [user (fetch-one c
22+
(select (fields [:login :password])
23+
(from :u :users)
24+
(where (= :u.login login))))
25+
user-id (:id user)]
26+
(if (password/check password (:password user))
27+
(let [session (generate-session)]
28+
(update! c :users
29+
(setf :session session)
30+
(where (= :id user-id)))
31+
session)
32+
nil)))))

src/hyperspace/server/main.clj

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

0 commit comments

Comments
 (0)