mirror of
https://github.com/skot/ESP-Miner.git
synced 2025-04-11 13:30:51 +02:00
fix CORS
This commit is contained in:
parent
1fae8781d4
commit
91a224245b
@ -1,5 +1,5 @@
|
||||
import { HttpErrorResponse } from '@angular/common/http';
|
||||
import { Component } from '@angular/core';
|
||||
import { Component, Input, OnInit } from '@angular/core';
|
||||
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
||||
import { ToastrService } from 'ngx-toastr';
|
||||
import { startWith } from 'rxjs';
|
||||
@ -12,7 +12,7 @@ import { eASICModel } from 'src/models/enum/eASICModel';
|
||||
templateUrl: './edit.component.html',
|
||||
styleUrls: ['./edit.component.scss']
|
||||
})
|
||||
export class EditComponent {
|
||||
export class EditComponent implements OnInit {
|
||||
|
||||
public form!: FormGroup;
|
||||
|
||||
@ -24,6 +24,8 @@ export class EditComponent {
|
||||
public eASICModel = eASICModel;
|
||||
public ASICModel!: eASICModel;
|
||||
|
||||
@Input() uri = '';
|
||||
|
||||
constructor(
|
||||
private fb: FormBuilder,
|
||||
private systemService: SystemService,
|
||||
@ -35,7 +37,11 @@ export class EditComponent {
|
||||
window.addEventListener('resize', this.checkDevTools);
|
||||
this.checkDevTools();
|
||||
|
||||
this.systemService.getInfo()
|
||||
|
||||
|
||||
}
|
||||
ngOnInit(): void {
|
||||
this.systemService.getInfo(this.uri)
|
||||
.pipe(this.loadingService.lockUIUntilComplete())
|
||||
.subscribe(info => {
|
||||
this.ASICModel = info.ASICModel;
|
||||
@ -73,8 +79,9 @@ export class EditComponent {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
private checkDevTools = () => {
|
||||
if (
|
||||
window.outerWidth - window.innerWidth > 160 ||
|
||||
@ -99,7 +106,7 @@ export class EditComponent {
|
||||
form.invertfanpolarity = form.invertfanpolarity == true ? 1 : 0;
|
||||
form.autofanspeed = form.autofanspeed == true ? 1 : 0;
|
||||
|
||||
this.systemService.updateSystem(form)
|
||||
this.systemService.updateSystem(this.uri, form)
|
||||
.pipe(this.loadingService.lockUIUntilComplete())
|
||||
.subscribe({
|
||||
next: () => {
|
||||
|
@ -99,7 +99,7 @@ export class SettingsComponent {
|
||||
form.invertfanpolarity = form.invertfanpolarity == true ? 1 : 0;
|
||||
form.autofanspeed = form.autofanspeed == true ? 1 : 0;
|
||||
|
||||
this.systemService.updateSystem(form)
|
||||
this.systemService.updateSystem(undefined, form)
|
||||
.pipe(this.loadingService.lockUIUntilComplete())
|
||||
.subscribe({
|
||||
next: () => {
|
||||
|
@ -16,6 +16,7 @@
|
||||
<th>Power</th>
|
||||
<th>Temp</th>
|
||||
<th>Best Difficulty</th>
|
||||
<th>Version</th>
|
||||
<th>Edit</th>
|
||||
<th>Restart</th>
|
||||
<th>Remove</th>
|
||||
@ -29,10 +30,18 @@
|
||||
<td>{{axe.power | number: '1.2-2'}} <small>W</small> </td>
|
||||
<td>{{axe.temp}} <small>C</small></td>
|
||||
<td>{{axe.bestDiff}}</td>
|
||||
<td>{{axe.version}}</td>
|
||||
<td><button class="btn btn-primary" (click)="edit(axe)">✎</button></td>
|
||||
<td><button class="btn btn-danger" (click)="restart(axe)">♺</button></td>
|
||||
<td><button class="btn btn-secondary" (click)="remove(axe)">✖</button></td>
|
||||
</tr>
|
||||
</ng-container>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="modal-backdrop" *ngIf="showEdit" (click)="showEdit = false"></div>
|
||||
<div class="modal card" *ngIf="showEdit">
|
||||
<div class="close" (click)="showEdit = false">✖</div>
|
||||
<h1>{{selectedAxeOs.ip}}</h1>
|
||||
<app-edit [uri]="'http://' + selectedAxeOs.ip"></app-edit>
|
||||
</div>
|
@ -23,4 +23,33 @@ td {
|
||||
|
||||
a {
|
||||
color: white;
|
||||
}
|
||||
|
||||
.modal-backdrop {
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
position: fixed;
|
||||
z-index: 9;
|
||||
}
|
||||
|
||||
.modal {
|
||||
position: fixed;
|
||||
max-height: 70vh;
|
||||
width: 50vw;
|
||||
z-index: 999;
|
||||
top: 0;
|
||||
left: 0;
|
||||
margin: 12vh 25vw;
|
||||
padding: 50px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.close {
|
||||
position: absolute;
|
||||
right: 20px;
|
||||
top: 20px;
|
||||
cursor: pointer;
|
||||
}
|
@ -17,6 +17,9 @@ export class SwarmComponent {
|
||||
|
||||
public refresh$: BehaviorSubject<null> = new BehaviorSubject(null);
|
||||
|
||||
public selectedAxeOs: any = null;
|
||||
public showEdit = false;
|
||||
|
||||
constructor(
|
||||
private fb: FormBuilder,
|
||||
private systemService: SystemService,
|
||||
@ -81,8 +84,10 @@ export class SwarmComponent {
|
||||
}
|
||||
|
||||
public edit(axe: any) {
|
||||
|
||||
this.selectedAxeOs = axe;
|
||||
this.showEdit = true;
|
||||
}
|
||||
|
||||
public restart(axe: any) {
|
||||
this.systemService.restart(`http://${axe.ip}`).subscribe(res => {
|
||||
|
||||
|
@ -57,8 +57,8 @@ export class SystemService {
|
||||
return this.httpClient.post(`${uri}/api/system/restart`, {});
|
||||
}
|
||||
|
||||
public updateSystem(update: any) {
|
||||
return this.httpClient.patch(`/api/system`, update);
|
||||
public updateSystem(uri: string = '', update: any) {
|
||||
return this.httpClient.patch(`${uri}/api/system`, update);
|
||||
}
|
||||
|
||||
|
||||
|
@ -199,8 +199,37 @@ static esp_err_t PATCH_update_swarm(httpd_req_t * req)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t set_cors_headers(httpd_req_t * req)
|
||||
{
|
||||
return httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*") == ESP_OK &&
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS") == ESP_OK &&
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Headers", "Content-Type") == ESP_OK
|
||||
? ESP_OK
|
||||
: ESP_FAIL;
|
||||
}
|
||||
|
||||
static esp_err_t handle_options_request(httpd_req_t * req)
|
||||
{
|
||||
// Set CORS headers for OPTIONS request
|
||||
if (set_cors_headers(req) != ESP_OK) {
|
||||
httpd_resp_send_500(req);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
// Send a blank response for OPTIONS request
|
||||
httpd_resp_send(req, NULL, 0);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t PATCH_update_settings(httpd_req_t * req)
|
||||
{
|
||||
// Set CORS headers
|
||||
if (set_cors_headers(req) != ESP_OK) {
|
||||
httpd_resp_send_500(req);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
int total_len = req->content_len;
|
||||
int cur_len = 0;
|
||||
char * buf = ((rest_server_context_t *) (req->user_ctx))->scratch;
|
||||
@ -266,11 +295,11 @@ static esp_err_t GET_swarm(httpd_req_t * req)
|
||||
{
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
|
||||
// Add CORS headers
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Headers", "Content-Type");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Credentials", "true");
|
||||
// Set CORS headers
|
||||
if (set_cors_headers(req) != ESP_OK) {
|
||||
httpd_resp_send_500(req);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
char * swarm_config = nvs_config_get_string(NVS_CONFIG_SWARM, "[]");
|
||||
httpd_resp_sendstr(req, swarm_config);
|
||||
@ -282,11 +311,11 @@ static esp_err_t GET_system_info(httpd_req_t * req)
|
||||
{
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
|
||||
// Add CORS headers
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Headers", "Content-Type");
|
||||
httpd_resp_set_hdr(req, "Access-Control-Allow-Credentials", "true");
|
||||
// Set CORS headers
|
||||
if (set_cors_headers(req) != ESP_OK) {
|
||||
httpd_resp_send_500(req);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
char * ssid = nvs_config_get_string(NVS_CONFIG_WIFI_SSID, CONFIG_ESP_WIFI_SSID);
|
||||
char * wifiPass = nvs_config_get_string(NVS_CONFIG_WIFI_PASS, CONFIG_ESP_WIFI_PASSWORD);
|
||||
@ -513,6 +542,14 @@ esp_err_t start_rest_server(void * pvParameters)
|
||||
.uri = "/api/system", .method = HTTP_PATCH, .handler = PATCH_update_settings, .user_ctx = rest_context};
|
||||
httpd_register_uri_handler(server, &update_system_settings_uri);
|
||||
|
||||
httpd_uri_t options_uri = {
|
||||
.uri = "/api/system",
|
||||
.method = HTTP_OPTIONS,
|
||||
.handler = handle_options_request,
|
||||
.user_ctx = NULL,
|
||||
};
|
||||
httpd_register_uri_handler(server, &options_uri);
|
||||
|
||||
httpd_uri_t update_post_ota_firmware = {
|
||||
.uri = "/api/system/OTA", .method = HTTP_POST, .handler = POST_OTA_update, .user_ctx = NULL};
|
||||
httpd_register_uri_handler(server, &update_post_ota_firmware);
|
||||
|
Loading…
x
Reference in New Issue
Block a user