-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweblink.go
133 lines (113 loc) · 2.54 KB
/
weblink.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
package main
import (
_ "embed"
"encoding/json"
"html/template"
"log"
"net/http"
"os"
"sort"
"strconv"
"sync"
"time"
"github.com/gin-gonic/gin"
)
//go:embed showlink.html
var showlink string
var urls = make(map[int64]InputUrl)
var lock sync.RWMutex
var filename = "store.json"
type InputUrl struct {
InputUrlLink string
InputUrlName string
}
func main() {
//gin.SetMode(gin.ReleaseMode)
app := gin.Default()
initdata()
html := template.Must(template.New("showlink.html").Parse(showlink))
app.SetHTMLTemplate(html)
app.GET("/", func(c *gin.Context) {
con := ""
var keys IntSlice64
lock.RLock()
defer lock.RUnlock()
for k := range urls {
keys = append(keys, k)
}
sort.Sort(keys)
for _, v := range keys {
url, ok := urls[v]
m := strconv.FormatInt(v, 10)
if ok {
con += "<div>"
con += `<input type="button" value="Del" onclick="delpost('` + m + `')">`
con += "<a href='" + url.InputUrlLink + "'>"
con += url.InputUrlName + "</a>"
con += "</div><hr/>"
}
}
c.HTML(http.StatusOK, "showlink.html", gin.H{
"initdata": con,
})
})
app.POST("/add", func(ctx *gin.Context) {
key := time.Now().UnixNano()
url := ctx.PostForm("inputdata")
urlname := ctx.PostForm("inputname")
if urlname == "" {
urlname = url
}
lock.Lock()
defer lock.Unlock()
urls[key] = InputUrl{url, urlname}
save()
ctx.Redirect(http.StatusFound, "/")
})
app.POST("/del", func(ctx *gin.Context) {
id := ctx.PostForm("delid")
key, err := strconv.ParseInt(id, 10, 64)
if err != nil {
log.Fatal("Error key error:", err)
}
lock.Lock()
defer lock.Unlock()
delete(urls, key)
save()
ctx.Redirect(http.StatusFound, "/")
})
servePort := "8888"
if len(os.Args) == 2 {
_, errport := strconv.Atoi(os.Args[1])
if errport == nil {
servePort = os.Args[1]
}
}
app.Run(":" + servePort)
}
//////////////////////
func initdata() {
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0644)
defer f.Close()
if err != nil {
log.Fatal("Error opening URLStore:", err)
}
d := json.NewDecoder(f)
d.Decode(&urls)
}
func save() {
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
defer f.Close()
if err != nil {
log.Fatal("Error save URLStore:", err)
}
e := json.NewEncoder(f)
err = e.Encode(urls)
if err != nil {
log.Fatal("Error save URLStore:", err)
}
}
type IntSlice64 []int64
func (x IntSlice64) Len() int { return len(x) }
func (x IntSlice64) Less(i, j int) bool { return x[i] > x[j] }
func (x IntSlice64) Swap(i, j int) { x[i], x[j] = x[j], x[i] }