mirror of
https://github.com/glimpse-app/server.git
synced 2025-04-02 10:52:45 -04:00
debugging logs
This commit is contained in:
parent
497e6eea7e
commit
a395d197a5
8 changed files with 26 additions and 13 deletions
|
@ -1,6 +1,10 @@
|
|||
import norm/[model, postgres]
|
||||
import std/logging
|
||||
import norm/model
|
||||
import norm/postgres except error
|
||||
import types/[users, files]
|
||||
import config/config
|
||||
|
||||
info "connecting to database.\n"
|
||||
let db* = open(cfg.dbHost, cfg.dbUser, cfg.dbPassword, cfg.dbDatabase)
|
||||
db.createTables(newFile()) # file objects require a user object, thus a tables for both are created
|
||||
info "connected to database.\n"
|
||||
|
|
|
@ -66,7 +66,7 @@ proc createAuthenticationRoutes*() =
|
|||
|
||||
if not H"Authorization".isEmptyOrWhitespace():
|
||||
if not db.validToken(user, H"Authorization"):
|
||||
respErr "Invalid token.\n"
|
||||
resp Http403, "Invalid token.\n"
|
||||
|
||||
db.generateToken(user)
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ proc createDeletionRoutes*(cfg: Cfg) =
|
|||
debug "Endpoint used.\n" & reqInfo
|
||||
var user = newUser()
|
||||
if not db.validToken(user, H"Authorization"):
|
||||
respErr "Invalid token.\n"
|
||||
resp Http403, "Invalid token.\n"
|
||||
|
||||
discard waitFor purgeUserFiles(H"Authorization")
|
||||
db.delete(user)
|
||||
|
@ -45,7 +45,7 @@ proc createDeletionRoutes*(cfg: Cfg) =
|
|||
debug "Endpoint used.\n" & reqInfo
|
||||
var user = newUser()
|
||||
if not db.validToken(user, H"Authorization"):
|
||||
respErr "Invalid token.\n"
|
||||
resp Http403, "Invalid token.\n"
|
||||
|
||||
db.delete(user)
|
||||
|
||||
|
@ -62,7 +62,7 @@ proc createDeletionRoutes*(cfg: Cfg) =
|
|||
debug "Endpoint used.\n" & reqInfo
|
||||
var user = newUser()
|
||||
if not db.validToken(user, H"Authorization"):
|
||||
respErr "Invalid token.\n"
|
||||
resp Http403, "Invalid token.\n"
|
||||
|
||||
var file = newFile()
|
||||
try:
|
||||
|
@ -94,7 +94,7 @@ proc createDeletionRoutes*(cfg: Cfg) =
|
|||
debug "Endpoint used.\n" & reqInfo
|
||||
var user = newUser()
|
||||
if not db.validToken(user, H"Authorization"):
|
||||
respErr "Invalid token.\n"
|
||||
resp Http403, "Invalid token.\n"
|
||||
|
||||
var listOfFiles = @[newFile()]
|
||||
try:
|
||||
|
|
|
@ -17,7 +17,7 @@ proc createDownloadRoutes*() =
|
|||
|
||||
var user = newUser()
|
||||
if not db.validToken(user, H"Authorization"):
|
||||
respErr "Invalid token.\n"
|
||||
resp Http403, "Invalid token.\n"
|
||||
|
||||
var file = newFile()
|
||||
try:
|
||||
|
@ -37,7 +37,7 @@ proc createDownloadRoutes*() =
|
|||
debug "Endpoint used.\n" & reqInfo
|
||||
var user = newUser()
|
||||
if not db.validToken(user, H"Authorization"):
|
||||
respErr "Invalid token.\n"
|
||||
resp Http403, "Invalid token.\n"
|
||||
|
||||
var listOfFiles = @[newFile()]
|
||||
try:
|
||||
|
|
|
@ -17,7 +17,7 @@ proc createUpdateRoutes*() =
|
|||
debug "Endpoint used.\n" & reqInfo
|
||||
var user = newUser()
|
||||
if not db.validToken(user, H"Authorization"):
|
||||
respErr "Invalid token.\n"
|
||||
resp Http403, "Invalid token.\n"
|
||||
|
||||
let
|
||||
oldName = H"Old name"
|
||||
|
|
|
@ -20,7 +20,7 @@ proc createUploadRoutes*(cfg: Cfg) =
|
|||
# fills the new `user` var with saved user data from database
|
||||
var user = newUser()
|
||||
if not db.validToken(user, H"Authorization"):
|
||||
respErr "Invalid token.\n"
|
||||
resp Http403, "Invalid token.\n"
|
||||
|
||||
# pull request form data arguments
|
||||
let fileData = request.formData["file"].body
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import std/logging
|
||||
import norm/[model, pragmas]
|
||||
import ./users
|
||||
|
||||
|
@ -9,7 +10,8 @@ type File* = ref object of Model
|
|||
tags*: string #? This is a temporary hack should be `seq[string]` or `JsonNode` instead
|
||||
|
||||
# creates a new file object and sets default values, recommended by the norm documentation
|
||||
func newFile*(user: User = newUser(), path: string = "", name: string = "",
|
||||
proc newFile*(user: User = newUser(), path: string = "", name: string = "",
|
||||
tags: string = ""): File =
|
||||
inc user.fileCount
|
||||
debug "Creating new file.\n"
|
||||
File(owner: user, path: path, name: name, tags: tags)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import std/oids
|
||||
import norm/[model, postgres, pragmas]
|
||||
import std/[oids, logging]
|
||||
import norm/[model, pragmas]
|
||||
import norm/postgres except error
|
||||
import checksums/sha3
|
||||
|
||||
# define user object
|
||||
|
@ -12,19 +13,25 @@ type User* = ref object of Model
|
|||
|
||||
# checks if the provided token exists in the database
|
||||
proc validToken*(db: DbConn, user: var User, token: string): bool =
|
||||
debug "Validating token.\n"
|
||||
try:
|
||||
db.select(user, """"token" = $1""", token)
|
||||
debug "Valid token.\n"
|
||||
return true
|
||||
except NotFoundError:
|
||||
error "Invalid token.\n"
|
||||
return false
|
||||
|
||||
# update user's token using a newly generated token
|
||||
proc generateToken*(db: DbConn, user: var User) =
|
||||
debug "Creating new token.\n"
|
||||
user.token = $Sha3_512.secureHash($genoid())
|
||||
db.update(user)
|
||||
debug "Created new token.\n"
|
||||
|
||||
# creates a new user object and sets default values, recommended by the norm documentation
|
||||
proc newUser*(username: string = "", email: string = "",
|
||||
password: string = ""): User =
|
||||
debug "Created new user.\n"
|
||||
User(username: username, email: email, password: $Sha3_512.secureHash(
|
||||
password), token: $Sha3_512.secureHash($genoid()))
|
||||
|
|
Loading…
Add table
Reference in a new issue