Commit 28f2e8e9 authored by chenhan wang's avatar chenhan wang
Browse files

Merge branch 'master1' of http://xlab.zju.edu.cn/git/chenhan/pastebin into master1

parents 230b533b 1a2ccef2
...@@ -4,8 +4,7 @@ import ( ...@@ -4,8 +4,7 @@ import (
"net" "net"
"net/http" "net/http"
"pastebin_backend/app/response" "pastebin_backend/app/response"
//"pastebin_backend/model"
"pastebin_backend/model"
//"error" //"error"
"time" "time"
......
package app package app
import ( import (
"pastebin_backend/utils" "backend/utils"
//"net/http" //"net/http"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
......
package app package app
import ( import (
"pastebin_backend/app/controller" "backend/app/controller"
"pastebin_backend/app/middleware" "backend/app/middleware"
//"pastebin_backend/model" //"backend/model"
) )
func addRoutes() { func addRoutes() {
......
module pastebin_backend module backend
go 1.19 go 1.19
......
...@@ -6,18 +6,27 @@ ...@@ -6,18 +6,27 @@
package main package main
import ( import (
"pastebin_backend/app" //"backend/app"
//"pastebin_backend/app/controller" //"pastebin_backend/app/controller"
"pastebin_backend/model" "backend/model"
//"time"
"fmt"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
func main() { func main() {
logrus.SetReportCaller(true) logrus.SetReportCaller(true)
model.Init() model.Init()
app.InitWebFramework() /* var user3 model.Users
app.StartServer() user3.Passwd = "123456"
model.Createuser(user3)
var url2 = new(model.Url)
var s2 string = "/person/example"
var s3 string = "lytgodbb"
for i := 0; i <= 10; i++ {
model.Checkt(s2)
}*/
fmt.Println(model.Find("fF2pGKiB","/person/example "))
//controller.BuildSocket() //controller.BuildSocket()
//controller.RecvFile() //controller.RecvFile()
} }
package model package model
import ( import (
//"fmt"
"time" "time"
"math/rand"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
...@@ -13,20 +15,24 @@ var DB *gorm.DB ...@@ -13,20 +15,24 @@ var DB *gorm.DB
var id1 uint = 0 var id1 uint = 0
func Init() { func Init() {
connectDatabase() ConnectDatabase()
var err error var err error
// insert auto-table // insert auto-table
err = DB.AutoMigrate(&Content{})
if err != nil {
logrus.Fatal(err)
}
err = DB.AutoMigrate(&Users{}) err = DB.AutoMigrate(&Users{})
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
err = DB.AutoMigrate(&content{}) err = DB.AutoMigrate(&Url{},&Sid{})
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
} }
func connectDatabase() { func ConnectDatabase() {
// config // config
viper.SetConfigName("config") viper.SetConfigName("config")
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
...@@ -47,35 +53,36 @@ func connectDatabase() { ...@@ -47,35 +53,36 @@ func connectDatabase() {
// Backcheck content through link // Backcheck content through link
//通过链接反查内容 //通过链接反查内容
func findlink(_Route string) content { func Findlink(_url string) string {
var p content var p Content
err := DB.First(&p, "Route = ?", _Route) err := DB.First(&p, "Url1 = ?", _url)
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
} }
return p return p.Route
} }
// save text information // save text information
//保存文本和文件信息 //保存文本和文件信息
func savetext(_Route string, _Time uint, _Passwd string,_Time1 time.Time) { func Savetext(_Route string, _Time uint, _Passwd string,_Time1 time.Time,_url string) {
id1++ id1++
p := content{ p := Content{
ID : id1, ID : id1,
Route: _Route, Route: _Route,
Time: _Time,
Passwd: _Passwd, Passwd: _Passwd,
Date: time.Now(), Date: time.Now(),
Time1: _Time1, Time1: _Time1,
Time2: 0, Time2: 0,
Url1: _url,
} }
DB.Create(&p) DB.Create(&p)
} }
// Check whether the number of visits exceeds the threshold and the time limit // Check whether the number of visits exceeds the threshold and the time limit
//检查是否超过总访问次数和截止时间 //检查是否超过总访问次数和截止时间
func checkt(_Route string) { func Checkt(_Route string) {
var p content var p Content
err := DB.First(&p, "Route = ?",_Route) err := DB.First(&p, "Route = ?",_Route)
if err != nil { if err != nil {
logrus.Error(err) logrus.Error(err)
...@@ -85,4 +92,59 @@ func checkt(_Route string) { ...@@ -85,4 +92,59 @@ func checkt(_Route string) {
if p.Time2 > p.Time||t.After(p.Time1) { if p.Time2 > p.Time||t.After(p.Time1) {
DB.Delete(&p) DB.Delete(&p)
} }
} }
\ No newline at end of file
//查找sessionid和url是否关联
func Find(sid string,_url string) bool {
var url Url
err1 := DB.First(&url, "Url1 = ?",_url).Error
//fmt.Println(url.Url1,url.ID,"\n")
if err1 != nil {
return false
}
var sid1 Sid
DB.Model(&url).Where("S = ?",sid).Association("User1").Find(&sid1)
if sid1.UrlID == 0 {
return false
}
return true
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789")
//随机生成字符串
func randStr(n int) string {
rand.Seed(time.Now().Unix())
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
//新建用户
func Createuser(_User Users) {
_User.SessionId=randStr(8)
DB.Create(&_User)
}
//新建链接
func Createlink(sid string , _passwd string , _url string) bool {
var p Content
DB.First(&p, "Url1 = ?", _url)
if p.Passwd == _passwd {
var p1 Sid
var p2 Url
p1.S = sid
err := DB.First(&p2,"Url1 = ?",_url).Error
// fmt.Println("yes ",err)
if err != nil {
//fmt.Println("yes ")
p2.Url1 = _url
DB.Create(&p2)
}
DB.Model(&p2).Association("User1").Append(&p1)
return true
}
return false
}
...@@ -2,9 +2,10 @@ package model ...@@ -2,9 +2,10 @@ package model
import ( import (
"time" "time"
"gorm.io/gorm"
) )
type content struct { type Content struct {
ID uint `gorm:"primarykey"` ID uint `gorm:"primarykey"`
Route string Route string
Time uint Time uint
...@@ -13,12 +14,26 @@ type content struct { ...@@ -13,12 +14,26 @@ type content struct {
Size uint Size uint
Time1 time.Time Time1 time.Time
Time2 uint Time2 uint
Url1 string
}
type Url struct {
gorm.Model
Url1 string
User1 []Sid
}
type Sid struct
{
S string
UrlID uint
} }
type Users struct { type Users struct {
SessionId string
Username string Username string
Passwd string Passwd string
Name string Name string
Size uint Size uint
Route string Route string
} }
\ No newline at end of file
pastebin @ ded77073
Subproject commit ded770739475219412b72eb9b8eb377405fb5803
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment