-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
78 lines (62 loc) · 1.34 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
package main
import (
"fmt"
"github.com/iris-contrib/template/html"
"github.com/kataras/iris"
"gopkg.in/redis.v4"
"os"
)
var redisStorage = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf(
"%s:%s",
os.Getenv("REDIS_HOST"),
os.Getenv("REDIS_PORT"),
),
})
type UrlAPI struct {
*iris.Context
}
func (u UrlAPI) Get() {
u.MustRender("index.html", nil)
}
func (u UrlAPI) GetBy(id string) {
url, err := redisStorage.Get(fmt.Sprintf("URL_%s", id)).Result()
if err != nil {
u.JSON(iris.StatusNotFound, iris.Map{
"error": fmt.Sprintf("Not found: %s", id),
})
return
}
u.JSON(iris.StatusOK, iris.Map{
"location": url,
})
}
func (u UrlAPI) Post() {
url := u.FormValue("url")
key := getNextKey()
err := redisStorage.Set(fmt.Sprintf("URL_%d", key), url, 0).Err()
if err != nil {
u.JSON(iris.StatusInternalServerError, iris.Map{
"error": "Failed to save",
})
}
u.JSON(iris.StatusCreated, iris.Map{
"location": url,
"key": key,
})
}
func getNextKey() int64 {
if err := redisStorage.Incr("key").Err(); err != nil {
panic(err)
}
key, _ := redisStorage.Get("key").Int64()
return key
}
func main() {
defer redisStorage.Close()
iris.UseTemplate(html.New(html.Config{
Layout: "_layout.html",
})).Directory("./templates", ".html")
iris.API("/", UrlAPI{})
iris.Listen(fmt.Sprintf("0.0.0.0:%s", os.Getenv("PORT")))
}