-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (63 loc) · 1.77 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
package main
import (
"flag"
"net/http"
"os"
"path/filepath"
"github.com/gin-gonic/autotls"
"github.com/gin-gonic/gin"
"github.com/masong19hippows/go-website/email"
"github.com/masong19hippows/go-website/proxy"
"golang.org/x/crypto/acme/autocert"
)
func main() {
ex, err := os.Executable()
if err != nil {
panic(err)
}
exPath := filepath.Dir(ex)
//get port flag and password flag
password := flag.String("password", "", "Choose the app password obtained form no-reply email account.")
httpPort := flag.String("HTTPPort", "80", "Choose the HTTP Port.")
proxyEnable := flag.Bool("proxy", true, "Enable to disable proxy.")
flag.Parse()
// non-verbose
gin.SetMode(gin.ReleaseMode)
//default routes + the proxy handler
router := gin.New()
if *proxyEnable {
proxy.Init()
router.Use(proxy.Handler)
}
router.StaticFile("/", exPath+"/assets/index.html")
router.POST("/send_email", email.SendEmail(*password))
router.StaticFile("/favicon.ico", exPath+"/assets/favicon.ico")
router.StaticFile("/index.css", exPath+"/assets/index.css")
router.StaticFS("/images", http.Dir(exPath+"/assets/images/"))
ch := make(chan error)
go func(ch chan error) {
err := router.Run("0.0.0.0:" + *httpPort)
ch <- err
}(ch)
if *proxyEnable {
go func(ch chan error) {
var list []string
var proxies []proxy.Proxy
proxy.GetProxies(&proxies)
list = append(list, "masongarten.com")
for _, proxy := range proxies {
if proxy.Hostname {
list = append(list, proxy.AccessPrefix + ".masongarten.com")
}
}
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(list...),
Cache: autocert.DirCache(exPath + "/certs"),
}
err := autotls.RunWithManager(router, &m)
ch <- err
}(ch)
}
panic(<-ch)
}