fixed cors

This commit is contained in:
Alexander Popov 2023-08-06 00:44:27 +03:00
parent fdcba863c2
commit 7d4bb47a53
Signed by: iiiypuk
GPG Key ID: E47FE0AB36CD5ED6
5 changed files with 29 additions and 14 deletions

View File

@ -3,9 +3,10 @@ import { routes } from './routes.js';
let DEBUG = true;
window.DEBUG = DEBUG;
let SERVER_URL;
let SERVER_HOST;
if (DEBUG) SERVER_HOST = 'localhost';
else SERVER_HOST = 'a2s.su';
window.SERVER_HOST = SERVER_HOST;
/* main */
window.onload = function () {
@ -75,12 +76,18 @@ const location_handler = async () => {
};
function get_from_api(callback, api_method = '', params = {}) {
respone = '';
if (api_method == '') {
console.log('wrong method');
} else {
fetch(`http://${SERVER_HOST}:3000/api/v1.0/${api_method}`)
let url = `http://${SERVER_HOST}:3000/api/v1.0/${api_method}`;
let opts = {
method: 'get',
headers: {
'Content-Type': 'application/json',
},
};
fetch(url, opts)
.then((response) => {
return response.json();
})

View File

@ -5,6 +5,7 @@
"description": "ololo",
"scripts": {
"serve": "npx parcel serve --dist-dir public app/index.html",
"serve-https": "npx parcel serve --https --dist-dir public app/index.html",
"build": "npm run clean; npx parcel build --dist-dir public app/index.html --no-optimize; npm run git-hash",
"prettier": "npx prettier --write .",
"git-hash": "./update_commit.sh",

View File

@ -1,15 +1,15 @@
name: api_server
name: a2s_server
version: 0.1.0
authors:
- Alexander Popov <iiiypuk@fastmail.fm>
# description: |
# Short description of server
description: |
API server for a2s community
targets:
api:
main: src/api.cr
a2s_server:
main: src/server.cr
dependencies:
kemal:
@ -17,8 +17,4 @@ dependencies:
lexbor:
github: kostya/lexbor
# development_dependencies:
# webmock:
# github: manastech/webmock.cr
license: WTFPL

View File

@ -9,6 +9,13 @@ require "lexbor"
# ```
# ...
# ```
options "/api/v1.0/rev2_monsters" do |env|
# Allow `GET /api/v1.0/rev2_monsters`...
env.response.headers.add("Access-Control-Allow-Methods", "GET")
env.response.headers.add("Access-Control-Allow-Headers", "Content-type")
env.response.headers.add("Access-Control-Allow-Origin", "*")
end
get "/api/v1.0/rev2_monsters" do |env|
url = "https://game.capcom.com/residentevil/en/onlineevent-4_1108.html"
@ -48,7 +55,10 @@ get "/api/v1.0/rev2_monsters" do |env|
response = File.read("./cache/rev2/data.json")
end
env.response.content_type = "application/json"
env.response.headers.add("Access-Control-Allow-Methods", "GET")
env.response.headers.add("Access-Control-Allow-Headers", "Content-type")
env.response.headers.add("Access-Control-Allow-Origin", "*")
env.response.content_type = "application/json"
response
end

View File

@ -8,4 +8,5 @@ get "/" do
end
Kemal.config.env = "development"
Kemal.config.port = 3000
Kemal.run