fix script backwards compatibility issue
This commit is contained in:
parent
022073c9a7
commit
b78ddd28f0
59
common.go
Normal file
59
common.go
Normal file
@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
valid "github.com/asaskevich/govalidator"
|
||||
"github.com/gin-gonic/gin"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type Post struct {
|
||||
Type string
|
||||
Uid string
|
||||
Key string
|
||||
Priv bool
|
||||
}
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
func validateKey(rKey string) bool {
|
||||
// if it doesn't match the key size or it isn't alphanumeric - throw it out
|
||||
if len(rKey) != keySize || !valid.IsAlphanumeric(rKey) {
|
||||
log.Warn().Str("rKey", rKey).
|
||||
Msg("delete request failed sanity check!")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *Post) URLString() string {
|
||||
var keyurl string = ""
|
||||
url := baseUrl + p.Type + "/" + string(p.Uid)
|
||||
if p.Key != "" {
|
||||
keyurl = baseUrl + "d/" + p.Type + "/" + p.Key
|
||||
}
|
||||
log.Info().
|
||||
Str("type", p.Type).
|
||||
Str("uid", p.Uid).Str("key", p.Key).
|
||||
Bool("private", p.Priv).Msg("success")
|
||||
if keyurl != "" {
|
||||
return url + "\nDelete: " + keyurl
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
func (p *Post) Serve(c *gin.Context) {
|
||||
var keyurl string = ""
|
||||
url := baseUrl + p.Type + "/" + string(p.Uid)
|
||||
if p.Key != "" {
|
||||
keyurl = baseUrl + "d/" + p.Type + "/" + p.Key
|
||||
}
|
||||
|
||||
log.Info().
|
||||
Str("type", p.Type).
|
||||
Str("uid", p.Uid).Str("key", p.Key).
|
||||
Bool("private", p.Priv).Msg("success")
|
||||
|
||||
c.JSON(201, gin.H{"Imgurl": url, "ToDelete": keyurl})
|
||||
return
|
||||
}
|
7
db.go
7
db.go
@ -25,3 +25,10 @@ func dbInit() {
|
||||
urlDB, _ = bitcask.Open(dbDir + "url") // url shortener entries
|
||||
log.Info().Msg("Initializing url database")
|
||||
}
|
||||
|
||||
func dbSync() {
|
||||
hashDB.Sync()
|
||||
keyDB.Sync()
|
||||
imgDB.Sync()
|
||||
urlDB.Sync()
|
||||
}
|
||||
|
16
main.go
16
main.go
@ -7,12 +7,13 @@ import (
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
var Banner string = "CiAgLGQgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogIDg4ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKTU04OE1NTSAsYWRQUFliYSwgOGIsZFBQWWJhLCAgICAgICxhZFBQWVliYSwgICxhZFBQWWJhLCAgCiAgODggICBhOCIgICAgICIiIDg4UCcgICAgIjhhICAgICAiIiAgICAgYFk4IGE4IiAgICAgIiIgIAogIDg4ICAgOGIgICAgICAgICA4OCAgICAgICBkOCAgICAgLGFkUFBQUFA4OCA4YiAgICAgICAgICAKICA4OCwgICI4YSwgICAsYWEgODhiLCAgICxhOCIgODg4IDg4LCAgICAsODggIjhhLCAgICxhYSAgCiAgIlk4ODggYCJZYmJkOCInIDg4YFliYmRQIicgIDg4OCBgIjhiYmRQIlk4ICBgIlliYmQ4IicgIAogICAgICAgICAgICAgICAgICA4OCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgODggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCg=="
|
||||
|
||||
func init() {
|
||||
|
||||
out := termenv.String(b64d(Banner))
|
||||
p := termenv.ColorProfile()
|
||||
out = out.Foreground(p.Color("#948DB8"))
|
||||
@ -62,6 +63,19 @@ func init() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
defer dbSync()
|
||||
go func() {
|
||||
c := make(chan os.Signal)
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
for {
|
||||
select {
|
||||
case <-c:
|
||||
log.Warn().Msg("Interrupt detected, shutting down gracefully...")
|
||||
dbSync()
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
}()
|
||||
go serveTermbin()
|
||||
// see router.go
|
||||
httpRouter()
|
||||
|
2
txt.go
2
txt.go
@ -102,7 +102,7 @@ func termPost(b []byte) {
|
||||
key = gouid.String(keySize, gouid.MixedCaseAlphaNum)
|
||||
}
|
||||
|
||||
hashDB.Put([]byte(hash), []byte(uid))
|
||||
hashDB.Put(hash, []byte(uid))
|
||||
|
||||
uid = gouid.String(uidSize, gouid.MixedCaseAlphaNum)
|
||||
key = gouid.String(keySize, gouid.MixedCaseAlphaNum)
|
||||
|
Loading…
Reference in New Issue
Block a user