-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (127 loc) · 4.33 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// MIT License
//
// Copyright (c) 2023 pleclech
// Github: https://github.com/pleclech
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"context"
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/pleclech/portainerproxy/pkg/portainer"
"go.uber.org/zap"
)
const (
defaultDockerHost = "unix:///var/run/docker.sock"
defaultAddress = ":8080"
)
var (
version = "0.0.0"
)
func main() {
// Parse command-line flags
versionFlag := flag.Bool("version", false, "Print the version")
portainerURL := flag.String("portainer", "", "Portainer URL")
portainerServiceName := flag.String("portainer-service-name", "", "if running docker, the name of the portainer service")
dockerHost := flag.String("host", defaultDockerHost, "real docker host")
useHTTPS := flag.Bool("https", false, "Enable HTTPS")
certFile := flag.String("cert", "", "Path to the certificate file")
keyFile := flag.String("key", "", "Path to the key file")
address := flag.String("address", defaultAddress, "Address and port to listen on")
debugFlag := flag.Bool("debug", false, "Enable debug mode")
flag.Parse()
// Print the version if the version flag is provided
if *versionFlag {
fmt.Printf("v%s", version)
return
}
var logger *zap.Logger
// Enable debug mode if the debug flag is provided
if *debugFlag {
// set debug mode for zap
logger, _ = zap.NewDevelopment()
// log.SetLevel(log.DebugLevel)
} else {
logger, _ = zap.NewProduction()
}
defer logger.Sync()
// split address into host and port
host, port, err := net.SplitHostPort(*address)
if err != nil {
logger.Fatal("Invalid address", zap.Error(err))
// log.Fatal(err)
}
// if host is empty, set it to localhost
if host == "" {
host = "localhost"
}
*address = host + ":" + port
proxy, err := portainer.NewProxy(logger, *portainerURL, *dockerHost, *address, *portainerServiceName)
if err != nil {
logger.Fatal(err.Error())
}
// Create an HTTP server
server := &http.Server{
Addr: *address,
Handler: proxy,
}
// Start the HTTP/HTTPS server based on the flag
go func() {
logger.Info("Starting proxy server...",
zap.String("version", version),
zap.String("Portainer url", *portainerURL),
zap.String("Docker host", *dockerHost),
zap.Bool("debug", *debugFlag))
logger.Sync()
var err error
if *useHTTPS {
if *certFile == "" || *keyFile == "" {
logger.Fatal("Both certificate file and key file paths must be provided for HTTPS mode")
}
logger.Info("HTTPS proxy server listening on", zap.String("address", *address))
err = server.ListenAndServeTLS(*certFile, *keyFile)
} else {
logger.Info("HTTP proxy server listening on", zap.String("address", *address))
err = server.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
logger.Fatal(err.Error())
}
}()
shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
// Block until a shutdown signal is received
<-shutdown
// Create a context with a timeout for graceful shutdown
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Attempt to gracefully shut down the server
if err := server.Shutdown(ctx); err != nil {
logger.Fatal("Server shutdown error", zap.Error(err))
}
// and then shutdown the server
logger.Info("Shutting down the server...")
}