-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (72 loc) · 1.4 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/gin-gonic/gin"
)
const (
_port = "10002"
_path = "pastebin_data"
)
var (
version string
port *string
path *string
)
func main() {
flagInit()
dbInit()
logInit()
attachmentInit()
run()
}
func run() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
defer dbClose()
gin.SetMode(gin.ReleaseMode)
r := gin.New()
//g := r.Group("/assets")
//{
// g.Use(cacheControl)
// publicFile(g)
//}
r.GET("/favicon.ico", cacheControl, func(c *gin.Context) {
c.Data(http.StatusOK, "image/x-icon", []byte{})
})
r.GET("/", cacheControl, indexPage)
r.GET("/:id", handleReqData)
r.POST("/", func(c *gin.Context) {
pathId, is := handleUploadData(c)
if is {
logWrite(c, pathId)
}
})
fmt.Printf("pastebin %s\n", version)
log.Printf("start HTTP server @ 0.0.0.0:%s\n", *port)
go func() {
if err := r.Run(":" + *port); err != nil {
log.Fatalln("start error: ", err)
}
}()
<-ch
}
func flagInit() {
port = flag.String("port", _port, "server port")
path = flag.String("path", _path, "data directory")
_v := flag.Bool("v", false, "version")
flag.Parse()
if *_v {
fmt.Printf("Version %s\nVisit github.com/nibazshab/pastebin", version)
os.Exit(0)
}
}
func cacheControl(c *gin.Context) {
c.Header("Cache-Control", "public, max-age=3600")
c.Next()
}