-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (69 loc) · 1.92 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
package main
import (
"github.com/integrii/flaggy"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"net/http"
"os"
"strconv"
"time"
)
const (
envAddress = "LISTEN_ADDR"
envUser = "AUTH_USER"
envPassword = "AUTH_PASSWORD"
envDisableAuth = "DISABLE_AUTH"
defaultAddress = ":80"
)
var (
version = "dev"
handler *rssHandler
)
func main() {
log.Logger = zerolog.New(zerolog.NewConsoleWriter()).With().Timestamp().Str("version", version).Caller().Logger()
address := defaultAddress
authUser := ""
authPass := ""
disableAuth := false
flaggy.SetVersion(version)
flaggy.String(&address, "a", "address", "The local address the server listens on, in the for <address>:<port>.")
flaggy.String(&authUser, "u", "auth_user", "User part for basic http authentication of the endpoint.")
flaggy.String(&authPass, "p", "auth_password", "Secret part for basic http authentication of the endpoint.")
flaggy.Bool(&disableAuth, "", "disable_auth", "Disable authentication.")
flaggy.Parse()
adr := os.Getenv(envAddress)
user := os.Getenv(envUser)
pass := os.Getenv(envPassword)
disA := os.Getenv(envDisableAuth)
if adr != "" && (address == defaultAddress || address == "") {
address = adr
}
if user != "" && authUser == "" {
authUser = user
}
if pass != "" && authPass == "" {
authPass = pass
}
if disA != "" && !disableAuth {
var err error
disableAuth, err = strconv.ParseBool(disA)
if err != nil {
log.Fatal().Err(err).Msg("can't parse " + envDisableAuth)
}
}
if authPass == "" && !disableAuth {
log.Fatal().Msg("you MUST provide a password")
}
handler = newRssHandler(authUser, authPass, disableAuth)
server := &http.Server{
Addr: address,
Handler: handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Info().Msgf("listening on: '%s'", address)
if err := server.ListenAndServe(); err != nil {
log.Fatal().Err(err).Send()
}
}