-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (47 loc) · 1.36 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"runtime"
"time"
"github.com/2bytes/8k/internal/config"
"github.com/2bytes/8k/pkg/backend"
"github.com/2bytes/8k/pkg/storage"
"github.com/2bytes/8k/pkg/storage/inmemory"
"github.com/2bytes/8k/util"
)
func main() {
flag.Parse()
if *config.ShowVersion {
fmt.Printf("Version %q built using %s.\n", config.Version, runtime.Version())
os.Exit(0)
}
if *config.PublicAddress == "" {
log.Println("public address cannot be empty")
flag.Usage()
os.Exit(1)
}
s := backend.NewServer(storage.NewMediator(inmemory.New(*config.DataTTL, time.Second, *config.MaxItemsStored)))
http.HandleFunc("/", util.StatusCoder(s.HandleRequest))
bindAddrDisplay := *config.BindAddress
if bindAddrDisplay == "" {
bindAddrDisplay = "*"
}
var err error
fmt.Printf("UI address set to: %s\n", s.BaseAddress())
if *config.BindTLS {
fmt.Printf("Binding: https://%s:%d\n", bindAddrDisplay, *config.BindPort)
listenAddr := fmt.Sprintf("%s:%d", *config.BindAddress, *config.BindPort)
err = http.ListenAndServeTLS(listenAddr, *config.TLSCertFile, *config.TLSKeyFile, nil)
} else {
fmt.Printf("Binding: http://%s:%d\n", bindAddrDisplay, *config.BindPort)
listenAddr := fmt.Sprintf("%s:%d", *config.BindAddress, *config.BindPort)
err = http.ListenAndServe(listenAddr, nil)
}
if err != nil {
log.Println(err)
}
}