forked from Sunbalcony/note
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
137 lines (113 loc) · 3.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bytes"
"fmt"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
"github.com/pkg/errors"
"github.com/spf13/viper"
"math/rand"
"net/http"
"note/config"
"strings"
"time"
)
var DBEngine *gorm.DB
type Message struct {
Tag string `json:"tag"`
Text string `json:"text"`
}
type NoteApi struct {
Message
db *gorm.DB
}
const char = "abcdefghijklmnopqrstuvwxyz0123456789"
func RandChar(size int) string {
rand.NewSource(time.Now().UnixNano())
var s bytes.Buffer
for i := 0; i < size; i++ {
s.WriteByte(char[rand.Int63()%int64(len(char))])
}
return s.String()
}
func index(ctx *gin.Context) {
ctx.Header("Cache-Control", "no-cache, no-store, must-revalidate")
ctx.Header("Pragma", "no-cache")
ctx.Header("Expires", "0")
ctx.Redirect(http.StatusMovedPermanently, ctx.Request.URL.EscapedPath()+RandChar(viper.GetInt("note.keylength"))) //url path长度
}
func processHandle(ctx *gin.Context) {
fmt.Println(strings.Split(ctx.Request.URL.EscapedPath(), "/")[1])
msg := &Message{}
if ctx.Request.Method == http.MethodPost {
fmt.Println(ctx.Request.FormValue("text"))
//dataSet[ctx.Request.RequestURI] = ctx.PostForm("text")
if DBEngine.Where("tag = ?", strings.Split(ctx.Request.URL.EscapedPath(), "/")[1]).First(&msg).RecordNotFound() == true {
msg.Tag = strings.Split(ctx.Request.URL.EscapedPath(), "/")[1]
msg.Text = ctx.Request.FormValue("text")
DBEngine.Create(&msg)
}
if DBEngine.Where("tag = ?", strings.Split(ctx.Request.URL.EscapedPath(), "/")[1]).First(&msg).RecordNotFound() == false {
DBEngine.Model(&msg).Where("tag = ?", strings.Split(ctx.Request.URL.EscapedPath(), "/")[1]).Update("text", ctx.Request.FormValue("text"))
}
}
if ctx.Request.Method == http.MethodGet {
if ctx.Request.URL.Path != "/favicon.ico" {
DBEngine.Where("tag = ?", strings.Split(ctx.Request.URL.EscapedPath(), "/")[1]).First(&msg)
ctx.HTML(http.StatusOK, "index.html", gin.H{"title": "免登录 可分享 实时保存的记事本", "text": msg.Text})
}
}
}
func NewNoteApi() *NoteApi {
return &NoteApi{db: DBEngine}
}
// Api POST请求传递参数Text
func (r *NoteApi) create(ctx *gin.Context) {
msg := &Message{}
ctx.Bind(&r.Message)
randomTag := RandChar(10)
if r.db.Where("tag = ?", randomTag).First(msg).RecordNotFound() == true {
r.Message.Tag = randomTag
if affected := r.db.Create(r.Message).RowsAffected; affected == 1 {
//如果你配置了nginx代理并启用了HTTPS 下面就填https如果没有就是http
ctx.String(http.StatusOK, fmt.Sprintf("http://%s/%s", ctx.Request.Host, randomTag))
return
}
ctx.String(http.StatusInternalServerError, "CreateNotebookTextFailed")
return
}
}
//POST请求传递参数Tag和Text
func (r *NoteApi) update(ctx *gin.Context) {
msg := &Message{}
ctx.Bind(&r.Message)
if r.db.Where("tag = ?", r.Message.Tag).First(msg).RecordNotFound() == false {
if affected := r.db.Model(r.Message).Where("tag = ?", r.Message.Tag).Update("text", &r.Message.Text).RowsAffected; affected != 0 {
ctx.String(http.StatusOK, "UpdateNotebookTextSuccess")
return
}
ctx.String(http.StatusOK, "UpdateNotebookTextNotChanged")
return
}
ctx.String(http.StatusInternalServerError, "UpdateNotebookTextFailed")
}
func init() {
//初始化配置文件信息
config.InitConfig()
//初始化数据库连接
db, err := NewDBEngine()
if err != nil {
panic(errors.Wrap(err,"初始化数据库链接异常"))
}
DBEngine = db
}
func main() {
r := gin.Default()
r = NewRoutes(r)
port := viper.GetString("note.serverPort")
if port != "" {
r.Run(":" + port) //监听端口
}
panic(r.Run())
}