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: clean:
rm -rf client/dist/ rm -rf client/dist/
rm -rf client/.nuxt/

View File

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

View File

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

View File

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

View File

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

View File

@ -1,15 +1,29 @@
import buildUrl from 'build-url'
export default (context, inject) => { export default (context, inject) => {
const { store, $axios } = context const { store, $axios, $config } = context
const engine = { const engine = {
lastFetched: [0, 0, 0, 0, 0], lastFetched: [0, 0, 0, 0, 0],
start() { async start() {
if (!store.state.engine.user) {
window.location.href = "/"
}
this.fetchUpdate() this.fetchUpdate()
}, },
async fetchUpdate() { async fetchUpdate() {
let delay = 0 let delay = 0
try { 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 const json = response.data
store.commit('engine/setJson', json) store.commit('engine/setJson', json)
} catch (e) { } catch (e) {

View File

@ -1,6 +1,7 @@
export const state = () => ({ export const state = () => ({
json: {}, json: {},
version: -1, version: -1,
user: undefined,
}) })
export const mutations = { export const mutations = {
@ -11,4 +12,7 @@ export const mutations = {
setVersion(state, version) { setVersion(state, version) {
state.version = 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" base64-js "^1.3.1"
ieee754 "^1.1.13" 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: builtin-status-codes@^3.0.0:
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/builtin-status-codes/-/builtin-status-codes-3.0.0.tgz#85982878e21b98e1c66425e03d0174788f569ee8" 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 { type userLight struct {
Name string `json:"name"` Name string `json:"name"`
Role string `json:"role"` Role string `json:"role"`
Game string `json:"game"`
} }
func (authMux *AuthMux) GetUserInfo(w http.ResponseWriter, r *http.Request) { 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{ usrLight := userLight{
Name: usr.Name, Name: usr.Name,
Role: usr.Role, Role: usr.Role,
Game: usr.Game,
} }
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")

View File

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