Newer
Older
"forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/common"
"forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/mocks"
"forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/rootmux"
"forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/tokens"
"golang.org/x/crypto/acme/autocert"
var (
letsCacheDir = common.StringValueFromEnv("LETS_CACHE_DIR", "./letsencrypt_cache") // Let's Encrypt cache directory
httpsPort = common.IntValueFromEnv("HTTPS_PORT", 443) // HTTPS port to serve on
httpPort = common.IntValueFromEnv("HTTP_PORT", 80) // HTTP port to serve on, only used for Let's Encrypt HTTP Challenge
debugMode = common.BoolValueFromEnv("DEBUG_MODE", false) // Debug mode, disable Let's Encrypt, enable CORS and more logging
disableLetsEncrypt = common.BoolValueFromEnv("DISABLE_LETSENCRYPT", false) // Disable Let's Encrypt certificates (in normal mode) and use development certificates (./dev_certificates/localhost.crt and .key) instead
)
func init() {
if debugMode {
}
}
// Initializations
tokens.Init("./configs/tokenskey.json", debugMode)
// Create the server
rootMux := rootmux.CreateRootMux("web")
// Serve locally with https on debug mode or with let's encrypt on production mode
if debugMode {
// Init the hostname
mocks.Init(httpsPort)
// Start a mock oauth2 server if debug mode is on
mockOAuth2Port := ":8090"
go http.ListenAndServe(mockOAuth2Port, mocks.CreateMockOAuth2())
fmt.Println("Mock OAuth2 server Listening on: http://localhost" + mockOAuth2Port)
// Start a mock API server if debug mode is on
mockAPIPort := ":8091"
go http.ListenAndServe(mockAPIPort, mocks.CreateMockAPI())
fmt.Println("Mock API server Listening on: http://localhost" + mockAPIPort)
log.Fatal(http.ListenAndServeTLS(":"+strconv.Itoa(httpsPort), "./dev_certificates/localhost.crt", "./dev_certificates/localhost.key", rootMux.Router))
} else {
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
Cache: autocert.DirCache(letsCacheDir),
}
server := &http.Server{
Addr: ":" + strconv.Itoa(httpsPort),
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
MinVersion: tls.VersionTLS12,
},
ReadTimeout: 30 * time.Minute, // in case of upload
WriteTimeout: 5 * time.Hour, // in case of download
IdleTimeout: 120 * time.Second,
}
go func() {
h := certManager.HTTPHandler(nil)
log.Fatal(http.ListenAndServe(":"+strconv.Itoa(httpPort), h))
}()
log.Fatal(server.ListenAndServeTLS("", ""))
}