From 3c304063bf5b83eb7f690300861cc3eaf260671c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20PAILHAREY?= <rpailharey@grandlyon.com>
Date: Wed, 11 Aug 2021 08:43:50 +0000
Subject: [PATCH] Handling CORS

---
 Dockerfile                          |  4 +-
 go.mod                              |  5 ++-
 go.sum                              | 13 -------
 internal/auth/auth.go               |  5 ---
 internal/auth/oauth2.go             |  3 +-
 internal/database/database.go       |  2 -
 internal/mocks/mocks.go             | 24 +-----------
 internal/monthlyNews/monthlyNews.go |  4 --
 internal/poll/poll.go               |  4 --
 internal/rootmux/rootmux_test.go    |  5 +--
 internal/tokens/tokens.go           |  2 +-
 main.go                             | 59 +++++------------------------
 12 files changed, 19 insertions(+), 111 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index a15d15a..2bf8a31 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -33,8 +33,8 @@ RUN chown -Rf "${UID}" ./*
 
 # Get dependencies and run tests
 RUN go version
-RUN go get -d -v && \
-    CGO_ENABLED=1 go test ./...
+RUN go get -d -v
+RUN CGO_ENABLED=1 go test ./...
 
 # Build the binary
 RUN CGO_ENABLED=1 go build \
diff --git a/go.mod b/go.mod
index 807d8bc..002b1e8 100644
--- a/go.mod
+++ b/go.mod
@@ -4,12 +4,13 @@ go 1.15
 
 require (
 	github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
+	github.com/golang/protobuf v1.5.2 // indirect
 	github.com/gorilla/mux v1.8.0
-	github.com/nicolaspernoud/vestibule v0.0.0-20210626100803-e2554e116746
 	github.com/swaggo/http-swagger v1.0.0
 	github.com/swaggo/swag v1.7.0
-	golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97
+	golang.org/x/net v0.0.0-20210525063256-abc453219eb5 // indirect
 	golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914
+	google.golang.org/appengine v1.6.7 // indirect
 	gorm.io/driver/mysql v1.1.1
 	gorm.io/driver/sqlite v1.1.4
 	gorm.io/gorm v1.21.12
diff --git a/go.sum b/go.sum
index 6789aac..c9047ca 100644
--- a/go.sum
+++ b/go.sum
@@ -154,17 +154,13 @@ github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA
 github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
 github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KKTQ=
 github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
-github.com/nicolaspernoud/vestibule v0.0.0-20210626100803-e2554e116746 h1:mMpAetOOm54X87qjKq+RiSNutdULgFWp1knqhUeYf4s=
-github.com/nicolaspernoud/vestibule v0.0.0-20210626100803-e2554e116746/go.mod h1:zQIZ4A7ZYJBcS/DBZpMadr5N8WrATlj7267VlvKSX88=
 github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
 github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
-github.com/oschwald/maxminddb-golang v1.8.0/go.mod h1:RXZtst0N6+FY/3qCNmZMBApR19cdQj43/NM9VkrNAis=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
 github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
 github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
-github.com/secure-io/sio-go v0.3.1/go.mod h1:+xbkjDzPjwh4Axd07pRKSNriS9SCiYksWnZqdnfpQxs=
 github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
@@ -191,11 +187,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
 golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
 golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
 golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
-golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
-golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
-golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
-golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
 golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
 golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
 golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -257,7 +249,6 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R
 golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
 golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
 golang.org/x/net v0.0.0-20201207224615-747e23833adb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
-golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
 golang.org/x/net v0.0.0-20210525063256-abc453219eb5 h1:wjuX4b5yYQnEQHzd+CBcrcC6OVR2J1CN6mUy0oSxIPo=
 golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
 golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@@ -265,7 +256,6 @@ golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4Iltr
 golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
 golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
 golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
-golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
 golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914 h1:3B43BWw0xEBsLZ/NO1VALz6fppU3481pik+2Ksv45z8=
 golang.org/x/oauth2 v0.0.0-20210628180205-a41e5a781914/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
 golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -288,7 +278,6 @@ golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod h1:h1NjWce9XRLGQEsW7w
 golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@@ -306,8 +295,6 @@ golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7w
 golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20210608053332-aa57babbf139/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
diff --git a/internal/auth/auth.go b/internal/auth/auth.go
index 2659865..fd469bd 100644
--- a/internal/auth/auth.go
+++ b/internal/auth/auth.go
@@ -76,11 +76,6 @@ func ValidateAuthMiddleware(next http.Handler, allowedRoles []string, checkXSRF
 			if perr == nil {
 				redirectTo += ":" + port
 			}
-			// Write the requested url in a cookie
-			if r.Host != redirectTo && r.URL.Path != "/favicon.ico" {
-				cookie := http.Cookie{Name: "redirectAfterLogin", Path: "/", Domain: hostname, Value: r.Host + r.URL.Path + "?" + r.URL.RawQuery, MaxAge: 30, Secure: true, HttpOnly: false, SameSite: http.SameSiteLaxMode}
-				http.SetCookie(w, &cookie)
-			}
 			w.Header().Set("Content-Type", "text/html")
 			w.WriteHeader(http.StatusUnauthorized)
 			responseContent := fmt.Sprintf("error extracting token: %v<meta http-equiv=\"Refresh\" content=\"0; url=https://%v#login\"/>", err.Error(), redirectTo)
diff --git a/internal/auth/oauth2.go b/internal/auth/oauth2.go
index d757d76..d7b5aff 100644
--- a/internal/auth/oauth2.go
+++ b/internal/auth/oauth2.go
@@ -137,8 +137,7 @@ func (m Manager) HandleOAuth2Callback() http.Handler {
 		// Log the connexion
 		log.Printf("| %v (%v %v) | Login success | %v", user.Login, user.Name, user.Surname, req.RemoteAddr)
 		// Redirect
-		w.WriteHeader(http.StatusOK)
-		w.Write([]byte("successful login"))
+		http.Redirect(w, r, "/", http.StatusFound)
 	}
 	return http.HandlerFunc(oauth2Handler)
 }
diff --git a/internal/database/database.go b/internal/database/database.go
index c435e9a..f8d2b00 100644
--- a/internal/database/database.go
+++ b/internal/database/database.go
@@ -2,7 +2,6 @@ package database
 
 import (
 	"fmt"
-	"log"
 
 	"forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/common"
 	"gorm.io/driver/mysql"
@@ -37,7 +36,6 @@ func init() {
 		}
 	} else {
 		dsn := fmt.Sprintf("%v:%v@tcp(%v:3306)/%v?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPassword, dbHost, dbName)
-		log.Printf("Data Source Name : %s", dsn)
 		db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
 		if err != nil {
 			panic("failed to connect database")
diff --git a/internal/mocks/mocks.go b/internal/mocks/mocks.go
index 6a597f6..f25b70c 100644
--- a/internal/mocks/mocks.go
+++ b/internal/mocks/mocks.go
@@ -4,14 +4,10 @@ package mocks
 import (
 	"fmt"
 	"net/http"
-
-	"forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/common"
-	"github.com/nicolaspernoud/vestibule/pkg/middlewares"
 )
 
 var (
-	hostname = common.StringValueFromEnv("HOSTNAME", "ecolyobackoffice.127.0.0.1.nip.io")
-	port     int
+	port int
 )
 
 // Init initialize the configuration
@@ -73,21 +69,3 @@ func CreateMockOAuth2() *http.ServeMux {
 
 	return mux
 }
-
-// CreateMockAPI creates a mock OAuth2 serve mux for development purposes
-func CreateMockAPI() *http.ServeMux {
-	mux := http.NewServeMux()
-	// Returns authorization code back to the user
-	mux.Handle("/", middlewares.Cors(func() http.Handler {
-		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-			w.Header().Set("Content-Type", "application/json")
-			w.Header().Set("X-XSS-Protection", "1; mode=block")
-			w.Header().Set("Content-Security-Policy", "default-src 'self'; frame-ancestors http://www.example.com")
-			w.Write([]byte(`{
-				"foo": "bar",
-				"bar": "foo"
-			}`))
-		})
-	}(), hostname, port))
-	return mux
-}
diff --git a/internal/monthlyNews/monthlyNews.go b/internal/monthlyNews/monthlyNews.go
index 9d5d986..cff7ac5 100644
--- a/internal/monthlyNews/monthlyNews.go
+++ b/internal/monthlyNews/monthlyNews.go
@@ -103,8 +103,6 @@ func GetSingleMonthlyNews(w http.ResponseWriter, r *http.Request) {
 // @Param monthlyNews body MonthlyNews true "MonthlyNews to create"
 // @Router /api/admin/monthlyNews [post]
 func AddMonthlyNews(w http.ResponseWriter, r *http.Request) {
-	w.Header().Set("Access-Control-Allow-Origin", "*")
-
 	if r.Body == http.NoBody {
 		http.Error(w, "request body is empty", http.StatusBadRequest)
 		return
@@ -147,8 +145,6 @@ func AddMonthlyNews(w http.ResponseWriter, r *http.Request) {
 // @Param monthlyNews body MonthlyNews true "MonthlyNews to create/update with new content"
 // @Router /api/admin/monthlyNews [put]
 func UpdateMonthlyNews(w http.ResponseWriter, r *http.Request) {
-	w.Header().Set("Access-Control-Allow-Origin", "*")
-
 	if r.Body == http.NoBody {
 		http.Error(w, "request body is empty", http.StatusBadRequest)
 		return
diff --git a/internal/poll/poll.go b/internal/poll/poll.go
index 28907e5..dc31b45 100644
--- a/internal/poll/poll.go
+++ b/internal/poll/poll.go
@@ -103,8 +103,6 @@ func GetSinglePoll(w http.ResponseWriter, r *http.Request) {
 // @Param poll body Poll true "Poll to create"
 // @Router /api/admin/poll [post]
 func AddPoll(w http.ResponseWriter, r *http.Request) {
-	w.Header().Set("Access-Control-Allow-Origin", "*")
-
 	if r.Body == http.NoBody {
 		http.Error(w, "request body is empty", http.StatusBadRequest)
 		return
@@ -147,8 +145,6 @@ func AddPoll(w http.ResponseWriter, r *http.Request) {
 // @Param poll body Poll true "Poll to update with new content"
 // @Router /api/admin/poll [put]
 func UpdatePoll(w http.ResponseWriter, r *http.Request) {
-	w.Header().Set("Access-Control-Allow-Origin", "*")
-
 	if r.Body == http.NoBody {
 		http.Error(w, "request body is empty", http.StatusBadRequest)
 		return
diff --git a/internal/rootmux/rootmux_test.go b/internal/rootmux/rootmux_test.go
index 1a4664b..44ee1eb 100644
--- a/internal/rootmux/rootmux_test.go
+++ b/internal/rootmux/rootmux_test.go
@@ -34,9 +34,6 @@ func TestMain(m *testing.M) {
 	oAuth2Server = httptest.NewServer(mocks.CreateMockOAuth2())
 	defer oAuth2Server.Close()
 
-	// Create the API mock server
-	go http.ListenAndServe(":8091", mocks.CreateMockAPI())
-
 	// Set the constants with environment variables
 	os.Setenv("HOSTNAME", "localhost")
 	os.Setenv("ADMIN_ROLE", "ADMINS")
@@ -160,7 +157,7 @@ func adminTests(t *testing.T) {
 		do("GET", "/api/admin/poll/2020/0", xsrfHeader, "", http.StatusNotFound, "")
 	}
 	// Try to login (must pass)
-	do("GET", "/OAuth2Login", noH, "", http.StatusOK, "successful login")
+	do("GET", "/OAuth2Login", noH, "", http.StatusOK, "")
 	// Run the tests
 	tests()
 	// Try to logout (must pass)
diff --git a/internal/tokens/tokens.go b/internal/tokens/tokens.go
index c22225f..287e925 100644
--- a/internal/tokens/tokens.go
+++ b/internal/tokens/tokens.go
@@ -75,7 +75,7 @@ func CreateCookie(data interface{}, hostName string, cookieName string, duration
 		http.Error(w, err.Error(), http.StatusInternalServerError)
 		return
 	}
-	cookie := http.Cookie{Name: cookieName, Domain: hostName, Value: value, Expires: expiration, Secure: !m.debugMode, HttpOnly: true, SameSite: http.SameSiteLaxMode}
+	cookie := http.Cookie{Name: cookieName, Domain: hostName, Value: value, Expires: expiration, Secure: !m.debugMode, HttpOnly: true, SameSite: http.SameSiteStrictMode}
 	http.SetCookie(w, &cookie)
 }
 
diff --git a/main.go b/main.go
index c2377d2..1458db1 100644
--- a/main.go
+++ b/main.go
@@ -1,11 +1,9 @@
 package main
 
 import (
-	"crypto/tls"
 	"fmt"
 	"net/http"
 	"strconv"
-	"time"
 
 	"log"
 
@@ -13,22 +11,13 @@ import (
 	"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
+	httpsPort = common.IntValueFromEnv("HTTPS_PORT", 443)    // HTTPS port to serve on
+	debugMode = common.BoolValueFromEnv("DEBUG_MODE", false) // Debug mode, disable Let's Encrypt, enable CORS and more logging
 )
 
-func init() {
-	if debugMode {
-	}
-}
-
 func main() {
 
 	log.Println("--- Server is starting ---")
@@ -39,42 +28,14 @@ func main() {
 	// 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),
-		}
+	// Init the hostname
+	mocks.Init(httpsPort)
 
-		server := &http.Server{
-			Addr:    ":" + strconv.Itoa(httpsPort),
-			Handler: rootMux.Router,
-			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,
-		}
+	// 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)
 
-		go func() {
-			h := certManager.HTTPHandler(nil)
-			log.Fatal(http.ListenAndServe(":"+strconv.Itoa(httpPort), h))
-		}()
-
-		log.Fatal(server.ListenAndServeTLS("", ""))
-	}
+	// Serve locally with https on debug mode or with let's encrypt on production mode
+	log.Fatal(http.ListenAndServeTLS(":"+strconv.Itoa(httpsPort), "./dev_certificates/localhost.crt", "./dev_certificates/localhost.key", rootMux.Router))
 }
-- 
GitLab