mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-07-06 18:20:15 +00:00
feat: init swagger
This commit is contained in:
@@ -6,17 +6,28 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
swaggerfiles "github.com/swaggo/files"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
"github.com/tinyauthapp/tinyauth/internal/controller"
|
||||
"github.com/tinyauthapp/tinyauth/internal/middleware"
|
||||
"github.com/tinyauthapp/tinyauth/internal/model"
|
||||
docs "github.com/tinyauthapp/tinyauth/internal/swagger"
|
||||
"go.uber.org/dig"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// @title Tinyauth API
|
||||
// @version development
|
||||
// @description Swagger documentation for Tinyauth's API.
|
||||
// @license.name AGPL-3.0
|
||||
// @license.url https://github.com/tinyauthapp/tinyauth/blob/main/LICENSE
|
||||
// @BasePath /api
|
||||
func (app *BootstrapApp) setupRouter() error {
|
||||
// we don't want gin debug mode
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
@@ -80,6 +91,12 @@ func (app *BootstrapApp) setupRouter() error {
|
||||
return fmt.Errorf("failed to provide api router group: %w", err)
|
||||
}
|
||||
|
||||
err = app.setupSwagger()
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to setup swagger: %w", err)
|
||||
}
|
||||
|
||||
controllerProvideFor := []any{
|
||||
controller.NewContextController,
|
||||
controller.NewOAuthController,
|
||||
@@ -125,6 +142,42 @@ func (app *BootstrapApp) setupRouter() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (app *BootstrapApp) setupSwagger() error {
|
||||
appUrl, err := url.Parse(app.runtime.AppURL)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse app url: %w", err)
|
||||
}
|
||||
|
||||
docs.SwaggerInfo.Host = appUrl.Host
|
||||
docs.SwaggerInfo.Schemes = []string{appUrl.Scheme}
|
||||
docs.SwaggerInfo.Version = model.Version
|
||||
|
||||
type swaggerInput struct {
|
||||
dig.In
|
||||
|
||||
RouterGroup *gin.RouterGroup `name:"mainRouterGroup"`
|
||||
}
|
||||
|
||||
err = app.dig.Invoke(func(i swaggerInput) {
|
||||
i.RouterGroup.Use(func(c *gin.Context) {
|
||||
if strings.TrimSuffix(c.Request.URL.Path, "/") == "/swagger" {
|
||||
c.Redirect(http.StatusFound, "/swagger/index.html")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
})
|
||||
i.RouterGroup.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to invoke swagger: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Top down
|
||||
// 1. Tailscale (if tailscale.listen)
|
||||
// 2. Unix socket (if server.socketPath)
|
||||
|
||||
@@ -44,7 +44,7 @@ func (m *UIMiddleware) Middleware() gin.HandlerFunc {
|
||||
path := strings.TrimPrefix(c.Request.URL.Path, "/")
|
||||
|
||||
switch strings.SplitN(path, "/", 2)[0] {
|
||||
case "api", "resources", ".well-known", "authorize":
|
||||
case "api", "resources", ".well-known", "authorize", "swagger":
|
||||
c.Next()
|
||||
return
|
||||
case "robots.txt":
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
// Package swagger Code generated by swaggo/swag. DO NOT EDIT
|
||||
package swagger
|
||||
|
||||
import "github.com/swaggo/swag"
|
||||
|
||||
const docTemplate = `{
|
||||
"schemes": {{ marshal .Schemes }},
|
||||
"swagger": "2.0",
|
||||
"info": {
|
||||
"description": "{{escape .Description}}",
|
||||
"title": "{{.Title}}",
|
||||
"contact": {},
|
||||
"license": {
|
||||
"name": "AGPL-3.0",
|
||||
"url": "https://github.com/tinyauthapp/tinyauth/blob/main/LICENSE"
|
||||
},
|
||||
"version": "{{.Version}}"
|
||||
},
|
||||
"host": "{{.Host}}",
|
||||
"basePath": "{{.BasePath}}",
|
||||
"paths": {}
|
||||
}`
|
||||
|
||||
// SwaggerInfo holds exported Swagger Info so clients can modify it
|
||||
var SwaggerInfo = &swag.Spec{
|
||||
Version: "development",
|
||||
Host: "",
|
||||
BasePath: "/api",
|
||||
Schemes: []string{},
|
||||
Title: "Tinyauth API",
|
||||
Description: "Swagger documentation for Tinyauth's API.",
|
||||
InfoInstanceName: "swagger",
|
||||
SwaggerTemplate: docTemplate,
|
||||
LeftDelim: "{{",
|
||||
RightDelim: "}}",
|
||||
}
|
||||
|
||||
func init() {
|
||||
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"swagger": "2.0",
|
||||
"info": {
|
||||
"description": "Swagger documentation for Tinyauth's API.",
|
||||
"title": "Tinyauth API",
|
||||
"contact": {},
|
||||
"license": {
|
||||
"name": "AGPL-3.0",
|
||||
"url": "https://github.com/tinyauthapp/tinyauth/blob/main/LICENSE"
|
||||
},
|
||||
"version": "development"
|
||||
},
|
||||
"basePath": "/api",
|
||||
"paths": {}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
basePath: /api
|
||||
info:
|
||||
contact: {}
|
||||
description: Swagger documentation for Tinyauth's API.
|
||||
license:
|
||||
name: AGPL-3.0
|
||||
url: https://github.com/tinyauthapp/tinyauth/blob/main/LICENSE
|
||||
title: Tinyauth API
|
||||
version: development
|
||||
paths: {}
|
||||
swagger: "2.0"
|
||||
Reference in New Issue
Block a user