-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (152 loc) · 4.62 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// User structure
type User struct {
ID primitive.ObjectID `bson:"_id"`
Username string `bson:"username"`
Count uint64 `bson:"count"`
}
// Initalize mongodb connection
func initmongo(uri string) (context.Context, *mongo.Client, error) {
ctx := context.Background()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
return ctx, client, err
}
func launchAndServe() {
if USE_HEROKU == true {
// Get the heroku port assigned by heroku itself
heroku_port := os.Getenv("PORT")
if heroku_port == "" {
fmt.Println("No port found from heroku to listen")
return
}
fmt.Println("Launching server at port " + heroku_port)
if err := http.ListenAndServe(":"+heroku_port, nil); err != nil {
fmt.Println("Failed to launch the server, error: " + err.Error())
return
}
} else {
fmt.Println("Lauching server at " + DOMAIN + PORT)
if err := http.ListenAndServe(DOMAIN+PORT, nil); err != nil {
fmt.Println("Failed to launch the server, error: " + err.Error())
return
}
}
}
func main() {
// Issue initmongo with the URI
ctx, client, err := initmongo(MONGODB_URL)
if err != nil {
fmt.Println("MongoDB connection error: " + err.Error())
return
} else {
fmt.Println("Connected with MongoDB")
}
// Root page, basically the base URL
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Check the useragent to check if it contains github-camo
if len(r.UserAgent()) != 0 {
if !strings.Contains(r.UserAgent(), "github-camo") {
http.Error(w, "URL access only allowed on GitHub readme", http.StatusForbidden)
return
}
}
usr_name := r.URL.Query().Get("username")
first_color := r.URL.Query().Get("first_color")
second_color := r.URL.Query().Get("second_color")
var result bson.M
// Check username length
if len(usr_name) == 0 {
http.Error(w, "Please provide a username", http.StatusNotFound)
return
}
// Check if username length is more than 39 charecters
if usr_name != "" && len(usr_name) > 39 {
http.Error(w, "User name length must be under 39 charecters", http.StatusNotFound)
return
}
// Index database and get the collection
collection := client.Database("users").Collection("list")
e := collection.FindOne(ctx, bson.M{"username": usr_name}).Decode(&result)
if e != nil {
if e == mongo.ErrNoDocuments {
// If username doesn't exist in the database, create one
fmt.Println("Requested for new account, username: " + usr_name)
user := &User{
ID: primitive.NewObjectID(),
Username: usr_name,
Count: 1,
}
_, err := collection.InsertOne(ctx, *user)
// Check if valid params exist
if len(first_color) != 0 {
if len(second_color) != 0 {
svg_image(w, r, "#"+first_color, "#"+second_color, 0)
} else {
svg_image(w, r, "#"+first_color, DEFAULT_SECOND_COLOR, 0)
}
} else {
if len(second_color) != 0 {
svg_image(w, r, DEFAULT_FIRST_COLOR, "#"+second_color, 0)
} else {
svg_image(w, r, DEFAULT_FIRST_COLOR, DEFAULT_SECOND_COLOR, 0)
}
}
if err != nil {
fmt.Println("InsertOne returned an error: " + err.Error())
return
}
return
} else {
fmt.Println("FindOne returned an error: " + e.Error())
return
}
}
// Get key, value from result
for _, value := range result {
fmtval := fmt.Sprint(value)
var subresult struct {
Username string
Count uint64
}
ff := bson.M{"username": usr_name}
ex := collection.FindOne(ctx, ff).Decode(&subresult)
if ex != nil {
fmt.Println("FineOne returned an error: " + ex.Error())
return
}
// If fmtval variable's value is same as usr_name, render the SVG image
if fmtval == usr_name {
filter := bson.M{"username": usr_name}
collection.FindOneAndUpdate(ctx, filter, bson.M{"$set": bson.M{"count": subresult.Count + 1}})
// Check if valid params exist
if len(first_color) != 0 {
if len(second_color) != 0 {
svg_image(w, r, "#"+first_color, "#"+second_color, subresult.Count)
} else {
svg_image(w, r, "#"+first_color, DEFAULT_SECOND_COLOR, subresult.Count)
}
} else {
if len(second_color) != 0 {
svg_image(w, r, DEFAULT_FIRST_COLOR, "#"+second_color, subresult.Count)
} else {
svg_image(w, r, DEFAULT_FIRST_COLOR, DEFAULT_SECOND_COLOR, subresult.Count)
}
}
return
}
}
})
// Launch the server
launchAndServe()
}