-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (50 loc) · 1.45 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
package main
import (
"log"
"net/http"
"os"
"strings"
"time"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
type realClock struct {}
func (realClock) Now() time.Time { return time.Now() }
func main() {
dbName := os.Getenv("MONGODB_DB_NAME")
if dbName == "" {
dbName = "url-shortener"
}
collectionName := os.Getenv("MONGODB_COLLECTION_NAME")
if collectionName == "" {
collectionName = "shortURL"
}
s := &server{
ShortURLPrefix: os.Getenv("SHORT_URL_PREFIX"),
DB: &mongoDatabase{
URL: os.Getenv("MONGODB_URL"),
DBName: dbName,
CollectionName: collectionName,
},
Clock: realClock{},
}
if superUsers := strings.TrimSpace(os.Getenv("SUPER_USERS")); superUsers != "" {
s.SuperUser = map[string]bool{}
for _, superUser := range strings.Split(superUsers, ",") {
s.SuperUser[strings.TrimSpace(superUser)] = true
}
}
r := mux.NewRouter()
r.HandleFunc("/"+internalPagesPrefix+"/list", s.List).Methods("POST")
r.HandleFunc("/"+internalPagesPrefix+"/save", s.Save).Methods("POST")
r.HandleFunc("/"+internalPagesPrefix+"/{name}", s.Delete).Methods("DELETE")
r.HandleFunc("/{name}{folder:(?:/.*)?}", s.Load)
r.HandleFunc("/", func(response http.ResponseWriter, request *http.Request) {
http.ServeFile(response, request, "public/index.html")
})
port := "5000"
if envPort := os.Getenv("PORT"); envPort != "" {
port = envPort
}
log.Fatal(http.ListenAndServe(":"+port, handlers.LoggingHandler(os.Stdout, r)))
}