-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
101 lines (83 loc) · 2.2 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
package main
import (
"log"
"net"
"net/http"
"os"
"strconv"
"github.com/LevInteractive/dwarf/logger"
"github.com/LevInteractive/dwarf/pb"
"github.com/LevInteractive/dwarf/storage"
"github.com/go-redis/redis"
"github.com/gorilla/mux"
"google.golang.org/grpc"
)
// BaseURL is the base URL of the application. Short codes are appended on to it
// to ultimately construct the full short url.
var BaseURL string
// NotFoundURL is the URL used when Dwarf needs to redirect if a short code
// doesn't exist. We also redirect to this if the user lands on the root (/) of
// Dwarf.
var NotFoundURL string
func init() {
BaseURL = GetEnv("APP_BASE_URL", "https://example.com")
NotFoundURL = GetEnv("NOTFOUND_REDIRECT_URL", "https://google.com")
}
// GetEnv grabs env with a fallback
func GetEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func main() {
// Configure the Redis store. Redis is all we have now so eh.
db, err := strconv.Atoi(GetEnv("REDIS_DB", "0"))
if err != nil {
log.Fatal(err)
}
charFloor, err := strconv.Atoi(GetEnv("CHAR_FLOOR", "3"))
if err != nil {
log.Fatal(err)
}
store := storage.Redis{
CharFloor: charFloor,
Conn: &redis.Options{
Addr: GetEnv("REDIS_SERVER", "localhost:6379"),
Password: GetEnv("REDIS_PASS", ""),
DB: db,
},
}
store.Init()
go setupHTTPDiscovery(store)
setupGrpcDiscovery(store)
}
func setupGrpcDiscovery(store storage.IStorage) {
lis, err := net.Listen("tcp", GetEnv("GRPC_PORT", ":8001"))
if err != nil {
log.Fatalf("Failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterDwarfServer(s, &CreateServer{
Store: store,
})
logger.Info(
"Dwarf's gRPC server is listening here -> %s",
GetEnv("GRPC_PORT", ":8001"),
)
if err := s.Serve(lis); err != nil {
log.Fatalf("Failed to serve: %v", err)
}
}
func setupHTTPDiscovery(store storage.IStorage) {
appPort := GetEnv("APP_PORT", ":8000")
r := mux.NewRouter()
r.HandleFunc("/{hash:.*}", LookupHandler(store)).Methods("GET")
http.Handle("/", r)
logger.Info(
"Dwarf's public HTTP server is listening here -> %s and exposed here -> %s",
appPort,
BaseURL,
)
log.Fatal(http.ListenAndServe(appPort, r))
}