-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistorykv.go
113 lines (94 loc) · 3.97 KB
/
historykv.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"time"
historyConsul "github.com/tokopedia/historykv/src/consul"
historyDB "github.com/tokopedia/historykv/src/db"
historyGoogleLogin "github.com/tokopedia/historykv/src/googlelogin"
historyHTTP "github.com/tokopedia/historykv/src/http"
historySession "github.com/tokopedia/historykv/src/session"
historyUtil "github.com/tokopedia/historykv/src/util"
)
func main() {
log.Println("--------------------------------------------------")
log.Println("Starting HistoryKV")
log.Println("--------------------------------------------------")
// One Time Password
flagAdminPassword := flag.String("admin-password", "", "New password for user admin. Leave it empty if you do not want to change password.")
cfg := getConfig()
// Get Session
sessionType := "memory"
sessionAddress := ""
if cfg.Session.Redis != "" {
sessionType = "redis"
sessionAddress = cfg.Session.Redis
}
session := historySession.New(sessionType, sessionAddress)
// Get API Consul
if cfg.Consul.Prefix != "" && cfg.Consul.Prefix[len(cfg.Consul.Prefix)-1] != '/' {
cfg.Consul.Prefix = fmt.Sprintf("%s/", cfg.Consul.Prefix)
}
consulAPI := historyConsul.New(cfg.Consul.URI, cfg.Consul.Token, cfg.Consul.Datacenter, cfg.Consul.Prefix)
// Get DB
dbType := "sqlite"
dbPath := cfg.DB.Path
if cfg.DB.MySQL != "" {
dbType = "mysql"
dbPath = cfg.DB.MySQL
}
db := historyDB.New(dbType, dbPath, cfg.History.Limit, cfg.Consul.Prefix)
// If reset Password
if *flagAdminPassword != "" {
log.Println("--------------------------------------------------")
passwordMD5 := historyUtil.CreateHashPassword("admin", *flagAdminPassword)
if db.UpdateUser("admin", passwordMD5) {
log.Printf("Admin password changed to: %s\n", *flagAdminPassword)
} else {
log.Println("Failed to change admin password.")
}
log.Println("--------------------------------------------------")
}
// Google Login
glogin := historyGoogleLogin.New(cfg.GoogleLogin.ClientID, cfg.GoogleLogin.ClientSecret, cfg.GoogleLogin.Domain, cfg.GoogleLogin.CallbackURI, session)
if glogin.IsEnabled() {
log.Println("--------------------------------------------------")
log.Println("Google Login is Enabled...")
log.Printf("Using E-Mail Domain: [user]@%s\n", glogin.GetDomain())
log.Println("--------------------------------------------------")
} else {
log.Println("--------------------------------------------------")
log.Println("Google Login is Disabled...")
log.Println("--------------------------------------------------")
}
// Init HTTP
httpServe := historyHTTP.New(session, db, consulAPI, glogin, cfg.History.DisableLogin)
// Serve HTTP
http.HandleFunc("/glogin/login", httpServe.GoogleLogin)
http.HandleFunc("/glogin/callback", httpServe.GoogleCallback)
http.HandleFunc("/ajax/user/login.json", httpServe.Login)
http.HandleFunc("/ajax/user/logout.json", httpServe.Logout)
http.HandleFunc("/ajax/user/change-password.json", httpServe.ChangePassword)
http.HandleFunc("/ajax/user/change-token.json", httpServe.ChangeToken)
http.HandleFunc("/ajax/kv/history.json", httpServe.GetHistoryID)
http.HandleFunc("/ajax/kv/update.json", httpServe.CreateUpdateKey)
http.HandleFunc("/ajax/kv/delete.json", httpServe.DeleteKey)
http.HandleFunc("/ajax/kv/value.json", httpServe.GetValueKey)
http.HandleFunc("/ajax/kv/list.json", httpServe.GetList)
http.HandleFunc("/ajax/admin/list.json", httpServe.AdminGetUserList)
http.HandleFunc("/ajax/admin/update.json", httpServe.AdminCreateChangeUser)
http.HandleFunc("/ajax/admin/delete.json", httpServe.AdminDeleteUser)
// Page
http.HandleFunc("/", httpServe.PageGetIndex)
http.HandleFunc("/admin", httpServe.PageGetAdmin)
serve := &http.Server{
Addr: fmt.Sprintf("%s:%d", cfg.Listen.IP, cfg.Listen.Port),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
log.Printf("Listening to %s:%d\n", cfg.Listen.IP, cfg.Listen.Port)
log.Println("--------------------------------------------------")
log.Fatal(serve.ListenAndServe())
}