Fix server hang issue

This commit is contained in:
John Lyon-Smith
2018-04-13 09:34:34 -07:00
parent 9cfb173d1b
commit bdc80c5b01
9 changed files with 86 additions and 49 deletions

2
.gitignore vendored
View File

@@ -2,8 +2,6 @@ node_modules/
coverage/
dist/
scratch/
mobile/android/.idea/
mobile/ios/**/xcuserdata/
.DS_Store
npm-debug.log*
yarn-debug.log*

3
mobile/.gitignore vendored
View File

@@ -1,6 +1,9 @@
local.js
ios/build/
ios/Pods/
ios/DerivedData/
ios/**/xcuserdata/
android/.idea/
android/**/build/**
android/**/.gradle/**
**/.idea/**

View File

@@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>20180410.0</string>
<string>20180413.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>

View File

@@ -1,15 +1,11 @@
import EventEmitter from "eventemitter3"
import io from "socket.io-client"
import { AsyncStorage } from "react-native"
import autobind from "autobind-decorator"
import { local } from "./local"
const authTokenKeyName = "AuthToken"
const backendKeyName = "BackendName"
// if (__DEV__) {
// const localIPAddr = process.env.LOCAL_IP_ADDR
// baseURL = `http://${localIPAddr || "localhost"}:3001`
// apiPath = ""
// }
const backendKeyName = "Backend"
class NetworkError extends Error {
constructor(message) {
@@ -36,50 +32,71 @@ class APIError extends Error {
}
}
@autobind
class API extends EventEmitter {
static apiPath = "/api"
constructor() {
super()
this.user = { pending: true }
// AsyncStorage.getItem(backendKeyName)
// .then((backend) => {
// this.backend = backend
// })
// .catch((err) => {
// this.backend = "normal"
// AsyncStorage.setItem(backendKeyName, this.backend)
// })
const baseURLs = {
normal: "https://dar.kss.us.com",
test: "https://dar-test.kss.us.com",
development: `http://${local.ipAddr}:3001`,
}
this.baseURL = null
this.backendName = "normal"
this.baseURL = "https://dar.kss.us.com"
this.apiPath = "/api"
const checkForToken = () => {
console.log(`${this.backendName} - ${this.baseURL}`)
AsyncStorage.getItem(authTokenKeyName)
.then((token) => {
if (!token) {
return Promise.reject()
}
AsyncStorage.getItem(authTokenKeyName)
.then((token) => {
if (!token) {
this.token = token
return this.who()
})
.then((user) => {
console.log("token good")
this.user = user
this.connectSocket()
this.emit("login")
})
.catch(() => {
console.log("token not present")
AsyncStorage.removeItem(authTokenKeyName)
this.token = null
this.user = {}
this.socket = null
this.emit("logout")
})
}
AsyncStorage.getItem(backendKeyName)
.then((backendName) => {
this.backendName = backendName
this.baseURL = baseURLs[backendName]
if (!this.baseURL) {
return Promise.reject()
}
this.token = token
return this.who()
console.log("setting backend")
checkForToken()
})
.then((user) => {
this.user = user
this.connectSocket()
this.emit("login")
})
.catch((err) => {
AsyncStorage.removeItem(authTokenKeyName)
this.token = null
this.user = {}
this.socket = null
this.emit("logout")
.catch(() => {
this.backendName = "normal"
this.baseURL = baseURLS[this.backendName]
AsyncStorage.setItem(backendKeyName, this.backendName)
console.log("setting default backend")
checkForToken()
})
}
connectSocket() {
this.socket = io(this.baseURL, {
path: this.apiPath + "/socketio",
path: API.apiPath + "/socketio",
query: {
auth_token: this.token,
},
@@ -117,11 +134,21 @@ class API extends EventEmitter {
return this.backendName
}
set backend(value) {
if (this.backendName !== value) {
this.backendName = value
AsyncStorage.setItem(backendKeyName, this.backendName).then(
this.logout,
this.logout
)
}
}
makeImageUrl(id, size) {
if (id) {
return this.apiPath + "/assets/" + id + "?access_token=" + this.token
return API.apiPath + "/assets/" + id + "?access_token=" + this.token
} else if (size && size.width && size.height) {
return `${this.apiPath}/placeholders/${size.width}x${
return `${API.apiPath}/placeholders/${size.width}x${
size.height
}?access_token=${this.token}`
} else {
@@ -131,7 +158,7 @@ class API extends EventEmitter {
makeAssetUrl(id) {
return id
? this.apiPath + "/assets/" + id + "?access_token=" + this.token
? API.apiPath + "/assets/" + id + "?access_token=" + this.token
: null
}
@@ -168,7 +195,16 @@ class API extends EventEmitter {
}
}
fetchOptions.headers = headers
fetch(this.baseURL + this.apiPath + path, fetchOptions)
if (!this.baseURL) {
tconsole.error("no baseURL!")
return reject(new Error("API layer not ready"))
}
console.log(
`${fetchOptions.method} - ${this.baseURL + API.apiPath + path}`
)
fetch(this.baseURL + API.apiPath + path, fetchOptions)
.then((res) => {
return Promise.all([
Promise.resolve(res),

View File

@@ -1,6 +1,6 @@
export const versionInfo = {
version: '1.0.0',
fullVersion: '1.0.0-20180410.0',
fullVersion: '1.0.0-20180413.0',
title: 'Deighton AR System',
copyright: '© 2018, Kingston Software Solutions.',
supportEmail: 'support@kss.us.com',

View File

@@ -79,7 +79,7 @@ export class AuthRoutes {
let User = this.db.User
if (!email || !password) {
createError.BadRequest("Must supply user name and password")
throw createError.BadRequest("Must supply user name and password")
}
// Lookup the user

View File

@@ -7,9 +7,9 @@ export { TeamRoutes } from "./TeamRoutes"
import createError from "http-errors"
export function catchAll(routeHandler) {
return (req, res, next) => {
return async (req, res, next) => {
try {
routeHandler(req, res, next)
await routeHandler(req, res, next)
} catch (err) {
if (err instanceof createError.HttpError) {
next(err)

View File

@@ -15,7 +15,7 @@
major: 1,
minor: 0,
patch: 0,
build: 20180410,
build: 20180413,
revision: 0,
sequence: 1,
tz: "America/Los_Angeles",

View File

@@ -1,6 +1,6 @@
export const versionInfo = {
version: '1.0.0',
fullVersion: '1.0.0-20180410.0',
fullVersion: '1.0.0-20180413.0',
title: 'Deighton AR System',
copyright: '© 2018, Kingston Software Solutions.',
supportEmail: 'support@kss.us.com',