Newer
Older
OktaOauth / main.go
@Yangyang Xie Yangyang Xie on 12 Jun 2022 1 KB first commit
package main

import (
	"fmt"
	"net/http"
	"os"

	"github.com/gin-gonic/gin"
)

var (
	clientID      string
	clientSercert string
	orgURL        string
	kubeCA        string
	kubeAPIServer string
)

func addHtmlTemplates(r *gin.Engine) {
	r.LoadHTMLGlob("templates/**/*")

	// Add health check page
	r.GET("/health", func(ctx *gin.Context) {
		ctx.String(http.StatusOK, "healthy")
	})

	// Add login page.
	r.GET("/authentication/login", func(ctx *gin.Context) {
		ctx.HTML(http.StatusOK, "authentication/login.tmpl", gin.H{
			"orgURL":        orgURL,
			"clientID":      clientID,
			"kubeCA":        kubeCA,
			"kubeAPIServer": kubeAPIServer,
		})
	})
}

func loadEnvironments() error {
	var found bool
	if clientID, found = os.LookupEnv("CLIENT_ID"); found != true || clientID == "" {
		return fmt.Errorf("Missing env CLIENT_ID")
	}

	if clientSercert, found = os.LookupEnv("CLIENT_SECRET"); found != true || clientSercert == "" {
		return fmt.Errorf("Missing env CLIENT_SECRET")
	}

	if orgURL, found = os.LookupEnv("ORG_URL"); found != true || orgURL == "" {
		return fmt.Errorf("Missing env ORG_URL")
	}

	if kubeAPIServer, found = os.LookupEnv("KUBE_APISERVER"); found != true || kubeAPIServer == "" {
		return fmt.Errorf("Missing env KUBE_APISERVER")
	}

	if kubeCA, found = os.LookupEnv("KUBE_CA"); found != true || kubeCA == "" {
		return fmt.Errorf("Missing env KUBE_CA")
	}
	return nil
}

func main() {
	if err := loadEnvironments(); err != nil {
		fmt.Printf(err.Error())
		return
	}

	router := gin.Default()
	addHtmlTemplates(router)
	server := http.Server{
		Addr:    "0.0.0.0:8080",
		Handler: router.Handler(),
	}

	server.ListenAndServe()
}