mirror of
https://github.com/glimpse-app/server.git
synced 2025-04-02 10:52:45 -04:00
use JSONY
This commit is contained in:
parent
cb329752ae
commit
eb3cdaa87f
9 changed files with 50 additions and 80 deletions
|
@ -1,4 +1,4 @@
|
||||||
import std/[strutils, os, json, asyncdispatch, httpclient, with, logging]
|
import std/[strutils, os, json, asyncdispatch, httpclient, logging]
|
||||||
|
|
||||||
import jester
|
import jester
|
||||||
import checksums/sha3
|
import checksums/sha3
|
||||||
|
|
|
@ -14,3 +14,6 @@ template respErr*(s: string): untyped =
|
||||||
template respErr*(e: HttpCode, s: string): untyped =
|
template respErr*(e: HttpCode, s: string): untyped =
|
||||||
error s & reqInfo
|
error s & reqInfo
|
||||||
resp e, s
|
resp e, s
|
||||||
|
|
||||||
|
template resp200*(json: string = ""): untyped =
|
||||||
|
resp Http200, json & "\n", "application/json"
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import std/[strutils, with, logging]
|
import std/[strutils, logging]
|
||||||
import jester
|
import jester
|
||||||
|
import jsony
|
||||||
import norm/model
|
import norm/model
|
||||||
import norm/postgres except error
|
import norm/postgres except error
|
||||||
import checksums/sha3
|
import checksums/sha3
|
||||||
|
@ -13,7 +14,6 @@ proc createAuthenticationRoutes*() =
|
||||||
username - string - required
|
username - string - required
|
||||||
email - string - required
|
email - string - required
|
||||||
password - string - required
|
password - string - required
|
||||||
returns: JSON
|
|
||||||
]#
|
]#
|
||||||
post "/api/v1/newUser":
|
post "/api/v1/newUser":
|
||||||
debug "Endpoint used.\n" & reqInfo
|
debug "Endpoint used.\n" & reqInfo
|
||||||
|
@ -38,18 +38,8 @@ proc createAuthenticationRoutes*() =
|
||||||
var user = newUser(@"username", @"email", @"password")
|
var user = newUser(@"username", @"email", @"password")
|
||||||
db.insert(user)
|
db.insert(user)
|
||||||
|
|
||||||
var userProfile: string
|
|
||||||
with userProfile:
|
|
||||||
add "[{"
|
|
||||||
add("\"username\": \"" & user.username & "\",")
|
|
||||||
add("\"email\": \"" & user.email & "\",")
|
|
||||||
add("\"password\": \"" & user.password & "\",")
|
|
||||||
add("\"token\": \"" & user.token & "\",")
|
|
||||||
add("\"fileCount\": \"" & $user.fileCount & "\"")
|
|
||||||
add "}]"
|
|
||||||
|
|
||||||
info "User created.\n" & reqInfo
|
info "User created.\n" & reqInfo
|
||||||
resp Http200, userProfile & "\n", "application/json"
|
resp200 user.toJson()
|
||||||
|
|
||||||
#[
|
#[
|
||||||
request parameters:
|
request parameters:
|
||||||
|
@ -57,7 +47,6 @@ proc createAuthenticationRoutes*() =
|
||||||
OR
|
OR
|
||||||
username - string - required via header
|
username - string - required via header
|
||||||
password - string - required via header
|
password - string - required via header
|
||||||
returns: JSON
|
|
||||||
]#
|
]#
|
||||||
get "/api/v1/newSession":
|
get "/api/v1/newSession":
|
||||||
debug "Endpoint used.\n" & reqInfo
|
debug "Endpoint used.\n" & reqInfo
|
||||||
|
@ -66,7 +55,7 @@ proc createAuthenticationRoutes*() =
|
||||||
|
|
||||||
if not H"Authorization".isEmptyOrWhitespace():
|
if not H"Authorization".isEmptyOrWhitespace():
|
||||||
if not db.validToken(user, H"Authorization"):
|
if not db.validToken(user, H"Authorization"):
|
||||||
resp Http403, "Invalid token.\n"
|
respErr "Invalid token.\n"
|
||||||
|
|
||||||
db.generateToken(user)
|
db.generateToken(user)
|
||||||
|
|
||||||
|
@ -80,12 +69,6 @@ proc createAuthenticationRoutes*() =
|
||||||
else:
|
else:
|
||||||
respErr "Incorrect username or password.\n" # fails if password is wrong but mentions username to obfuscates if a user exists or not
|
respErr "Incorrect username or password.\n" # fails if password is wrong but mentions username to obfuscates if a user exists or not
|
||||||
|
|
||||||
var userToken: string
|
|
||||||
with userToken:
|
|
||||||
add "[{"
|
|
||||||
add("\"token\": \"" & user.token & "\"")
|
|
||||||
add "}]"
|
|
||||||
|
|
||||||
info "Replaced token.\n" & reqInfo
|
info "Replaced token.\n" & reqInfo
|
||||||
resp Http200, userToken & "\n", "application/json"
|
resp200 user.token.toJson()
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import std/[strutils, os, httpclient, strformat, with, logging]
|
import std/[strutils, os, httpclient, strformat, logging]
|
||||||
import jester
|
import jester
|
||||||
|
import jsony
|
||||||
import norm/model
|
import norm/model
|
||||||
import norm/postgres except error
|
import norm/postgres except error
|
||||||
import ../types/[users, files]
|
import ../types/[users, files]
|
||||||
|
@ -22,47 +23,45 @@ proc createDeletionRoutes*(cfg: Cfg) =
|
||||||
#[
|
#[
|
||||||
request parameters:
|
request parameters:
|
||||||
token - string - required via header
|
token - string - required via header
|
||||||
returns: JSON
|
|
||||||
]#
|
]#
|
||||||
delete "/api/v1/userCompletely":
|
delete "/api/v1/userCompletely":
|
||||||
debug "Endpoint used.\n" & reqInfo
|
debug "Endpoint used.\n" & reqInfo
|
||||||
var user = newUser()
|
var user = newUser()
|
||||||
if not db.validToken(user, H"Authorization"):
|
if not db.validToken(user, H"Authorization"):
|
||||||
resp Http403, "Invalid token.\n"
|
respErr "Invalid token.\n"
|
||||||
|
|
||||||
|
|
||||||
discard waitFor purgeUserFiles(H"Authorization")
|
discard waitFor purgeUserFiles(H"Authorization")
|
||||||
db.delete(user)
|
db.delete(user)
|
||||||
|
|
||||||
info "User deactivated.\n" & reqInfo
|
info "User deactivated.\n" & reqInfo
|
||||||
resp Http200, "[]\n", "application/json"
|
resp200
|
||||||
|
|
||||||
#[
|
#[
|
||||||
request parameters:
|
request parameters:
|
||||||
token - string - required via header
|
token - string - required via header
|
||||||
returns: JSON
|
|
||||||
]#
|
]#
|
||||||
delete "/api/v1/user":
|
delete "/api/v1/user":
|
||||||
debug "Endpoint used.\n" & reqInfo
|
debug "Endpoint used.\n" & reqInfo
|
||||||
var user = newUser()
|
var user = newUser()
|
||||||
if not db.validToken(user, H"Authorization"):
|
if not db.validToken(user, H"Authorization"):
|
||||||
resp Http403, "Invalid token.\n"
|
respErr "Invalid token.\n"
|
||||||
|
|
||||||
db.delete(user)
|
db.delete(user)
|
||||||
|
|
||||||
info "User account deleted.\n" & reqInfo
|
info "User account deleted.\n" & reqInfo
|
||||||
resp Http200, "[]\n", "application/json"
|
resp200
|
||||||
|
|
||||||
#[
|
#[
|
||||||
request parameters:
|
request parameters:
|
||||||
token - string - required via header
|
token - string - required via header
|
||||||
name - string - required via header
|
name - string - required via header
|
||||||
returns: JSON
|
|
||||||
]#
|
]#
|
||||||
delete "/api/v1/file":
|
delete "/api/v1/file":
|
||||||
debug "Endpoint used.\n" & reqInfo
|
debug "Endpoint used.\n" & reqInfo
|
||||||
var user = newUser()
|
var user = newUser()
|
||||||
if not db.validToken(user, H"Authorization"):
|
if not db.validToken(user, H"Authorization"):
|
||||||
resp Http403, "Invalid token.\n"
|
respErr "Invalid token.\n"
|
||||||
|
|
||||||
var file = newFile()
|
var file = newFile()
|
||||||
try:
|
try:
|
||||||
|
@ -70,31 +69,25 @@ proc createDeletionRoutes*(cfg: Cfg) =
|
||||||
except NotFoundError:
|
except NotFoundError:
|
||||||
respErr Http404, "File does not exist.\n"
|
respErr Http404, "File does not exist.\n"
|
||||||
|
|
||||||
|
var fileInfo = getFileInfo(file)
|
||||||
|
|
||||||
removeFile(file.path)
|
removeFile(file.path)
|
||||||
db.delete(file)
|
db.delete(file)
|
||||||
dec user.fileCount
|
dec user.fileCount
|
||||||
db.update(user)
|
db.update(user)
|
||||||
|
|
||||||
var userFileCount: string
|
|
||||||
with userFileCount:
|
|
||||||
add "[{"
|
|
||||||
add("\"fileCount\": \"" & $user.fileCount & "\"")
|
|
||||||
add "}]"
|
|
||||||
|
|
||||||
info "Deleted file.\n" & reqInfo
|
info "Deleted file.\n" & reqInfo
|
||||||
resp Http200, userFileCount & "\n", "application/json"
|
resp200 fileInfo.toJson()
|
||||||
|
|
||||||
#[
|
#[
|
||||||
request parameters:
|
request parameters:
|
||||||
token - string - required via header
|
token - string - required via header
|
||||||
returns
|
|
||||||
200 - deleted all of the user's file from db and filesystem only
|
|
||||||
]#
|
]#
|
||||||
delete "/api/v1/files":
|
delete "/api/v1/files":
|
||||||
debug "Endpoint used.\n" & reqInfo
|
debug "Endpoint used.\n" & reqInfo
|
||||||
var user = newUser()
|
var user = newUser()
|
||||||
if not db.validToken(user, H"Authorization"):
|
if not db.validToken(user, H"Authorization"):
|
||||||
resp Http403, "Invalid token.\n"
|
respErr "Invalid token.\n"
|
||||||
|
|
||||||
var listOfFiles = @[newFile()]
|
var listOfFiles = @[newFile()]
|
||||||
try:
|
try:
|
||||||
|
@ -110,4 +103,4 @@ proc createDeletionRoutes*(cfg: Cfg) =
|
||||||
removeDir(cfg.uploadDir & user.username & "/")
|
removeDir(cfg.uploadDir & user.username & "/")
|
||||||
|
|
||||||
info "Deleting user's files.\n" & reqInfo
|
info "Deleting user's files.\n" & reqInfo
|
||||||
resp Http200, "[]\n", "application/json"
|
resp200
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import std/[strutils, logging]
|
import std/[strutils, logging]
|
||||||
import jester
|
import jester
|
||||||
|
import jsony
|
||||||
import norm/postgres except error
|
import norm/postgres except error
|
||||||
import ../types/[users, files]
|
import ../types/[users, files]
|
||||||
import ../[database, helpers]
|
import ../[database, helpers]
|
||||||
|
@ -17,7 +18,7 @@ proc createDownloadRoutes*() =
|
||||||
|
|
||||||
var user = newUser()
|
var user = newUser()
|
||||||
if not db.validToken(user, H"Authorization"):
|
if not db.validToken(user, H"Authorization"):
|
||||||
resp Http403, "Invalid token.\n"
|
respErr "Invalid token.\n"
|
||||||
|
|
||||||
var file = newFile()
|
var file = newFile()
|
||||||
try:
|
try:
|
||||||
|
@ -31,26 +32,19 @@ proc createDownloadRoutes*() =
|
||||||
#[
|
#[
|
||||||
request parameters:
|
request parameters:
|
||||||
token - string - required via header
|
token - string - required via header
|
||||||
returns: JSON
|
|
||||||
]#
|
]#
|
||||||
get "/api/v1/listOfAllFiles":
|
get "/api/v1/listOfAllFiles":
|
||||||
debug "Endpoint used.\n" & reqInfo
|
debug "Endpoint used.\n" & reqInfo
|
||||||
var user = newUser()
|
var user = newUser()
|
||||||
if not db.validToken(user, H"Authorization"):
|
if not db.validToken(user, H"Authorization"):
|
||||||
resp Http403, "Invalid token.\n"
|
respErr "Invalid token.\n"
|
||||||
|
|
||||||
var listOfFiles = @[newFile()]
|
var listOfFiles = @[newFile()]
|
||||||
try:
|
try:
|
||||||
db.select(listOfFiles, """"File".owner = $1""", user.id)
|
db.select(listOfFiles, """"File".owner = $1""", user.id)
|
||||||
except NotFoundError:
|
except NotFoundError:
|
||||||
respErr Http404, "Files does not exist.\n"
|
respErr Http404, "No file exists.\n"
|
||||||
|
|
||||||
var allFiles: string
|
|
||||||
|
|
||||||
for file in listOfFiles:
|
|
||||||
allFiles = allFiles & "{" & "\"name\": \"" & file.name &
|
|
||||||
"\", \"tags\": " & file.tags & "},"
|
|
||||||
allFiles = "[" & allFiles[0..^2] & "]" # trim last comma
|
|
||||||
|
|
||||||
info "List user's file.\n" & reqInfo
|
info "List user's file.\n" & reqInfo
|
||||||
resp Http200, allFiles & "\n", "application/json"
|
resp200 listOfFiles.toJson() # TODO: create new type without unneeded members, read data to it then resp as json. if all else fails -> edit File type to not include owner json in it? Maybe make user.password private?
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import std/[strutils, os, with, logging]
|
import std/[strutils, os, logging]
|
||||||
import jester
|
import jester
|
||||||
|
import jsony
|
||||||
import norm/postgres except error
|
import norm/postgres except error
|
||||||
import ../types/[users, files]
|
import ../types/[users, files]
|
||||||
import ../[database, helpers]
|
import ../[database, helpers]
|
||||||
|
@ -17,7 +18,7 @@ proc createUpdateRoutes*() =
|
||||||
debug "Endpoint used.\n" & reqInfo
|
debug "Endpoint used.\n" & reqInfo
|
||||||
var user = newUser()
|
var user = newUser()
|
||||||
if not db.validToken(user, H"Authorization"):
|
if not db.validToken(user, H"Authorization"):
|
||||||
resp Http403, "Invalid token.\n"
|
respErr "Invalid token.\n"
|
||||||
|
|
||||||
let
|
let
|
||||||
oldName = H"Old name"
|
oldName = H"Old name"
|
||||||
|
@ -44,12 +45,5 @@ proc createUpdateRoutes*() =
|
||||||
file.name = newName
|
file.name = newName
|
||||||
db.update(file)
|
db.update(file)
|
||||||
|
|
||||||
var fileInfo: string
|
|
||||||
with fileInfo:
|
|
||||||
add "[{"
|
|
||||||
add("\"name\": \"" & file.name & "\",")
|
|
||||||
add("\"tags\": \"" & file.tags & "\"")
|
|
||||||
add "}]"
|
|
||||||
|
|
||||||
info "File renamed.\n" & reqInfo
|
info "File renamed.\n" & reqInfo
|
||||||
resp Http200, fileInfo & "\n", "application/json"
|
resp200 getFileInfo(file).toJson()
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import std/[strutils, os, json, with, logging]
|
import std/[strutils, os, json, logging]
|
||||||
import jester
|
import jester
|
||||||
|
import jsony
|
||||||
import norm/model
|
import norm/model
|
||||||
import norm/postgres except error
|
import norm/postgres except error
|
||||||
import ../types/[users, files]
|
import ../types/[users, files]
|
||||||
|
@ -13,14 +14,13 @@ proc createUploadRoutes*(cfg: Cfg) =
|
||||||
file - string/binary - required
|
file - string/binary - required
|
||||||
token - string - required via header
|
token - string - required via header
|
||||||
tags - JSON - optinal
|
tags - JSON - optinal
|
||||||
returns: JSON
|
|
||||||
]#
|
]#
|
||||||
post "/api/v1/newFile":
|
post "/api/v1/newFile":
|
||||||
debug "Endpoint used.\n" & reqInfo
|
debug "Endpoint used.\n" & reqInfo
|
||||||
# fills the new `user` var with saved user data from database
|
# fills the new `user` var with saved user data from database
|
||||||
var user = newUser()
|
var user = newUser()
|
||||||
if not db.validToken(user, H"Authorization"):
|
if not db.validToken(user, H"Authorization"):
|
||||||
resp Http403, "Invalid token.\n"
|
respErr "Invalid token.\n"
|
||||||
|
|
||||||
# pull request form data arguments
|
# pull request form data arguments
|
||||||
let fileData = request.formData["file"].body
|
let fileData = request.formData["file"].body
|
||||||
|
@ -55,11 +55,6 @@ proc createUploadRoutes*(cfg: Cfg) =
|
||||||
|
|
||||||
# write the file from memory
|
# write the file from memory
|
||||||
writeFile(filePath, fileData)
|
writeFile(filePath, fileData)
|
||||||
var userFileCount: string
|
|
||||||
with userFileCount:
|
|
||||||
add "[{"
|
|
||||||
add("\"fileCount\": \"" & $user.fileCount & "\"")
|
|
||||||
add "}]"
|
|
||||||
|
|
||||||
info "File uploaded.\n" & reqInfo
|
info "File uploaded.\n" & reqInfo
|
||||||
resp Http200, userFileCount & "\n", "application/json"
|
resp200 getFileInfo(file).toJson()
|
||||||
|
|
|
@ -9,9 +9,17 @@ type File* = ref object of Model
|
||||||
name*: string
|
name*: string
|
||||||
tags*: string #? This is a temporary hack should be `seq[string]` or `JsonNode` instead
|
tags*: string #? This is a temporary hack should be `seq[string]` or `JsonNode` instead
|
||||||
|
|
||||||
|
type FileInfo* = ref object
|
||||||
|
name*: string
|
||||||
|
tags*: string
|
||||||
|
|
||||||
# creates a new file object and sets default values, recommended by the norm documentation
|
# creates a new file object and sets default values, recommended by the norm documentation
|
||||||
proc newFile*(user: User = newUser(), path: string = "", name: string = "",
|
proc newFile*(user: User = newUser(), path: string = "", name: string = "",
|
||||||
tags: string = ""): File =
|
tags: string = ""): File =
|
||||||
inc user.fileCount
|
inc user.fileCount
|
||||||
debug "Creating new file.\n"
|
debug "Creating new file.\n"
|
||||||
File(owner: user, path: path, name: name, tags: tags)
|
File(owner: user, path: path, name: name, tags: tags)
|
||||||
|
|
||||||
|
proc getFileInfo*(file: File = newFile()): FileInfo =
|
||||||
|
debug "getting file info.\n"
|
||||||
|
FileInfo(name: file.name, tags: file.tags)
|
||||||
|
|
|
@ -46,7 +46,7 @@ fi; printf "%b\n" "$CLR";
|
||||||
|
|
||||||
ENDPOINT="/api/v1/newSession"
|
ENDPOINT="/api/v1/newSession"
|
||||||
printf "\n%bTest: $ENDPOINT%b\n" "$BLD" "$CLR"
|
printf "\n%bTest: $ENDPOINT%b\n" "$BLD" "$CLR"
|
||||||
MKUSER; TOKEN=$(curl --show-error --fail-with-body --request POST http://"$BINDADDR":"$PORT""/api/v1/newUser" -d "username=$TESTUSER" -d "password=$PASSWORD" -d "email=$TESTUSER@example.xyz" | jq ".[0].token" | tr -d '"')
|
MKUSER; TOKEN=$(curl --show-error --fail-with-body --request POST http://"$BINDADDR":"$PORT""/api/v1/newUser" -d "username=$TESTUSER" -d "password=$PASSWORD" -d "email=$TESTUSER@example.xyz" | jq ".token" | tr -d '"')
|
||||||
|
|
||||||
curl --show-error --fail-with-body --request GET http://"$BINDADDR":"$PORT""$ENDPOINT" \
|
curl --show-error --fail-with-body --request GET http://"$BINDADDR":"$PORT""$ENDPOINT" \
|
||||||
-H "Authorization: $TOKEN"
|
-H "Authorization: $TOKEN"
|
||||||
|
@ -59,7 +59,7 @@ fi; printf "%b\n" "$CLR";
|
||||||
|
|
||||||
ENDPOINT="/api/v1/newFile"
|
ENDPOINT="/api/v1/newFile"
|
||||||
printf "\n%bTest: $ENDPOINT%b\n" "$BLD" "$CLR"
|
printf "\n%bTest: $ENDPOINT%b\n" "$BLD" "$CLR"
|
||||||
MKUSER; TOKEN=$(curl --show-error --fail-with-body --request POST http://"$BINDADDR":"$PORT""/api/v1/newUser" -d "username=$TESTUSER" -d "password=$PASSWORD" -d "email=$TESTUSER@example.xyz" | jq ".[0].token" | tr -d '"')
|
MKUSER; TOKEN=$(curl --show-error --fail-with-body --request POST http://"$BINDADDR":"$PORT""/api/v1/newUser" -d "username=$TESTUSER" -d "password=$PASSWORD" -d "email=$TESTUSER@example.xyz" | jq ".token" | tr -d '"')
|
||||||
|
|
||||||
curl --show-error --fail-with-body --request POST http://"$BINDADDR":"$PORT""$ENDPOINT" \
|
curl --show-error --fail-with-body --request POST http://"$BINDADDR":"$PORT""$ENDPOINT" \
|
||||||
-H "Authorization: $TOKEN" -F "file=@image.png"
|
-H "Authorization: $TOKEN" -F "file=@image.png"
|
||||||
|
@ -155,7 +155,7 @@ fi; printf "%b\n" "$CLR";
|
||||||
|
|
||||||
ENDPOINT="/api/v1/userCompletely"
|
ENDPOINT="/api/v1/userCompletely"
|
||||||
printf "\n%bTest: $ENDPOINT%b\n" "$BLD" "$CLR"
|
printf "\n%bTest: $ENDPOINT%b\n" "$BLD" "$CLR"
|
||||||
MKUSER; TOKEN=$(curl --show-error --fail-with-body --request POST http://"$BINDADDR":"$PORT""/api/v1/newUser" -d "username=$TESTUSER" -d "password=$PASSWORD" -d "email=$TESTUSER@example.xyz" | jq ".[0].token" | tr -d '"')
|
MKUSER; TOKEN=$(curl --show-error --fail-with-body --request POST http://"$BINDADDR":"$PORT""/api/v1/newUser" -d "username=$TESTUSER" -d "password=$PASSWORD" -d "email=$TESTUSER@example.xyz" | jq ".token" | tr -d '"')
|
||||||
|
|
||||||
curl --show-error --fail-with-body --request DELETE http://"$BINDADDR":"$PORT""$ENDPOINT" \
|
curl --show-error --fail-with-body --request DELETE http://"$BINDADDR":"$PORT""$ENDPOINT" \
|
||||||
-H "Authorization: $TOKEN"
|
-H "Authorization: $TOKEN"
|
||||||
|
|
Loading…
Add table
Reference in a new issue