Skip to content
This repository was archived by the owner on Mar 16, 2021. It is now read-only.

Commit 5c005ae

Browse files
billimekmxschmitt
authored andcommitted
configurable app DisplayURL for the UI (#121)
* changes to enable custom 'display' URL in app * making example.yml use an empty string for DisplayURL
1 parent eea3417 commit 5c005ae

File tree

7 files changed

+40
-13
lines changed

7 files changed

+40
-13
lines changed

config/example.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
ListenAddr: ':8080' # Consists of 'IP:Port', e.g. ':8080' listens on any IP and on Port 8080
2-
BaseURL: 'http://localhost:3000' # Origin URL, required for the authentication via OAuth
2+
BaseURL: 'http://localhost:3000' # Origin URL, required for the authentication via OAuth callback
3+
DisplayURL: '' # (OPTIONAL) Display URL, how the apication will present itself in the UI - if not set, defaults to BaseURL
34
Backend: boltdb # Can be 'boltdb' or 'redis'
45
DataDir: ./data # Contains: the database and the private key
56
EnableDebugMode: true # Activates more detailed logging

internal/handlers/handlers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func (h *Handler) setHandlers() error {
163163
h.engine.GET("/api/v1/info", h.handleInfo)
164164
h.engine.GET("/d/:id/:hash", h.handleDelete)
165165
h.engine.GET("/ok", h.handleHealthcheck)
166+
h.engine.GET("/displayurl", h.handleDisplayURL)
166167

167168
// Handling the shorted URLs, if no one exists, it checks
168169
// in the filesystem and sets headers for caching

internal/handlers/public.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ func (h *Handler) handleInfo(c *gin.Context) {
162162
c.JSON(http.StatusOK, out)
163163
}
164164

165+
// handleDisplayURL returns the URL to use for display purposes
166+
func (h *Handler) handleDisplayURL(c *gin.Context) {
167+
out := util.GetConfig().DisplayURL
168+
c.JSON(http.StatusOK, out)
169+
}
170+
165171
func (h *Handler) handleRecent(c *gin.Context) {
166172
user := c.MustGet("user").(*auth.JWTClaims)
167173
entries, err := h.store.GetUserEntries(user.OAuthProvider, user.OAuthID)

internal/util/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
type Configuration struct {
1717
ListenAddr string `yaml:"ListenAddr" env:"LISTEN_ADDR"`
1818
BaseURL string `yaml:"BaseURL" env:"BASE_URL"`
19+
DisplayURL string `yaml:"DisplayURL" env:"DISPLAY_URL"`
1920
DataDir string `yaml:"DataDir" env:"DATA_DIR"`
2021
Backend string `yaml:"Backend" env:"BACKEND"`
2122
AuthBackend string `yaml:"AuthBackend" env:"AUTH_BACKEND"`
@@ -58,6 +59,7 @@ type proxyAuthConf struct {
5859
var Config = Configuration{
5960
ListenAddr: ":8080",
6061
BaseURL: "http://localhost:3000",
62+
DisplayURL: "",
6163
DataDir: "data",
6264
Backend: "boltdb",
6365
EnableDebugMode: false,
@@ -110,6 +112,11 @@ func (o oAuthConf) Enabled() bool {
110112

111113
// GetConfig returns the configuration from the memory
112114
func GetConfig() Configuration {
115+
// if DisplayURL is not set in the config, default to BaseURL
116+
if Config.DisplayURL == "" {
117+
Config.DisplayURL = Config.BaseURL
118+
}
119+
113120
return Config
114121
}
115122

web/src/Home/Home.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export default class HomeComponent extends Component {
1818
usedSettings: this.urlParams.get('customUrl') ? ['custom'] : [],
1919
customID: this.urlParams.get('customUrl') ? this.urlParams.get('customUrl') : '',
2020
showCustomIDError: false,
21-
expiration: null
21+
expiration: null,
22+
displayURL: window.location.origin
2223
}
2324
}
2425
handleURLChange = (e, { value }) => this.url = value
@@ -31,12 +32,13 @@ export default class HomeComponent extends Component {
3132
onSettingsChange = (e, { value }) => {
3233
this.setState({ usedSettings: value })
3334
}
34-
35-
3635

37-
36+
3837
componentDidMount() {
3938
this.urlInput.focus()
39+
fetch("/displayurl")
40+
.then(response => response.json())
41+
.then(data => this.setState({displayURL: data}));
4042
}
4143
handleURLSubmit = () => {
4244
if (!this.state.showCustomIDError) {
@@ -47,7 +49,7 @@ export default class HomeComponent extends Component {
4749
Password: this.state.usedSettings.includes("protected") && this.password ? this.password : undefined
4850
}, r => this.setState({
4951
links: [...this.state.links, {
50-
shortenedURL: r.URL,
52+
shortenedURL: this.state.displayURL + "/" + this.state.customID,
5153
originalURL: this.url,
5254
expiration: this.state.usedSettings.includes("expire") && this.state.expiration ? this.state.expiration.toISOString() : undefined,
5355
deletionURL: r.DeletionURL
@@ -89,7 +91,7 @@ export default class HomeComponent extends Component {
8991
</MediaQuery>
9092
<Form.Group style={{ marginBottom: "1rem" }}>
9193
{usedSettings.includes("custom") && <Form.Field error={showCustomIDError} width={16}>
92-
<Input label={window.location.origin + "/"} onChange={this.handleCustomIDChange} placeholder='my-shortened-url' value={this.state.customID}/>
94+
<Input label={this.state.displayURL + "/"} onChange={this.handleCustomIDChange} placeholder='my-shortened-url' value={this.state.customID}/>
9395
</Form.Field>}
9496
</Form.Group>
9597
<Form.Group widths="equal">

web/src/Lookup/Lookup.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ import CustomCard from '../Card/Card'
66

77
export default class LookupComponent extends Component {
88
state = {
9-
links: []
9+
links: [],
10+
displayURL: window.location.origin
11+
}
12+
componentDidMount() {
13+
fetch("/displayurl")
14+
.then(response => response.json())
15+
.then(data => this.setState({displayURL: data}));
1016
}
1117
handleURLChange = (e, { value }) => this.url = value
1218
handleURLSubmit = () => {
13-
let id = this.url.replace(window.location.origin + "/", "")
19+
let id = this.url.replace(this.state.displayURL + "/", "")
1420
util.lookupEntry(id, res => this.setState({
1521
links: [...this.state.links, [
1622
res.URL,
17-
this.url,
23+
this.state.displayURL + "/" + this.url,
1824
this.VisitCount,
1925
res.CratedOn,
2026
res.LastVisit,
@@ -30,7 +36,7 @@ export default class LookupComponent extends Component {
3036
<Header size='huge'>URL Lookup</Header>
3137
<Form onSubmit={this.handleURLSubmit}>
3238
<Form.Field>
33-
<Input required size='big' ref={input => this.urlInput = input} action={{ icon: 'arrow right', labelPosition: 'right', content: 'Lookup' }} type='url' onChange={this.handleURLChange} name='url' placeholder={window.location.origin + "/..."} autoComplete="off" />
39+
<Input required size='big' ref={input => this.urlInput = input} action={{ icon: 'arrow right', labelPosition: 'right', content: 'Lookup' }} type='text' label={this.state.displayURL + "/"} onChange={this.handleURLChange} name='url' placeholder={"short url"} autoComplete="off" />
3440
</Form.Field>
3541
</Form>
3642
</Segment>

web/src/Recent/Recent.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ import util from '../util/util'
88

99
export default class RecentComponent extends Component {
1010
state = {
11-
recent: []
11+
recent: [],
12+
displayURL: window.location.origin
1213
}
1314

1415
componentDidMount() {
1516
this.loadRecentURLs()
17+
fetch("/displayurl")
18+
.then(response => response.json())
19+
.then(data => this.setState({displayURL: data}));
1620
}
1721

1822
loadRecentURLs = () => {
@@ -47,7 +51,7 @@ export default class RecentComponent extends Component {
4751
}, {
4852
Header: 'Short URL',
4953
accessor: "ID",
50-
Cell: props => `${window.location.origin}/${props.value}`
54+
Cell: props => `${this.state.displayURL}/${props.value}`
5155
}, {
5256
Header: 'Visitor count',
5357
accessor: "Public.VisitCount"

0 commit comments

Comments
 (0)