Added pre-generated TLS certificate

This commit is contained in:
Pijus Kamandulis
2024-02-27 21:58:57 +02:00
parent 5ff923ce2c
commit 3aeae98404
6 changed files with 125 additions and 14 deletions

View File

@@ -1,7 +1,11 @@
package api
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/pikami/cosmium/api/config"
"github.com/pikami/cosmium/api/handlers"
"github.com/pikami/cosmium/api/handlers/middleware"
)
@@ -43,3 +47,39 @@ func CreateRouter() *gin.Engine {
return router
}
func StartAPI() {
router := CreateRouter()
listenAddress := fmt.Sprintf(":%d", config.Config.Port)
if config.Config.TLS_CertificatePath != "" && config.Config.TLS_CertificateKey != "" {
err := router.RunTLS(
listenAddress,
config.Config.TLS_CertificatePath,
config.Config.TLS_CertificateKey)
if err != nil {
fmt.Println("Failed to start HTTPS server:", err)
}
return
}
if config.Config.DisableTls {
router.Run(listenAddress)
}
tlsConfig := config.GetDefaultTlsConfig()
server := &http.Server{
Addr: listenAddress,
Handler: router.Handler(),
TLSConfig: tlsConfig,
}
fmt.Printf("Listening and serving HTTPS on %s\n", server.Addr)
err := server.ListenAndServeTLS("", "")
if err != nil {
fmt.Println("Failed to start HTTPS server:", err)
}
router.Run()
}