-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (87 loc) · 2.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
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
package main
import (
"log"
_rand "math/rand"
"os"
"strconv"
"github.com/dblclik/EasyCache/utils"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
const DefaultCacheLimit int = 10
var (
CacheLimit int
LRUCache *utils.DoublyLinkedList = utils.InitDoublyList()
CacheMap = map[string]string{}
InstanceID uint32 = _rand.Uint32()
HashCheckinURL string
)
/* TODO:
- Implement DLL + Updating (DONE)
- Add tests for existing functions (DONE)
- Consider adding auth module
- Add env file for config variables
- Add Config Vars to HEALTH
- Enable resource updates on CONFIG vars (that can be changed)
- Start time tracking for all actions + logging these
*/
// use godot package to load/read the .env file and
func initDotEnv(envFile string) bool {
// load .env file
err := godotenv.Load(envFile)
if err != nil {
log.Fatalf("Error loading .env file")
return false
}
return true
}
// return the value of the key
func goDotEnvVariable(key string) string {
log.Println(os.Getenv(key))
return os.Getenv(key)
}
func main() {
// initialize the env file
envLoaded := initDotEnv(".env")
if !envLoaded {
panic("env file could not be loaded; panicking!")
}
// before coming online, cache node needs to check in
// ** IF CHECKIN_HOST ENV IS "" THEN WILL NOT CHECK **
HashCheckinURL = goDotEnvVariable("CHECKIN_HOST")
if HashCheckinURL != "" {
// initiate BLOCKING checkin against CHECKIN_HOST
checkinOK := checkinToHashRing(HashCheckinURL)
if !checkinOK {
log.Fatalln("ERROR: Was not able to perform checkin, aborting now!")
}
} else {
log.Println("No checkin host provided, will not assume distributed")
}
// Initialize Cache Limit
log.Println("Initial Cache Limit set to: ", CacheLimit)
godotenv.Load()
var err error
CacheLimit, err = strconv.Atoi(goDotEnvVariable("CACHE_SIZE_LIMIT"))
log.Println("Cache Limit set to: ", CacheLimit)
if err != nil {
log.Println("WARNING: Could not set CacheLimit with env variable, using DEFAULT")
CacheLimit = DefaultCacheLimit
}
log.Println("Cache Limit set to: ", CacheLimit)
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Routes
// Health Check
e.GET("/health", health)
// Get Cache Value
e.GET("/cache", getCache)
// Put (key, value) into cache
e.POST("/cache", putCache)
// Start server
e.Logger.Fatal(e.Start(":1323"))
}