-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
73 lines (65 loc) · 1.64 KB
/
app.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
package main
import (
"log"
"net/http"
"os"
"strconv"
"github.com/gin-gonic/gin"
"github.com/zemirco/papertrail"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
var (
mongoCollection = os.Getenv("MONGO_COLLECTION")
mongoDB = os.Getenv("MONGO_DB")
mongoURI = os.Getenv("MONGO_URI")
ptPort = os.Getenv("PT_PORT")
ptServer = os.Getenv("PT_SERVER")
runAddress = os.Getenv("RUN_ADDRESS")
)
type record struct {
ID bson.ObjectId `bson:"_id"`
Hits int `json:"hits"`
URL string `json:"url"`
UUID string `json:"uuid"`
}
func atoi(str string) int {
n, _ := strconv.Atoi(str)
return n
}
func main() {
remote := papertrail.Writer{
Port: atoi(ptPort),
Network: papertrail.UDP,
Server: ptServer,
}
log.SetOutput(&remote)
middleware := dbMiddleware()
router := gin.Default()
router.Use(middleware)
router.GET("/:uuid", redirect)
router.Run(runAddress)
}
func dbMiddleware() gin.HandlerFunc {
if session, err := mgo.Dial(mongoURI); err != nil {
panic(err)
} else {
return func(ctx *gin.Context) {
copy := session.Copy()
defer copy.Close()
ctx.Set("collection", copy.DB(mongoDB).C(mongoCollection))
ctx.Next()
}
}
}
func redirect(ctx *gin.Context) {
collection := ctx.MustGet("collection").(*mgo.Collection)
var result record
if err := collection.Find(bson.M{"uuid": ctx.Param("uuid")}).One(&result); err != nil {
ctx.String(http.StatusNotFound, "404 not found")
log.Printf("jre[dot]villas/%s - 404 not found\n", ctx.Param("uuid"))
return
}
ctx.Redirect(http.StatusFound, result.URL)
log.Printf("jre[dot]villas/%s - %s\n", ctx.Param("uuid"), result.URL)
}