-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
81 lines (68 loc) · 1.98 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
package main
import (
"html/template"
"net"
"net/http"
"os"
"strings"
"syscall"
)
func main() {
var err error
// prepare challenge HTML template
challengeTemplate, err = template.New("challenge.html").Parse(challengeHTMLTemplate)
if err != nil {
Error.Fatalf("challenge service template error: %s\n", err.Error())
}
// prepare challenge Lite HTML template
challengeLiteTemplate, err = template.New("challenge-lite.html").Parse(challengeLightHTMLTemplate)
if err != nil {
Error.Fatalf("challenge service template error: %s\n", err.Error())
}
// create new HTTP mux and define HTTP routes
mux := http.NewServeMux()
mux.HandleFunc("/", challengeHandle)
mux.HandleFunc("/auth", authHandle)
mux.HandleFunc("/favicon.ico", faviconHandler)
// run DB cleaner to clean expired keys
go cleanDB(&db)
// define net listner for HTTP serve function
var nl net.Listener
switch {
case strings.HasPrefix(cmdAddress, "unix:"):
// get socket path
socket := strings.TrimPrefix(cmdAddress, "unix:")
// remove old Unix socket
if _, err = os.Stat(socket); !os.IsNotExist(err) {
if err = syscall.Unlink(socket); err != nil {
Error.Fatalf("challenge service socket error: %s\n", err.Error())
}
}
// listen on unix socket
nl, err = net.Listen("unix", socket)
if err != nil {
Error.Fatalf("challenge service socket error: %s\n", err.Error())
}
// close unix socket on exit
defer func() {
if err = nl.Close(); err != nil {
Error.Fatalf("challenge service socket error: %s\n", err.Error())
}
}()
// change unix socket permissions
if err = os.Chmod(socket, os.FileMode(0777)); err != nil {
Error.Fatalf("challenge service socket error: %s\n", err.Error())
}
default:
// listen on TCP
nl, err = net.Listen("tcp", cmdAddress)
if err != nil {
Error.Fatalf("challenge service TCP error: %s\n", err.Error())
}
}
// start challenge server
err = http.Serve(nl, mux)
if err != nil {
Error.Fatalf("challenge service start error: %s\n", err.Error())
}
}