refactoring of client engine

This commit is contained in:
Settel 2021-08-05 02:22:28 +02:00
parent 34da0f5f34
commit 97b11d31d5
10 changed files with 43 additions and 6 deletions

View File

@ -44,3 +44,4 @@ run-standalone:
clean:
rm -rf client/dist/
rm -rf client/.nuxt/

View File

@ -16,6 +16,9 @@ export default {
modules: ['@nuxtjs/axios'],
plugins: [{ src: '~/plugins/engine.js', mode: 'client' }],
axios: { proxy: true },
publicRuntimeConfig: {
serverBaseUrl: 'http://localhost:3000/',
},
proxy: {
'/api/': 'http://localhost:32039',
},

View File

@ -10,8 +10,10 @@
},
"dependencies": {
"@nuxtjs/axios": "^5.13.6",
"build-url": "^6.0.1",
"core-js": "^3.15.1",
"nuxt": "^2.15.7"
"nuxt": "^2.15.7",
"url": "^0.11.0"
},
"devDependencies": {
"@babel/eslint-parser": "^7.14.7",

View File

@ -43,6 +43,7 @@ export default {
async fetch() {
try {
this.user = await this.$axios.$get('/api/userinfo')
this.$store.commit('engine/setUser', this.user)
} catch(e) {
// nop
}

View File

@ -1,18 +1,22 @@
<template>
<div>
<pre class="json">{{ user }}</pre>
<pre class="json">{{ json }}</pre>
</div>
</template>
<script>
export default {
mounted() {
this.$engine.start()
async mounted() {
await this.$engine.start()
},
computed: {
json() {
return JSON.stringify(this.$store.state.engine.json, null, 2)
},
user() {
return JSON.stringify(this.$store.state.engine.user, null, 2)
},
}
}
</script>

View File

@ -1,15 +1,29 @@
import buildUrl from 'build-url'
export default (context, inject) => {
const { store, $axios } = context
const { store, $axios, $config } = context
const engine = {
lastFetched: [0, 0, 0, 0, 0],
start() {
async start() {
if (!store.state.engine.user) {
window.location.href = "/"
}
this.fetchUpdate()
},
async fetchUpdate() {
let delay = 0
try {
const response = await $axios.get('http://localhost:3000/api/sync?v=' + (store.state.engine.version + 1))
const url = buildUrl($config.serverBaseUrl, {
path: '/api/sync',
queryParams: {
v: store.state.engine.version + 1,
g: store.state.engine.user?.game,
},
})
console.log(`url: ${url}`)
const response = await $axios.get(url)
const json = response.data
store.commit('engine/setJson', json)
} catch (e) {

View File

@ -1,6 +1,7 @@
export const state = () => ({
json: {},
version: -1,
user: undefined,
})
export const mutations = {
@ -11,4 +12,7 @@ export const mutations = {
setVersion(state, version) {
state.version = version
},
setUser(state, user) {
state.user = user
}
}

View File

@ -2108,6 +2108,11 @@ buffer@^5.1.0:
base64-js "^1.3.1"
ieee754 "^1.1.13"
build-url@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/build-url/-/build-url-6.0.1.tgz#b16505136248e24b1b6d7ccab99c7ee73cd962a7"
integrity sha512-FEmyP+3hCVoBBxGZEfC8WCTvqASZb+QOvTznddKCBgdxZFqTww1+7vtZP0TUIL0/8sup0m0QpKaOaj/V4Mol0w==
builtin-status-codes@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/builtin-status-codes/-/builtin-status-codes-3.0.0.tgz#85982878e21b98e1c66425e03d0174788f569ee8"

View File

@ -9,6 +9,7 @@ import (
type userLight struct {
Name string `json:"name"`
Role string `json:"role"`
Game string `json:"game"`
}
func (authMux *AuthMux) GetUserInfo(w http.ResponseWriter, r *http.Request) {
@ -21,6 +22,7 @@ func (authMux *AuthMux) GetUserInfo(w http.ResponseWriter, r *http.Request) {
usrLight := userLight{
Name: usr.Name,
Role: usr.Role,
Game: usr.Game,
}
w.Header().Add("Content-Type", "application/json")

View File

@ -10,4 +10,5 @@ type User struct {
id string
Name string `json:"name"`
Role string `json:"role"`
Game string `json:"game"`
}