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/ coverage/
dist/ dist/
scratch/ scratch/
mobile/android/.idea/
mobile/ios/**/xcuserdata/
.DS_Store .DS_Store
npm-debug.log* npm-debug.log*
yarn-debug.log* yarn-debug.log*

3
mobile/.gitignore vendored
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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