-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
46 lines (36 loc) · 782 Bytes
/
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
package main
import (
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/Kamva/mgm"
"github.com/mariomenjr/go/models"
"github.com/mariomenjr/go/utils"
"github.com/mariomenjr/handlr"
"go.mongodb.org/mongo-driver/bson"
)
func Init() {
utils.LoadEnv()
models.Init()
}
func main() {
Init()
h := handlr.New()
h.HandlerFunc("/:id", linkHandler)
portNumber, err := strconv.Atoi(os.Getenv("PORT"))
if err != nil {
log.Fatal(err)
}
h.Start(portNumber)
}
func linkHandler(w http.ResponseWriter, r *http.Request) {
link := &models.Link{}
coll := mgm.Coll(link)
err := coll.First(bson.M{"id": strings.Trim(r.URL.Path, "/")}, link)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
http.Redirect(w, r, link.Url, http.StatusSeeOther)
}