Skip to content

Commit

Permalink
apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
eternal-flame-AD committed Jul 15, 2023
1 parent 2f17617 commit 1f65c82
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
14 changes: 5 additions & 9 deletions docs/swagger.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
package docs

import (
"embed"
_ "embed"
"strings"

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

//go:embed spec.json
var box embed.FS
var spec string

// Serve serves the documentation.
func Serve(ctx *gin.Context) {
base := location.Get(ctx).Host
if basePathFromQuery := ctx.Query("base"); basePathFromQuery != "" {
base = basePathFromQuery
}
ctx.Writer.WriteString(get(base))
ctx.Writer.WriteString(getSwaggerJSON(base))
}

func get(base string) string {
spec, err := box.ReadFile("spec.json")
if err != nil {
panic(err)
}
return strings.Replace(string(spec), "localhost", base, 1)
func getSwaggerJSON(base string) string {
return strings.Replace(spec, "localhost", base, 1)
}
21 changes: 7 additions & 14 deletions ui/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ui
import (
"embed"
"encoding/json"
"io/fs"
"net/http"
"strings"

Expand All @@ -19,16 +20,6 @@ type uiConfig struct {
Version model.VersionInfo `json:"version"`
}

type AddPrefix struct {
Prefix string
InnerHandler http.Handler
}

func (a *AddPrefix) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.URL.Path = a.Prefix + r.URL.Path
a.InnerHandler.ServeHTTP(w, r)
}

// Register registers the ui on the root path.
func Register(r *gin.Engine, version model.VersionInfo, register bool) {
uiConfigBytes, err := json.Marshal(uiConfig{Version: version, Register: register})
Expand All @@ -42,10 +33,12 @@ func Register(r *gin.Engine, version model.VersionInfo, register bool) {
ui.GET("/index.html", serveFile("index.html", "text/html", noop))
ui.GET("/manifest.json", serveFile("manifest.json", "application/json", noop))
ui.GET("/asset-manifest.json", serveFile("asset-manifest.json", "application/json", noop))
ui.GET("/static/*any", gin.WrapH(&AddPrefix{
Prefix: "/build",
InnerHandler: http.FileServer(http.FS(box)),
}))

subBox, err := fs.Sub(box, "build")
if err != nil {
panic(err)
}
ui.GET("/static/*any", gin.WrapH(http.FileServer(http.FS(subBox))))
}

func noop(s string) string {
Expand Down

0 comments on commit 1f65c82

Please sign in to comment.