-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
201 lines (177 loc) · 6.05 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/mux"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"io"
"log"
"net/http"
"os"
"regexp"
"strings"
)
type Entry struct {
Shorthand string
Url string
}
const DATABASE = "sh"
const COLLECTION = "redirects"
var mongoclient *mongo.Client
func homePage(w http.ResponseWriter, _ *http.Request) {
indexhtml, err := Asset("index.html")
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
log.Fatal(err)
}
_, err = w.Write(indexhtml)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
log.Fatal(err)
}
}
func addEntry(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
http.Error(w, "Only JSON data is accepted for POST", http.StatusUnsupportedMediaType)
}
r.Body = http.MaxBytesReader(w, r.Body, 1048576)
var entry Entry
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields()
err := dec.Decode(&entry)
if err != nil {
//differentiate between errors, because not all errors are user errors (400 or 500)
//borrowed from https://www.alexedwards.net/blog/how-to-properly-parse-a-json-request-body
var syntaxErr *json.SyntaxError
var unmarshalTypeErr *json.UnmarshalTypeError
switch {
case errors.As(err, &syntaxErr):
http.Error(w, "Bad JSON format", http.StatusBadRequest)
case errors.As(err, unmarshalTypeErr):
http.Error(w, "JSON type mismatch (should be string/string)", http.StatusBadRequest)
case strings.HasPrefix(err.Error(), "json: unknown field"):
http.Error(w, "JSON body has unexpected field", http.StatusBadRequest)
case errors.Is(err, io.EOF):
http.Error(w, "JSON body can't be empty", http.StatusBadRequest)
case err.Error() == "http: request body too large":
http.Error(w, "JSON body too large, 1MB at max", http.StatusBadRequest)
default:
log.Println(err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
return
}
entry.Shorthand = strings.TrimSpace(entry.Shorthand)
entry.Url = strings.TrimSpace(entry.Url)
if entry.Shorthand == "" || entry.Url == "" {
http.Error(w, "Shorthand and Url can't be empty.", http.StatusBadRequest)
return
}
if !(strings.HasPrefix(entry.Url, "http://") || strings.HasPrefix(entry.Url, "https://")) {
http.Error(w, "Please start the Url with http:// or https://", http.StatusBadRequest)
return
}
match, err := regexp.MatchString("^[\\d\\w]+$", entry.Shorthand)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
log.Fatal(err)
}
if !match {
msg := fmt.Sprintf("Shorthand %s didn't match RegEx ^[\\d\\w]+$", entry.Shorthand)
http.Error(w, msg, http.StatusBadRequest)
return
}
//check if Shorthand is already in database, if not insert and give back 200
collection := mongoclient.Database(DATABASE).Collection(COLLECTION)
filter := bson.D{{Key: "shorthand", Value: entry.Shorthand}}
result := collection.FindOne(r.Context(), filter)
if result.Err() != mongo.ErrNoDocuments {
http.Error(w, "There already exists an entry with the specified Shorthand.", http.StatusBadRequest)
return
}
_, err = collection.InsertOne(r.Context(), entry)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
log.Fatal(err)
}
w.WriteHeader(http.StatusCreated)
msg := fmt.Sprintf("<h1>The association for %s to %s has been created!</h1>", entry.Shorthand, entry.Url)
hostname := os.Getenv("HOSTNAME")
if hostname != "" {
msg += fmt.Sprintf("\n<p>Click <a href=\"%s/%s\">here</a> to try out the redirect!", hostname, entry.Shorthand)
}
_, _ = fmt.Fprint(w, msg)
}
func redirectByKey(w http.ResponseWriter, r *http.Request) {
shorthand := mux.Vars(r)["shorthand"]
shorthand = strings.TrimSpace(shorthand)
match, err := regexp.MatchString("^[\\d\\w]+$", shorthand)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
log.Fatal(err)
}
if !match {
msg := fmt.Sprintf("Shorthand %s didn't match RegEx ^[\\d\\w]+$", shorthand)
http.Error(w, msg, http.StatusBadRequest)
return
}
collection := mongoclient.Database(DATABASE).Collection(COLLECTION)
filter := bson.D{{Key: "shorthand", Value: shorthand}}
result := collection.FindOne(r.Context(), filter)
if result.Err() == mongo.ErrNoDocuments {
msg := fmt.Sprintf("Shorthand %s is unknown", shorthand)
http.Error(w, msg, http.StatusBadRequest)
return
}
var entry Entry
err = result.Decode(&entry)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
log.Fatal(err)
}
http.Redirect(w, r, entry.Url, http.StatusPermanentRedirect)
}
func initializeDBClient() {
cs := fmt.Sprintf("mongodb://%s:%s@%s:%s", os.Getenv("MONGO_USER"),
os.Getenv("MONGO_PASSWORD"), os.Getenv("MONGO_URI"), os.Getenv("MONGO_PORT"))
clientOptions := options.Client().ApplyURI(cs)
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatal(err)
}
err = client.Ping(context.TODO(), nil)
if err != nil {
log.Fatal(err)
}
mongoclient = client
collection := mongoclient.Database(DATABASE).Collection(COLLECTION)
index := mongo.IndexModel{
Keys: bson.D{{"shorthand", 1}},
Options: options.Index().SetUnique(true),
}
_, err = collection.Indexes().CreateOne(context.TODO(), index)
if err != nil {
log.Fatal(err)
}
}
func robotstxt(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, "User-agent: *\nDisallow: /")
}
func main() {
log.Println("Connecting to mongoDB...")
initializeDBClient()
defer mongoclient.Disconnect(context.TODO())
log.Println("Connected!")
router := mux.NewRouter()
router.HandleFunc("/", homePage).Methods("GET")
router.HandleFunc("/", addEntry).Methods("POST")
router.HandleFunc("/robots.txt", robotstxt)
router.HandleFunc("/{shorthand}", redirectByKey).Methods("GET")
http.Handle("/", router)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", os.Getenv("SERVER_PORT")), nil))
}