mirror of
				https://github.com/steveiliop56/tinyauth.git
				synced 2025-11-03 23:55:44 +00:00 
			
		
		
		
	Compare commits
	
		
			3 Commits
		
	
	
		
			refactor/c
			...
			a629430a88
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					a629430a88 | ||
| 
						 | 
					f0a48cc91c | ||
| 
						 | 
					2f8fa39a9b | 
@@ -45,8 +45,6 @@ FROM alpine:3.22 AS runner
 | 
			
		||||
 | 
			
		||||
WORKDIR /tinyauth
 | 
			
		||||
 | 
			
		||||
RUN apk add --no-cache curl
 | 
			
		||||
 | 
			
		||||
COPY --from=builder /tinyauth/tinyauth ./
 | 
			
		||||
 | 
			
		||||
EXPOSE 3000
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
<div align="center">
 | 
			
		||||
    <img alt="Tinyauth" title="Tinyauth" width="96" src="assets/logo-rounded.png">
 | 
			
		||||
    <h1>Tinyauth</h1>
 | 
			
		||||
    <p>The easiest way to secure your apps with a login screen.</p>
 | 
			
		||||
    <p>The simplest way to protect your apps with a login screen.</p>
 | 
			
		||||
</div>
 | 
			
		||||
 | 
			
		||||
<div align="center">
 | 
			
		||||
@@ -14,7 +14,7 @@
 | 
			
		||||
 | 
			
		||||
<br />
 | 
			
		||||
 | 
			
		||||
Tinyauth is a simple authentication middleware that adds a simple login screen or OAuth with Google, Github and any provider to all of your docker apps. It supports all the popular proxies like Traefik, Nginx and Caddy.
 | 
			
		||||
Tinyauth is a simple authentication middleware that adds a simple login screen or OAuth with Google, Github or any other provider to all of your apps. It supports all the popular proxies like Traefik, Nginx and Caddy.
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										109
									
								
								cmd/health.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								cmd/health.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,109 @@
 | 
			
		||||
package cmd
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"tinyauth/internal/config"
 | 
			
		||||
 | 
			
		||||
	"github.com/rs/zerolog"
 | 
			
		||||
	"github.com/rs/zerolog/log"
 | 
			
		||||
	"github.com/spf13/cobra"
 | 
			
		||||
	"github.com/spf13/viper"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type healthzResponse struct {
 | 
			
		||||
	Status  string `json:"status"`
 | 
			
		||||
	Message string `json:"message"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type healthCmd struct {
 | 
			
		||||
	root *cobra.Command
 | 
			
		||||
	cmd  *cobra.Command
 | 
			
		||||
 | 
			
		||||
	viper  *viper.Viper
 | 
			
		||||
	appUrl string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func newHealthCmd(root *cobra.Command) *healthCmd {
 | 
			
		||||
	return &healthCmd{
 | 
			
		||||
		root:  root,
 | 
			
		||||
		viper: viper.New(),
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *healthCmd) Register() {
 | 
			
		||||
	c.cmd = &cobra.Command{
 | 
			
		||||
		Use:   "health",
 | 
			
		||||
		Short: "Health check",
 | 
			
		||||
		Long:  `Use the health check endpoint to verify that Tinyauth is running and it's healthy.`,
 | 
			
		||||
		Run:   c.run,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	c.viper.AutomaticEnv()
 | 
			
		||||
	c.cmd.Flags().StringVar(&c.appUrl, "app-url", "http://localhost:3000", "The URL where the Tinyauth server is running on.")
 | 
			
		||||
	c.viper.BindEnv("app-url", "APP_URL")
 | 
			
		||||
	c.viper.BindPFlags(c.cmd.Flags())
 | 
			
		||||
 | 
			
		||||
	if c.root != nil {
 | 
			
		||||
		c.root.AddCommand(c.cmd)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *healthCmd) GetCmd() *cobra.Command {
 | 
			
		||||
	return c.cmd
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (c *healthCmd) run(cmd *cobra.Command, args []string) {
 | 
			
		||||
	log.Logger = log.Level(zerolog.InfoLevel)
 | 
			
		||||
 | 
			
		||||
	appUrl := c.viper.GetString("app-url")
 | 
			
		||||
 | 
			
		||||
	if appUrl == "" {
 | 
			
		||||
		log.Fatal().Err(errors.New("app-url is required")).Msg("App URL is required")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if config.Version == "development" {
 | 
			
		||||
		log.Warn().Msg("Running in development mode. Overriding the app-url to http://localhost:3000")
 | 
			
		||||
		appUrl = "http://localhost:3000"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Info().Msgf("Health check endpoint is available at %s/api/healthz", appUrl)
 | 
			
		||||
 | 
			
		||||
	client := http.Client{}
 | 
			
		||||
 | 
			
		||||
	req, err := http.NewRequest("GET", appUrl+"/api/healthz", nil)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal().Err(err).Msg("Failed to create request")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	resp, err := client.Do(req)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal().Err(err).Msg("Failed to perform request")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if resp.StatusCode != http.StatusOK {
 | 
			
		||||
		log.Fatal().Err(errors.New("service is not healthy")).Msgf("Service is not healthy. Status code: %d", resp.StatusCode)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defer resp.Body.Close()
 | 
			
		||||
 | 
			
		||||
	var healthResp healthzResponse
 | 
			
		||||
 | 
			
		||||
	body, err := io.ReadAll(resp.Body)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal().Err(err).Msg("Failed to read response")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = json.Unmarshal(body, &healthResp)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal().Err(err).Msg("Failed to decode response")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	log.Info().Interface("response", healthResp).Msg("Service is healthy")
 | 
			
		||||
}
 | 
			
		||||
@@ -140,6 +140,7 @@ func Run() {
 | 
			
		||||
	newVerifyUserCmd(userCmd).Register()
 | 
			
		||||
	newGenerateTotpCmd(totpCmd).Register()
 | 
			
		||||
	newVersionCmd(root).Register()
 | 
			
		||||
	newHealthCmd(root).Register()
 | 
			
		||||
 | 
			
		||||
	root.AddCommand(userCmd)
 | 
			
		||||
	root.AddCommand(totpCmd)
 | 
			
		||||
 
 | 
			
		||||
@@ -13,8 +13,8 @@ func NewHealthController(router *gin.RouterGroup) *HealthController {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (controller *HealthController) SetupRoutes() {
 | 
			
		||||
	controller.router.GET("/health", controller.healthHandler)
 | 
			
		||||
	controller.router.HEAD("/health", controller.healthHandler)
 | 
			
		||||
	controller.router.GET("/healthz", controller.healthHandler)
 | 
			
		||||
	controller.router.HEAD("/healthz", controller.healthHandler)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (controller *HealthController) healthHandler(c *gin.Context) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user