-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (43 loc) · 1.12 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
package main
import (
"fmt"
"net"
"net/http"
"strings"
"github.com/moeghifar/meng/core"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
zerolog.NewConsoleWriter()
log.Info().Msg("+++ meng +++")
config := core.Readflag()
if config.DynamicBindNic.Enabled {
startingPort := config.DynamicBindNic.StartPort
nics, err := net.Interfaces()
if err != nil {
log.Panic().Err(err)
}
for _, nic := range nics {
if strings.HasPrefix(nic.Name, config.DynamicBindNic.Prefix) {
log.Info().Msgf("found nic %s bind to port %d", nic.Name, startingPort)
go func() {
if err := listenAndServe(startingPort, core.SetServer(core.CreateTransportWithNic(nic))); err != nil {
log.Panic().Err(err)
}
}()
startingPort++
}
}
}
if err := listenAndServe(config.HttpPort, core.SetServer(core.CreateTransport(nil))); err != nil {
log.Fatal().Err(err)
}
}
func listenAndServe(port int, handler http.Handler) error {
log.Info().Msgf("serving in port %d", port)
return http.ListenAndServe(intToPort(port), handler)
}
func intToPort(port int) string {
return fmt.Sprintf(":%d", port)
}