mirror of
				https://github.com/steveiliop56/tinyauth.git
				synced 2025-11-04 08:05:42 +00:00 
			
		
		
		
	Compare commits
	
		
			4 Commits
		
	
	
		
			402e7e565d
			...
			refactor/c
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					e1dcbda4ec | ||
| 
						 | 
					fa62ba2e33 | ||
| 
						 | 
					04b075c748 | ||
| 
						 | 
					74a360369d | 
@@ -45,6 +45,8 @@ FROM alpine:3.22 AS runner
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
WORKDIR /tinyauth
 | 
					WORKDIR /tinyauth
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					RUN apk add --no-cache curl
 | 
				
			||||||
 | 
					
 | 
				
			||||||
COPY --from=builder /tinyauth/tinyauth ./
 | 
					COPY --from=builder /tinyauth/tinyauth ./
 | 
				
			||||||
 | 
					
 | 
				
			||||||
EXPOSE 3000
 | 
					EXPOSE 3000
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,7 @@
 | 
				
			|||||||
<div align="center">
 | 
					<div align="center">
 | 
				
			||||||
    <img alt="Tinyauth" title="Tinyauth" width="96" src="assets/logo-rounded.png">
 | 
					    <img alt="Tinyauth" title="Tinyauth" width="96" src="assets/logo-rounded.png">
 | 
				
			||||||
    <h1>Tinyauth</h1>
 | 
					    <h1>Tinyauth</h1>
 | 
				
			||||||
    <p>The simplest way to protect your apps with a login screen.</p>
 | 
					    <p>The easiest way to secure your apps with a login screen.</p>
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<div align="center">
 | 
					<div align="center">
 | 
				
			||||||
@@ -14,7 +14,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
<br />
 | 
					<br />
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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.
 | 
					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.
 | 
				
			||||||
 | 
					
 | 
				
			||||||

 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										109
									
								
								cmd/health.go
									
									
									
									
									
								
							
							
						
						
									
										109
									
								
								cmd/health.go
									
									
									
									
									
								
							@@ -1,109 +0,0 @@
 | 
				
			|||||||
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,7 +140,6 @@ func Run() {
 | 
				
			|||||||
	newVerifyUserCmd(userCmd).Register()
 | 
						newVerifyUserCmd(userCmd).Register()
 | 
				
			||||||
	newGenerateTotpCmd(totpCmd).Register()
 | 
						newGenerateTotpCmd(totpCmd).Register()
 | 
				
			||||||
	newVersionCmd(root).Register()
 | 
						newVersionCmd(root).Register()
 | 
				
			||||||
	newHealthCmd(root).Register()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	root.AddCommand(userCmd)
 | 
						root.AddCommand(userCmd)
 | 
				
			||||||
	root.AddCommand(totpCmd)
 | 
						root.AddCommand(totpCmd)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										4
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								go.mod
									
									
									
									
									
								
							@@ -8,7 +8,7 @@ require (
 | 
				
			|||||||
	github.com/cenkalti/backoff/v5 v5.0.3
 | 
						github.com/cenkalti/backoff/v5 v5.0.3
 | 
				
			||||||
	github.com/gin-gonic/gin v1.11.0
 | 
						github.com/gin-gonic/gin v1.11.0
 | 
				
			||||||
	github.com/glebarez/sqlite v1.11.0
 | 
						github.com/glebarez/sqlite v1.11.0
 | 
				
			||||||
	github.com/go-playground/validator/v10 v10.28.0
 | 
						github.com/go-playground/validator/v10 v10.27.0
 | 
				
			||||||
	github.com/golang-migrate/migrate/v4 v4.19.0
 | 
						github.com/golang-migrate/migrate/v4 v4.19.0
 | 
				
			||||||
	github.com/google/go-querystring v1.1.0
 | 
						github.com/google/go-querystring v1.1.0
 | 
				
			||||||
	github.com/google/uuid v1.6.0
 | 
						github.com/google/uuid v1.6.0
 | 
				
			||||||
@@ -87,7 +87,7 @@ require (
 | 
				
			|||||||
	github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
 | 
						github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
 | 
				
			||||||
	github.com/felixge/httpsnoop v1.0.4 // indirect
 | 
						github.com/felixge/httpsnoop v1.0.4 // indirect
 | 
				
			||||||
	github.com/fsnotify/fsnotify v1.9.0 // indirect
 | 
						github.com/fsnotify/fsnotify v1.9.0 // indirect
 | 
				
			||||||
	github.com/gabriel-vasile/mimetype v1.4.10 // indirect
 | 
						github.com/gabriel-vasile/mimetype v1.4.8 // indirect
 | 
				
			||||||
	github.com/gin-contrib/sse v1.1.0 // indirect
 | 
						github.com/gin-contrib/sse v1.1.0 // indirect
 | 
				
			||||||
	github.com/go-ldap/ldap/v3 v3.4.12
 | 
						github.com/go-ldap/ldap/v3 v3.4.12
 | 
				
			||||||
	github.com/go-logr/logr v1.4.3 // indirect
 | 
						github.com/go-logr/logr v1.4.3 // indirect
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										8
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								go.sum
									
									
									
									
									
								
							@@ -88,8 +88,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
 | 
				
			|||||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
 | 
					github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
 | 
				
			||||||
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
 | 
					github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
 | 
				
			||||||
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
 | 
					github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
 | 
				
			||||||
github.com/gabriel-vasile/mimetype v1.4.10 h1:zyueNbySn/z8mJZHLt6IPw0KoZsiQNszIpU+bX4+ZK0=
 | 
					github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
 | 
				
			||||||
github.com/gabriel-vasile/mimetype v1.4.10/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
 | 
					github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
 | 
				
			||||||
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
 | 
					github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
 | 
				
			||||||
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
 | 
					github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
 | 
				
			||||||
github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk=
 | 
					github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk=
 | 
				
			||||||
@@ -113,8 +113,8 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
 | 
				
			|||||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
 | 
					github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
 | 
				
			||||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
 | 
					github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
 | 
				
			||||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
 | 
					github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
 | 
				
			||||||
github.com/go-playground/validator/v10 v10.28.0 h1:Q7ibns33JjyW48gHkuFT91qX48KG0ktULL6FgHdG688=
 | 
					github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
 | 
				
			||||||
github.com/go-playground/validator/v10 v10.28.0/go.mod h1:GoI6I1SjPBh9p7ykNE/yj3fFYbyDOpwMn5KXd+m2hUU=
 | 
					github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
 | 
				
			||||||
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
 | 
					github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
 | 
				
			||||||
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
 | 
					github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
 | 
				
			||||||
github.com/goccy/go-json v0.10.4 h1:JSwxQzIqKfmFX1swYPpUThQZp/Ka4wzJdK0LWVytLPM=
 | 
					github.com/goccy/go-json v0.10.4 h1:JSwxQzIqKfmFX1swYPpUThQZp/Ka4wzJdK0LWVytLPM=
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,8 +13,8 @@ func NewHealthController(router *gin.RouterGroup) *HealthController {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (controller *HealthController) SetupRoutes() {
 | 
					func (controller *HealthController) SetupRoutes() {
 | 
				
			||||||
	controller.router.GET("/healthz", controller.healthHandler)
 | 
						controller.router.GET("/health", controller.healthHandler)
 | 
				
			||||||
	controller.router.HEAD("/healthz", controller.healthHandler)
 | 
						controller.router.HEAD("/health", controller.healthHandler)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (controller *HealthController) healthHandler(c *gin.Context) {
 | 
					func (controller *HealthController) healthHandler(c *gin.Context) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user