-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
166 lines (130 loc) · 3.65 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
package main
import (
"flag"
"fmt"
"net"
"net/http"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/skratchdot/open-golang/open"
"github.com/sirupsen/logrus"
"github.com/socialplanner/instahelper/app/auth"
"github.com/socialplanner/instahelper/app/config"
"github.com/socialplanner/instahelper/app/handlers"
"github.com/socialplanner/instahelper/app/insta"
"github.com/socialplanner/instahelper/app/notifications"
)
func main() {
port := flag.Int("port", 3333, "Port to run Instahelper on")
debug := flag.Bool("debug", false, "Run in debug mode")
username := flag.String("user", "", "Username for instahelper")
password := flag.String("pass", "", "Password for instahelper")
useAuth := flag.Bool("auth", false, "Run using http basic auth")
noOpen := flag.Bool("noopen", false, "Do not automatically open the page.")
flag.Parse()
// To be removed on working prototype :)
fmt.Println("Rome wasn't built in a day.")
err := config.Open()
if err != nil {
logrus.Fatal("While opening db ", err)
}
defer config.Close()
c, err := config.Config()
if err != nil {
logrus.Fatal(err)
}
r := chi.NewRouter()
// They passed in a command line argument to use basic auth
if *useAuth || *password != "" || *username != "" {
if *password == "" {
pass, err := insta.Decrypt(c.Password)
if err != nil {
logrus.Fatal(err)
}
password = &pass
}
if *username == "" {
*username = c.Username
}
logrus.Info("Using authentication")
r.Use(auth.SimpleBasicAuth(*username, *password))
}
// MIDDLEWARE
// gzip compress
r.Use(middleware.DefaultCompress)
// do not cache
r.Use(middleware.NoCache)
// recovers from panic gracefully
r.Use(middleware.Recoverer)
// timeout requests after 30 seconds
r.Use(middleware.Timeout(time.Second * 30))
// redirect "/url/" to "/url"
r.Use(middleware.RedirectSlashes)
// ROUTES
// Pages
if *debug {
logrus.SetLevel(logrus.DebugLevel)
for _, p := range handlers.Pages {
r.With(middleware.Logger).Get(p.Link, p.Handler)
}
} else {
for _, p := range handlers.Pages {
r.Get(p.Link, p.Handler)
}
}
// Assets
r.With(handlers.MimeTypeMiddleware).Get(
"/assets/*",
handlers.AssetsHandler,
)
// API
r.Route("/api", func(r chi.Router) {
// Accounts
r.Route("/accounts", func(r chi.Router) {
r.Get("/", handlers.APIAccountsHandler)
r.Post("/create", handlers.APICreateAccountHandler)
r.Delete("/{username}", handlers.APIDeleteAccountHandler)
r.Post("/update/{username}", handlers.APIUpdateAccountHandler)
r.Post("/profile/{username}", handlers.APIAccProfileHandler)
})
r.Route("/notifications", func(r chi.Router) {
r.Delete("/", handlers.APIDeleteNotificationsHandler)
})
r.Route("/update", func(r chi.Router) {
r.Post("/to/{version}", handlers.APIUpdateToHandler)
r.Post("/", handlers.APIUpdateHandler)
})
r.Route("/settings", func(r chi.Router) {
r.Post("/edit", handlers.APISettingsEditHandler)
})
})
// Websocket handler
go notifications.Hub.Start()
r.Get("/ws", notifications.WSHandler)
go func() {
time.Sleep(time.Second)
ip, err := localIP()
if err != nil {
ip = "localhost"
}
logrus.Infof("Up and running at http://%s:%d !", ip, *port)
if !*noOpen {
err = open.Run(fmt.Sprintf("http://%s:%d", ip, *port))
if err != nil {
logrus.Error(err)
}
}
}()
logrus.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), r))
}
// localIP returns the local ip of the current device
func localIP() (string, error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return "", err
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP.String(), nil
}