This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortsh.go
173 lines (133 loc) · 5.4 KB
/
shortsh.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/short-sh/shortsh-backend/models"
"github.com/short-sh/shortsh-backend/utils"
"github.com/spf13/viper"
"log"
"net"
"net/http"
"net/url"
"runtime"
)
var engine *xorm.Engine
func main() {
// maximize CPU usage for maximum performance
runtime.GOMAXPROCS(runtime.NumCPU())
var err error
// read config
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath("./_config") // optionally look for config in the working directory
viper.AddConfigPath(".") // optionally look for config in the working directory
err = viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
engine, err = xorm.NewEngine("mysql", viper.GetString("mysql_dsn"))
if err != nil {
log.Fatalf("We could not connect to the database %v\n", err)
}
engine.ShowSQL(true)
engine.Logger().SetLevel(core.LOG_DEBUG)
// Sync models
engine.Sync2(new(models.Url), new(models.ShortDomains), new(models.Visitors))
e := echo.New()
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{viper.GetString("allow_origins")},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAcceptEncoding, echo.HeaderAuthorization, "X-Requested-With", "Cache-Control"},
}))
e.Use(middleware.RequestIDWithConfig(middleware.RequestIDConfig{}))
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "short.sh engine")
})
e.POST("/shorten", func(c echo.Context) error {
urlMeta := new(models.Url)
var protocol string // protocol of the short domain
if err := c.Bind(urlMeta); err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"success": false, "msg": "There was an error while processing your request!"})
}
parsedURL, err := url.Parse(urlMeta.Url)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"success": false, "msg": "This does not seem to be a valid URL!"})
}
host, _, _ := net.SplitHostPort(parsedURL.Host)
if len(host) == 0 {
host = parsedURL.Host
}
if host == "a2.to" || host == "short.sh" || len(host) == 0 {
return c.JSON(http.StatusForbidden, echo.Map{"success": false, "msg": "This does not seem to be a valid URL!"})
}
urlMeta.SetRandomID(3)
urlMeta.SetDomain()
data, exists := utils.UrlExists(engine, urlMeta.Url)
shortDomain := utils.GetShortDomain(engine, urlMeta)
if shortDomain == nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"success": false, "msg": "There was an error while communicating with the database!"})
}
if shortDomain.Secure {
protocol = "https"
} else {
protocol = "http"
}
if exists {
return c.JSON(http.StatusOK, echo.Map{"success": true, "url": protocol + "://" + shortDomain.ShortDomain + "/" + data.ShortId})
}
_, err = engine.Insert(urlMeta)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"success": false, "msg": "There was an error while communicating with the database!"})
}
return c.JSON(http.StatusOK, echo.Map{"success": true, "url": protocol + "://" + shortDomain.ShortDomain + "/" + urlMeta.ShortId})
})
e.GET("/url/:shortID/stats", func(c echo.Context) error {
shortID := c.Param("shortID")
var urlMeta = models.Url{ShortId: shortID}
has, err := engine.Get(&urlMeta)
fmt.Println(urlMeta)
if !has || err != nil {
return c.JSON(http.StatusNotFound, echo.Map{"success": false})
}
stats2 := models.UrlStatRet{}
rows, err := engine.SQL("SELECT url.id,url.created_at,url.url,short_domains.short_domain,short_domains.secure,COUNT(visitors.id) as count FROM url LEFT JOIN visitors ON url.id=visitors.url LEFT JOIN short_domains ON url.short_domain=short_domains.id WHERE short_id=? LIMIT 1;", shortID).Rows(stats2)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"success": false, "error": "Internal Server Error!"})
}
defer rows.Close()
for rows.Next() {
err = rows.Scan(&stats2)
if err != nil {
fmt.Println("Error", err)
}
}
// if secure true
if stats2.Secure == 1 {
stats2.ShortDomain = "https://" + stats2.ShortDomain
} else {
stats2.ShortDomain = "http://" + stats2.ShortDomain
}
// stats per day
singleDay := models.UrlStatDaily{}
var daily []models.UrlStatDaily
rows2, err := engine.SQL("SELECT DATE_FORMAT(visitors.created_at, \"%Y-%m-%d\") as day,COUNT(visitors.id) as clicks FROM url LEFT JOIN visitors ON url.id=visitors.url LEFT JOIN short_domains ON url.short_domain=short_domains.id WHERE short_id=? GROUP BY DAY(visitors.created_at);", shortID).Rows(singleDay)
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"success": false, "error": "Internal Server Error!"})
}
defer rows2.Close()
for rows2.Next() {
err = rows2.Scan(&singleDay)
fmt.Println(singleDay)
if err == nil {
daily = append(daily, singleDay)
}
}
if err != nil {
return c.JSON(http.StatusInternalServerError, echo.Map{"success": false, "msg": "There was a problem while communicating with the database!"})
}
return c.JSON(http.StatusOK, echo.Map{"success": true, "stats": stats2, "daily_stats": daily})
})
e.Logger.Fatal(e.Start(":1323"))
}