-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
123 lines (115 loc) · 3.11 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
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"cloud.google.com/go/firestore"
"context"
"html/template"
"log"
"net/http"
"sort"
"strings"
)
var linkdata map[string]interface{}
var doc *firestore.DocumentRef
// key/value structure for short link data
type kv struct {
Key string // short name
Count int64 // count
Url string // URL
Desc string // description
}
func redirect(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
path := strings.TrimLeft(r.URL.Path, "/")
if path == "" || path == "/" {
t, err := template.ParseFiles("home.html")
if err != nil {
log.Println(err.Error())
http.Error(w, http.StatusText(500), 500)
}
var kvs []kv
for k, v := range linkdata {
tmp := v.(map[string]interface{})
count := tmp["count"].(int64)
desturl := tmp["url"].(string)
desc := tmp["desc"].(string)
private := tmp["private"]
if private != nil && private.(bool) == true {
continue
}
kvs = append(kvs, kv{k, count, desturl, desc})
}
sort.Slice(kvs, func(i, j int) bool {
return kvs[i].Count > kvs[j].Count
})
err = t.Execute(w, kvs)
if err != nil {
log.Println(err.Error())
http.Error(w, http.StatusText(500), 500)
}
} else if strings.HasPrefix(path, "css/") ||
strings.HasPrefix(path, "img/") {
http.ServeFile(w, r, path)
} else if m, ok := linkdata[path]; ok {
v := m.(map[string]interface{})
if u, ok := v["url"]; ok {
log.Println("before: %d", v["count"])
v["count"] = v["count"].(int64) + 1
log.Println("after: %d", v["count"])
doc.Set(ctx, linkdata)
http.Redirect(w, r, u.(string), 301)
} else {
log.Println(w, "no URL found for event: %v", path)
return
}
} else {
http.ServeFile(w, r, "404.html")
}
}
func main() {
proj := "mco-fyi"
ctx := context.Background()
client, err := firestore.NewClient(ctx, proj)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
doc = client.Doc("Redirects/Shortlinks")
docsnap, err := doc.Get(ctx)
if err != nil {
log.Fatalln(err)
}
linkdata = docsnap.Data()
// This function runs in the background. It gets notified
// anytime the dataset changes, and reloads the local copy
// in response to those notifications so that the running
// instance always has the latest version of the data handy.
go func() {
iter := doc.Snapshots(ctx)
defer iter.Stop()
for {
docsnap, err := iter.Next()
if err != nil {
log.Fatalln(err)
}
linkdata = docsnap.Data()
}
}()
http.HandleFunc("/", redirect)
err = http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}