当前位置:网站首页>Goframe framework (RK boot): rapid configuration of server CORS
Goframe framework (RK boot): rapid configuration of server CORS
2022-06-23 02:36:00 【Trespass 】
Introduce
How does this article describe rk-boot Quick configuration server CORS.
What is? CORS? Cross source resource sharing (CORS) ( Cross domain resource sharing ) It's based on HTTP The mechanism of head , The mechanism allows the server to label other than itself origin( Domain , Protocol and port ), So that the browser can access and load these resources .
Please visit the following address for a complete tutorial :
install
go get github.com/rookie-ninja/rk-boot/gf
Quick start
1. establish boot.yaml
boot.yaml The file will tell rk-boot How to start gogf/gf service .
In this case , We only allow localhost:8080 Requests sent , Pass the verification .
---
gf:
- name: greeter # Required
port: 8080 # Required
enabled: true # Required
interceptors:
cors:
enabled: true # Optional, default: false
allowOrigins:
- "http://localhost:8080" # Optional, default: *2. establish main.go
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/rookie-ninja/rk-boot"
"github.com/rookie-ninja/rk-boot/gf"
"net/http"
)
func main() {
// Create a new boot instance.
boot := rkboot.NewBoot()
// Register handler
entry := rkbootgf.GetGfEntry("greeter")
entry.Server.BindHandler("/v1/hello", hello)
// Bootstrap
boot.Bootstrap(context.TODO())
boot.WaitForShutdownSig(context.TODO())
}
func hello(ctx *ghttp.Request) {
ctx.Response.WriteHeader(http.StatusOK)
ctx.Response.WriteJson(map[string]string{
"message": "hello!",
})
}3. establish cors.html
Let's create a simple web page , The web page will call localhost:8080/v1/hello, We verify by returning results CORS.
<!DOCTYPE html>
<html>
<body>
<h1>CORS Test</h1>
<p>Call http://localhost:8080/v1/hello</p>
<script type="text/javascript">
window.onload = function() {
var apiUrl = 'http://localhost:8080/v1/hello';
fetch(apiUrl).then(response => response.json()).then(data => {
document.getElementById("res").innerHTML = data["message"]
}).catch(err => {
document.getElementById("res").innerHTML = err
});
};
</script>
<h4>Response: </h4>
<p id="res"></p>
</body>
</html>4. Folder structure
. ├── boot.yaml ├── cors.html ├── go.mod ├── go.sum └── main.go 0 directories, 5 files
5. verification
open cors.html,cors.html Will from non 8080 Port send request , So it doesn't pass the test .
6. to CORS Add whitelist
This time, , We put allowOrigins configure localhost:*, namely , Let all from localhost Sent request , You can go through .
---
gf:
- name: greeter # Required
port: 8080 # Required
enabled: true # Required
interceptors:
cors:
enabled: true # Optional, default: false
allowOrigins:
- "http://localhost:*" # Optional, default: *7. verification
open cors.html
Complete parameters
Please refer to gf-cors Get the complete parameter list .
name | describe | type | The default value is |
|---|---|---|---|
gf.interceptors.cors.enabled | start-up CORS Interceptor | boolean | false |
gf.interceptors.cors.allowOrigins | Verifiable Origin Address . | []string | * |
gf.interceptors.cors.allowMethods | passable http method, Will be included in OPTIONS Requested Header in . | []string | All http methods |
gf.interceptors.cors.allowHeaders | passable http header, Will be included in OPTIONS Requested Header in . | []string | Headers from request |
gf.interceptors.cors.allowCredentials | Will be included in OPTIONS Requested Header in . | bool | false |
gf.interceptors.cors.exposeHeaders | Will be included in OPTIONS Requested Header Medium Header. | []string | "" |
gf.interceptors.cors.maxAge | Will be included in OPTIONS Requested Header Medium MaxAge. | int | 0 |
边栏推荐
- Mongodb aggregate query implements multi table associated query, type conversion, and returns specified parameters.
- Buuctf misc-[bjdctf2020] Nani
- How to make a borrowing card
- What is sitelock? What is the function?
- 【CodeWars】What is between?
- 5 trends brought to us by customers
- Practice and exploration of vivo live broadcast application technology
- Data analysis method - user group analysis
- Anaconda creates a new environment encounter pit
- Nebula operator cloud practice
猜你喜欢

Vs Code inadvertently disable error waveform curve

You must know the type and method of urllib

How to store, manage and view family photos in an orderly manner?

Analog Electronic Technology

5g core network and core network evolution

Understand GB, gbdt and xgboost step by step

5. concept of ruler method

Digital circuit logic design

5g spectrum

C language series - Section 4 - arrays
随机推荐
Analysis of web page status code
PNAs: power spectrum shows obvious bold resting state time process in white matter
what the fuck! If you can't grab it, write it yourself. Use code to realize a Bing Dwen Dwen. It's so beautiful ~!
Pychart installation instructions
[CodeWars]Matrix Determinant
5g access network and base station evolution
Reinforcement learning series (III) -gym introduction and examples
Operate attribute chestnut through reflection
Error in OpenCV image operation: error: (-215:assertion failed)_ src. empty() in function ‘cv::cvtColor‘
Schedule tasks to periodically restart remote services or restart machines
Custom shapes for ugui skill learning
Interviewer: what is the difference between SSH and SSM frameworks? How to choose??
Xgboost principle
Easygbs adds websocket message push, which can quickly locate video playback faults
For Xiaobai who just learned to crawl, you can understand it after reading it
Cut! 39 year old Ali P9 saved 150million
How to store, manage and view family photos in an orderly manner?
Call rest port to implement nailing notification
Capture passwords of all chrome versions
Deep scan log4j2 vulnerability using codesec code audit platform