ui: generate query tokens URL, remove JS

This commit is contained in:
Alexander Baryshnikov 2020-12-08 18:16:20 +08:00
parent fc36ee6064
commit d031b5ff3b
2 changed files with 36 additions and 5 deletions

View File

@ -1,6 +1,8 @@
package ui
import (
"net/http"
"net/url"
"strings"
"github.com/gin-gonic/gin"
@ -10,6 +12,7 @@ type baseResponse struct {
Context *gin.Context
Login string
Authorized bool
BaseURL string
}
func (br baseResponse) Rel(path string, tail ...string) string {
@ -30,5 +33,21 @@ func base(gctx *gin.Context) baseResponse {
Authorized: gctx.GetBool(ctxAuthorized),
Context: gctx,
Login: gctx.GetString(ctxLogin),
BaseURL: getProto(gctx.Request) + "://" + gctx.Request.Host,
}
}
func getProto(req *http.Request) string {
return extractScheme(req.Header.Get("Origin"), extractScheme(req.Header.Get("Referer"), "https"))
}
func extractScheme(guess, fallback string) string {
if guess == "" {
return fallback
}
u, err := url.Parse(guess)
if err != nil {
return fallback
}
return u.Scheme
}

View File

@ -57,8 +57,7 @@
<dt class="col-sm-3">API endpoint</dt>
<dd class="col-sm-9">
<code>
<noscript>JS required to detect API url</noscript>
<script>document.write((new URL("../../../api/{{.Unit.Name}}/", window.location).href))</script>
{{$.BaseURL}}/api/{{.Unit.Name}}/
</code>
</dd>
{{- end}}
@ -154,9 +153,22 @@
{{end}}
{{if .QueryToken.Enable}}
<dt class="col-sm-3">Query token</dt>
<dd class="col-sm-9">in
<code>{{.QueryToken.GetParam}}</code>
param
<dd class="col-sm-9">
<details>
<summary>
in
<code>{{.QueryToken.GetParam}}</code>
param
</summary>
<ul>
{{range .QueryToken.Tokens}}
<li>
<code>{{$.BaseURL}}/api/{{$.Unit.Name}}/?{{$.Unit.Authorization.QueryToken.GetParam}}={{.}}</code>
</li>
{{end}}
</ul>
</details>
</dd>
{{end}}
{{if .HeaderToken.Enable}}