-
-
Notifications
You must be signed in to change notification settings - Fork 589
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inject register & version information into index.html
The registration form will always be shown inside the dev mode, because there is no api that transmits if registration is enabled.
- Loading branch information
Showing
8 changed files
with
67 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,51 @@ | ||
package ui | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/gin-contrib/gzip" | ||
"github.com/gin-gonic/gin" | ||
"github.com/gobuffalo/packr/v2" | ||
"github.com/gotify/server/v2/model" | ||
) | ||
|
||
var box = packr.New("ui", "../ui/build") | ||
|
||
type uiConfig struct { | ||
Register bool `json:"register"` | ||
Version model.VersionInfo `json:"version"` | ||
} | ||
|
||
// Register registers the ui on the root path. | ||
func Register(r *gin.Engine) { | ||
func Register(r *gin.Engine, version model.VersionInfo, register bool) { | ||
uiConfigBytes, err := json.Marshal(uiConfig{Version: version, Register: register}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
ui := r.Group("/", gzip.Gzip(gzip.DefaultCompression)) | ||
ui.GET("/", serveFile("index.html", "text/html")) | ||
ui.GET("/index.html", serveFile("index.html", "text/html")) | ||
ui.GET("/manifest.json", serveFile("manifest.json", "application/json")) | ||
ui.GET("/assets-manifest.json", serveFile("asserts-manifest.json", "application/json")) | ||
ui.GET("/", serveFile("index.html", "text/html", func(content string) string { | ||
return strings.Replace(content, "%CONFIG%", string(uiConfigBytes), 1) | ||
})) | ||
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(http.FileServer(box))) | ||
} | ||
|
||
func serveFile(name, contentType string) gin.HandlerFunc { | ||
func noop(s string) string { | ||
return s | ||
} | ||
|
||
func serveFile(name, contentType string, convert func(string) string) gin.HandlerFunc { | ||
content, err := box.FindString(name) | ||
if err != nil { | ||
panic(err) | ||
} | ||
converted := convert(content) | ||
return func(ctx *gin.Context) { | ||
ctx.Header("Content-Type", contentType) | ||
content, err := box.FindString(name) | ||
if err != nil { | ||
panic(err) | ||
} | ||
ctx.String(200, content) | ||
ctx.String(200, converted) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,29 @@ | ||
import {IVersion} from './types'; | ||
|
||
export interface IConfig { | ||
url: string; | ||
register: boolean; | ||
version: IVersion; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
declare global { | ||
interface Window { | ||
config?: Partial<IConfig>; | ||
} | ||
} | ||
|
||
let config: IConfig; | ||
const config: IConfig = { | ||
url: 'unset', | ||
register: false, | ||
version: {commit: 'unknown', buildDate: 'unknown', version: 'unknown'}, | ||
...window.config, | ||
}; | ||
|
||
export function set(c: IConfig) { | ||
config = c; | ||
export function set<Key extends keyof IConfig>(key: Key, value: IConfig[Key]): void { | ||
config[key] = value; | ||
} | ||
|
||
export function get(val: 'url'): string { | ||
return config[val]; | ||
export function get<K extends keyof IConfig>(key: K): IConfig[K] { | ||
return config[key]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters