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

Commit

Permalink
update storage
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Jul 29, 2023
1 parent ea6da48 commit a8c265f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
14 changes: 14 additions & 0 deletions backend/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func ConnectMySQL() *sql.DB {
}

CreateUserTable(Database)
CreateStorageTable(Database)
return Database
}

Expand All @@ -70,3 +71,16 @@ func CreateUserTable(db *sql.DB) {
log.Fatal(err)
}
}

func CreateStorageTable(db *sql.DB) {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS storage (
id INT PRIMARY KEY AUTO_INCREMENT,
bind_id INT UNIQUE,
data TEXT(65535) NOT NULL
);
`)
if err != nil {
log.Fatal(err)
}
}
50 changes: 50 additions & 0 deletions backend/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"database/sql"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
"net/http"
"strings"
"time"
)

var allowedOrigins = []string{
Expand All @@ -16,6 +18,54 @@ var allowedOrigins = []string{
"http://localhost",
}

type Limiter struct {
Duration int
Count int64
}

func (l *Limiter) RateLimit(ctx *gin.Context, rds *redis.Client, ip string, path string) bool {
key := fmt.Sprintf("rate%s:%s", path, ip)
count, err := rds.Incr(ctx, key).Result()
if err != nil {
return true
}
if count == 1 {
rds.Expire(ctx, key, time.Duration(l.Duration)*time.Second)
}
return count > l.Count
}

var limits = map[string]Limiter{
"/login": {Duration: 10, Count: 5},
"/state": {Duration: 1, Count: 2},
"/storage/get": {Duration: 60, Count: 30},
"/storage/set": {Duration: 90, Count: 30},
}

func GetPrefixMap[T comparable](s string, p map[string]T) *T {
for k, v := range p {
if strings.HasPrefix(s, k) {
return &v
}
}
return nil
}

func ThrottleMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
ip := c.ClientIP()
path := c.Request.URL.Path
rds := c.MustGet("cache").(*redis.Client)
limiter := GetPrefixMap[Limiter](path, limits)
if limiter != nil && limiter.RateLimit(c, rds, ip, path) {
c.JSON(200, gin.H{"status": false, "reason": "You have sent too many requests. Please try again later."})
c.Abort()
return
}
c.Next()
}
}

func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
origin := c.Request.Header.Get("Origin")
Expand Down
18 changes: 18 additions & 0 deletions backend/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import "github.com/gin-gonic/gin"

type StorageRequestBody struct {
ChatGPT bool `json:"chatgpt" required:"true"`
Quote bool `json:"quote" required:"true"`
ToolBox bool `json:"toolbox" required:"true"`
About bool `json:"about" required:"true"`
ExactTime bool `json:"exactTime" required:"true"`
FocusInput bool `json:"focusInput" required:"true"`
Language string `json:"language" required:"true"`
Background string `json:"background" required:"true"`
}

func SaveStorageAPI(c *gin.Context) {

}
2 changes: 1 addition & 1 deletion src/assets/script/storage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {reactive, ref, watch} from 'vue';
import { reactive, watch } from 'vue';

function readDictConfig(data: Record<string, any>): Record<string, any> {
for (const key in data) {
Expand Down

1 comment on commit a8c265f

@vercel
Copy link

@vercel vercel bot commented on a8c265f Jul 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.