2020-09-02 23:04:12 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-09-19 00:29:49 +01:00
|
|
|
"fmt"
|
2020-09-02 23:04:12 +01:00
|
|
|
"math/rand"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/BeryJu/passbook/proxy/pkg/server"
|
|
|
|
)
|
|
|
|
|
2020-09-19 00:29:49 +01:00
|
|
|
const helpMessage = `passbook proxy
|
|
|
|
|
|
|
|
Required environment variables:
|
|
|
|
- PASSBOOK_HOST: URL to connect to (format "http://passbook.company")
|
|
|
|
- PASSBOOK_TOKEN: Token to authenticate with
|
|
|
|
- PASSBOOK_INSECURE: Skip SSL Certificate verification`
|
|
|
|
|
2020-09-02 23:04:12 +01:00
|
|
|
// RunServer main entrypoint, runs the full server
|
|
|
|
func RunServer() {
|
|
|
|
pbURL, found := os.LookupEnv("PASSBOOK_HOST")
|
|
|
|
if !found {
|
2020-09-19 00:29:49 +01:00
|
|
|
fmt.Println("env PASSBOOK_HOST not set!")
|
|
|
|
fmt.Println(helpMessage)
|
|
|
|
os.Exit(1)
|
2020-09-02 23:04:12 +01:00
|
|
|
}
|
|
|
|
pbToken, found := os.LookupEnv("PASSBOOK_TOKEN")
|
|
|
|
if !found {
|
2020-09-19 00:29:49 +01:00
|
|
|
fmt.Println("env PASSBOOK_TOKEN not set!")
|
|
|
|
fmt.Println(helpMessage)
|
|
|
|
os.Exit(1)
|
2020-09-02 23:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pbURLActual, err := url.Parse(pbURL)
|
|
|
|
if err != nil {
|
2020-09-19 00:29:49 +01:00
|
|
|
fmt.Println(err)
|
|
|
|
fmt.Println(helpMessage)
|
|
|
|
os.Exit(1)
|
2020-09-02 23:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
|
|
|
ac := server.NewAPIController(*pbURLActual, pbToken)
|
|
|
|
|
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, os.Interrupt)
|
|
|
|
|
|
|
|
ac.Start()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-interrupt:
|
|
|
|
ac.Shutdown()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|