-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
196 lines (156 loc) · 4.49 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"log"
"net/http"
"os"
"runtime"
"strconv"
"time"
"github.com/Zhima-Mochi/linkZapURL/config"
"github.com/Zhima-Mochi/linkZapURL/handlers/redirection"
"github.com/Zhima-Mochi/linkZapURL/handlers/shortening"
"github.com/Zhima-Mochi/linkZapURL/pkg/cache/redis"
"github.com/Zhima-Mochi/linkZapURL/pkg/database/mongodb"
"github.com/gin-gonic/gin"
doc "github.com/Zhima-Mochi/linkZapURL/docs"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
var (
machineID uint8
endpoint string
)
type Handler struct {
shortening shortening.Shortening
redirection redirection.Redirection
}
func setEnv() {
machineIDStr := os.Getenv("MACHINE_ID")
if machineIDStr == "" {
log.Fatal("MACHINE_ID is not set")
panic("MACHINE_ID is not set")
}
machineIDInt64, err := strconv.ParseUint(machineIDStr, 10, 8)
if err != nil {
log.Fatal(err)
panic(err)
}
if machineIDInt64 > 0xFF {
log.Fatal("MACHINE_ID is too large")
panic("MACHINE_ID is too large")
}
machineID = uint8(machineIDInt64)
log.Printf("Machine ID: %d", machineID)
endpoint = os.Getenv("ENDPOINT")
if endpoint == "" {
log.Fatal("ENDPOINT is not set")
panic("ENDPOINT is not set")
}
log.Printf("Endpoint: %s", endpoint)
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
setEnv()
config, err := config.GetConfig()
if err != nil {
log.Fatal(err)
panic(err)
}
database, err := mongodb.NewMongodb(config.Mongodb)
if err != nil {
log.Fatal(err)
panic(err)
}
cache, err := redis.NewRedis(config.Redis)
if err != nil {
log.Fatal(err)
panic(err)
}
shortening := shortening.NewShortening(machineID, database, cache)
redirection := redirection.NewRedirection(cache, database)
handler := &Handler{
shortening: shortening,
redirection: redirection,
}
router := gin.Default()
router.GET("/:code", handler.Redirect)
apiV1 := router.Group("/api/v1")
apiV1.POST("/urls", handler.Shorten)
// swagger
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
// swagger doc
doc.SwaggerInfo.Title = "linkZapURL"
doc.SwaggerInfo.Description = "A URL shortener service."
doc.SwaggerInfo.Version = "1.0"
router.Run(":8080")
}
type ShortenRequest struct {
URL string `json:"url" binding:"required" example:"https://example.com"`
ExpireAt string `json:"expireAt" binding:"required" example:"2025-02-08T09:20:41Z"`
ExpireAtInt64 int64 `json:"-" binding:"-"`
}
func (r *ShortenRequest) Bind(g *gin.Context) error {
if err := g.ShouldBindJSON(r); err != nil {
return err
}
expireAtTime, err := time.Parse(time.RFC3339, r.ExpireAt)
if err != nil {
return err
}
r.ExpireAtInt64 = expireAtTime.Unix()
return nil
}
type ShortenResponse struct {
ID string `json:"id" example:"5abcABC"`
ShortURL string `json:"shortURL" example:"https://localhost/5abcABC"`
}
// @Summary Shorten a URL
// @Description Shortens a given URL and provides an expiration time.
// @Tags URL Shortening
// @Accept json
// @Produce json
// @Param body body ShortenRequest true "Shorten Request"
// @Success 200 {object} ShortenResponse
// @Failure 400 {object} map[string]any
// @Failure 500 {object} map[string]any
// @Router /api/v1/urls [post]
func (h *Handler) Shorten(g *gin.Context) {
ctx := g.Request.Context()
req := ShortenRequest{}
if err := req.Bind(g); err != nil {
g.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
url, err := h.shortening.Shorten(ctx, req.URL, req.ExpireAtInt64)
if err != nil {
g.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
resp := ShortenResponse{
ID: url.Code,
ShortURL: endpoint + "/" + url.Code,
}
g.JSON(http.StatusCreated, resp)
}
// @Summary Redirect to a URL
// @Description Redirects to the original URL.
// @Tags URL Redirection
// @Param code path string true "Shortened URL Code"
// @Success 301 {string} string "Moved Permanently"
// @Failure 404 {object} map[string]any
// @Failure 500 {object} map[string]any
// @Router /{code} [get]
func (h *Handler) Redirect(g *gin.Context) {
ctx := g.Request.Context()
code := g.Param("code")
url, err := h.redirection.Redirect(ctx, code)
if err == redirection.ErrNotFound || err == redirection.ErrExpired || err == redirection.ErrInvalidCode {
g.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
} else if err != nil {
g.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
log.Printf("Redirecting to %s", url.URL)
g.Redirect(http.StatusMovedPermanently, url.URL)
}