-
Notifications
You must be signed in to change notification settings - Fork 0
/
priggr.go
152 lines (123 loc) · 3.35 KB
/
priggr.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"os"
"time"
// Loads of dependencies, and what?
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
"github.com/satori/go.uuid"
)
// Global? Come at me, bro.
var db gorm.DB
type LogFormatter struct{}
func (f *LogFormatter) Format(entry *log.Entry) ([]byte, error) {
return []byte(fmt.Sprintf("%s [%s] %s\n", entry.Time.Format("2006-01-02 15:04:05.000"), entry.Level.String(), entry.Message)), nil
}
type Paste struct {
ID int `json:"-"`
PasteID string `json:"paste_id" gorm:"column:paste_id" sql:"unique_index"`
Created int64 `json:"created"`
Syntax string `json:"syntax"`
Paste string `json:"paste"`
Expires int64 `json:"expires"`
ExpireTimestamp int64 `json:"-"`
}
func realMain(c *cli.Context) {
lvl, err := log.ParseLevel(c.String("loglevel"))
if err != nil {
fmt.Fprintf(os.Stderr, "Could not parse log level. Must be one of: debug, info, warning, error, panic, fatal\n")
os.Exit(1)
}
formatter := &LogFormatter{}
log.SetFormatter(formatter)
log.SetOutput(os.Stderr)
log.SetLevel(lvl)
db, err = gorm.Open("sqlite3", c.String("database"))
if err != nil {
log.Fatalf("Could not open database from %s: %s", c.String("database"), err)
}
defer db.Close()
if lvl == log.DebugLevel {
db.LogMode(true)
}
db.AutoMigrate(&Paste{})
log.Debug("Database init done")
r := gin.Default()
r.GET("/p/:pasteid", getPaste)
r.POST("/p", storePaste)
log.Warningf("Priggr serving on %s:%d", c.String("bind"), c.Int("port"))
r.Run(fmt.Sprintf("%s:%d", c.String("bind"), c.Int("port")))
}
func storePaste(c *gin.Context) {
paste := Paste{}
err := c.Bind(&paste)
if err != nil {
c.JSON(400, gin.H{"message": "Could not marshal POST data"})
return
}
paste.Created = time.Now().Unix()
paste.PasteID = uuid.NewV4().String()
paste.ExpireTimestamp = time.Now().Add(time.Duration(paste.Expires) * time.Second).Unix()
log.Debugf("Paste data: %+v", paste)
db.Save(&paste)
c.JSON(200, gin.H{"message": "ok", "id": paste.PasteID})
}
func getPaste(c *gin.Context) {
pasteid := c.Param("pasteid")
if pasteid == "" {
c.JSON(400, gin.H{"message": "Paste ID not provided"})
return
}
paste := Paste{}
db.Find(&paste, "paste_id = ?", pasteid)
if paste.Paste == "" {
c.JSON(404, gin.H{"message": "Paste not found"})
return
}
c.JSON(200, paste)
}
func expirePastes() {
log.Debug("Expire timer fired, deleting expired pastes")
db.Where("expire_timestamp < ?", time.Now().Unix()).Delete(Paste{})
}
func main() {
app := cli.NewApp()
app.Name = "Priggr"
app.Usage = "Go-based Pastebin-alike"
app.Author = "Dane Elwell"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "loglevel, l",
Value: "info",
Usage: "Logging level: debug, info, warning, error, panic, fatal",
},
cli.StringFlag{
Name: "database, d",
Value: "/var/lib/prigger/prigger.db",
Usage: "Path to sqlite3 database",
},
cli.StringFlag{
Name: "bind, b",
Value: "0.0.0.0",
Usage: "Bind to this IP address",
},
cli.IntFlag{
Name: "port, p",
Value: 8998,
Usage: "Use this port for HTTP requests",
},
}
app.Action = realMain
expireTicker := time.NewTicker(time.Second * 60)
go func() {
for {
<-expireTicker.C
expirePastes()
}
}()
app.Run(os.Args)
}