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; let DEBUG = true;
window.DEBUG = DEBUG; window.DEBUG = DEBUG;
let SERVER_URL; let SERVER_HOST;
if (DEBUG) SERVER_HOST = 'localhost'; if (DEBUG) SERVER_HOST = 'localhost';
else SERVER_HOST = 'a2s.su'; else SERVER_HOST = 'a2s.su';
window.SERVER_HOST = SERVER_HOST;
/* main */ /* main */
window.onload = function () { window.onload = function () {
@ -75,12 +76,18 @@ const location_handler = async () => {
}; };
function get_from_api(callback, api_method = '', params = {}) { function get_from_api(callback, api_method = '', params = {}) {
respone = '';
if (api_method == '') { if (api_method == '') {
console.log('wrong method'); console.log('wrong method');
} else { } 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) => { .then((response) => {
return response.json(); return response.json();
}) })

View File

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

View File

@ -1,15 +1,15 @@
name: api_server name: a2s_server
version: 0.1.0 version: 0.1.0
authors: authors:
- Alexander Popov <iiiypuk@fastmail.fm> - Alexander Popov <iiiypuk@fastmail.fm>
# description: | description: |
# Short description of server API server for a2s community
targets: targets:
api: a2s_server:
main: src/api.cr main: src/server.cr
dependencies: dependencies:
kemal: kemal:
@ -17,8 +17,4 @@ dependencies:
lexbor: lexbor:
github: kostya/lexbor github: kostya/lexbor
# development_dependencies:
# webmock:
# github: manastech/webmock.cr
license: WTFPL 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| get "/api/v1.0/rev2_monsters" do |env|
url = "https://game.capcom.com/residentevil/en/onlineevent-4_1108.html" 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") response = File.read("./cache/rev2/data.json")
end 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.headers.add("Access-Control-Allow-Origin", "*")
env.response.content_type = "application/json"
response response
end end

View File

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