Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
update cors and deeptrain oauth link
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Jul 20, 2023
1 parent cdde135 commit 6d7f921
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/src/views/IndexView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ function toggleUser() {
}
function login() {
location.href = "https://deeptrain.vercel.app/login?app=lightnotes";
location.href = "https://deeptrain.net/login?app=lightnotes";
}
function register() {
location.href = "https://deeptrain.vercel.app/register?app=lightnotes";
location.href = "https://deeptrain.net/register?app=lightnotes";
}
</script>
<template>
Expand Down
1 change: 1 addition & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func main() {
}

{
app.Use(middleware.CORSMiddleware())
app.Use(middleware.BuiltinMiddleWare(ConnectMySQL(), ConnectRedis()))
app.Use(middleware.ThrottleMiddleware())
app.Use(AuthMiddleware())
Expand Down
41 changes: 41 additions & 0 deletions backend/middleware/cors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package middleware

import (
"github.com/gin-gonic/gin"
"net/http"
)

var allowedOrigins = []string{
"https://fystart.cn",
"https://www.fystart.cn",
"https://40code.com",
"https://www.40code.com",
}

func Contains[T comparable](value T, slice []T) bool {
for _, item := range slice {
if item == value {
return true
}
}
return false
}

func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
origin := c.Request.Header.Get("Origin")
if Contains(origin, allowedOrigins) {
c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")

if c.Request.Method == "OPTIONS" {
c.Writer.Header().Set("Access-Control-Max-Age", "3600")
c.AbortWithStatus(http.StatusOK)
return
}
}

c.Next()
}
}

0 comments on commit 6d7f921

Please sign in to comment.