-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
139 lines (115 loc) · 3.46 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
package main
import (
"log"
"os"
"os/signal"
"runtime"
"time"
"github.com/go-awesome/shortlink/handler"
"github.com/go-awesome/shortlink/helper"
"github.com/akrylysov/pogreb"
badger "github.com/dgraph-io/badger/v3"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/favicon"
"github.com/gofiber/fiber/v2/middleware/recover"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
db, err := pogreb.Open(helper.DBFolder+"/pogreb/url.db", nil)
if err != nil {
log.Fatal(helper.ErrorPrint(err.Error(), helper.DB101))
return
}
defer db.Close()
opts := badger.DefaultOptions(helper.DBFolder + "/badger")
opts.NumVersionsToKeep = 1
opts.ReadOnly = false
if helper.BypassLockGuard {
// When uisng centralized DB server, it will be read from many and write to 1 node.
opts.BypassLockGuard = true
}
// Open badger Database
bdb, err := badger.Open(opts)
if err != nil {
log.Fatal(helper.ErrorPrint(err.Error(), helper.DB102))
}
defer bdb.Close()
var n int = 1
// Start the Gofiber APP
app := fiber.New(fiber.Config{
ReadTimeout: 10 * time.Minute,
WriteTimeout: 5 * time.Minute,
Prefork: false,
CaseSensitive: false,
StrictRouting: true,
DisableStartupMessage: true,
ErrorHandler: func(c *fiber.Ctx, err error) error {
code := fiber.StatusInternalServerError
if e, ok := err.(*fiber.Error); ok {
code = e.Code
}
return c.Status(code).JSON(fiber.Map{"error": "true", "Message": helper.ErrorPrint(err.Error(), helper.CO101)})
},
})
app.Use(recover.New(), favicon.New(favicon.Config{
File: "./favicon.ico",
}))
// Close the server and database on interruptor
go func() {
interruptor := make(chan os.Signal, 1)
signal.Notify(interruptor, os.Interrupt)
for range interruptor {
app.Shutdown()
db.Close()
bdb.Close()
os.Exit(1)
}
}()
/**
* fetch a short ID and redirect
* @param `shortURL` from URL
* @action also store analytics of the URL
* @return redirect
**/
api := app.Group("/api")
// IndexHandler
api.Get("/", handler.IndexHandler)
/**
* Store a new Long URL in storage.
* @param `url` from JSON
* @action generate unique short ID, increment n, store short id.
* @return json response with error and successful message
**/
api.Post("/create", handler.CreateHandler(n, bdb, db))
/**
* Update a Long URL in storage.
* @param `url` from JSON & Authorization token from Header
* @action update database for long URL
* @return json response with error and successful message
*/
api.Post("/update", handler.UpdateHandler(bdb, db))
/**
* Fetch a list of Long URL in storage for the API Holder.
* @param `Authorization Token` from Header
* @action fetch entry list from database
* @return json response with error and successful message with list
*/
api.Get("/fetch", handler.FetchAllHandler(bdb, db))
/**
* Fetch detail analytics in storage by the API holder
* @param `ShortID` from URL
* @action fetch all entry from database
* @return json response with error and successful message
**/
api.Get("/fetch/:code", handler.FetchSingleHandler(bdb, db))
/**
* Delete a Long URL in storage by the API holder
* @param `helper.DeleteURL struct` from JSON
* @action delete entry from database
* @return json response with error and successful message
**/
api.Post("/delete", handler.DeleteHandler(bdb, db))
app.Get("/:code", handler.RedirectToMeWebsite(db, bdb))
// run the server...
app.Listen(":8080")
}