Add BaseURL config parameter

This commit is contained in:
wpetit 2021-07-26 14:47:58 +02:00
parent 61c3cd33be
commit 8027ba7bcc
6 changed files with 28 additions and 10 deletions

View File

@ -6,10 +6,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{block "title" . -}}{{- end}}</title> <title>{{block "title" . -}}{{- end}}</title>
{{- block "head_style" . -}} {{- block "head_style" . -}}
<link rel="stylesheet" href="/css/main.css" /> <link rel="stylesheet" href="{{ .BaseURL }}/css/main.css" />
{{end}} {{end}}
{{- block "head_script" . -}} {{- block "head_script" . -}}
<script type="text/javascript" src="/main.js"></script> <script type="text/javascript" src="{{ .BaseURL }}/main.js"></script>
{{end}} {{end}}
</head> </head>
<body> <body>

View File

@ -2,7 +2,7 @@
<div class="columns is-mobile"> <div class="columns is-mobile">
<div class="column is-narrow"> <div class="column is-narrow">
<h1 class="is-size-3 title"> <h1 class="is-size-3 title">
<a href="/" rel="Inbox" class="has-text-grey-dark"> <a href="{{ .BaseURL }}/" rel="Inbox" class="has-text-grey-dark">
{{if or .Emails .Email}} {{if or .Emails .Email}}
📬 📬
{{else}} {{else}}

View File

@ -2,9 +2,9 @@
{{define "header_buttons"}} {{define "header_buttons"}}
<button class="button is-danger" <button class="button is-danger"
data-controller="restful" data-controller="restful"
data-restful-endpoint="./{{ .Email.ID }}" data-restful-endpoint="{{ .BaseURL }}/emails/{{ .Email.ID }}"
data-restful-method="DELETE" data-restful-method="DELETE"
data-restful-redirect="../"> data-restful-redirect="{{ .BaseURL }}/">
🗑️ Delete 🗑️ Delete
</button> </button>
{{end}} {{end}}
@ -24,8 +24,9 @@
<h4 class="title is-size-4">Attachments ({{len .Email.Attachments}})</h4> <h4 class="title is-size-4">Attachments ({{len .Email.Attachments}})</h4>
<ul> <ul>
{{ $email := .Email }} {{ $email := .Email }}
{{ $baseURL := .BaseURL }}
{{range $i, $a := .Email.Attachments}} {{range $i, $a := .Email.Attachments}}
<li><a href="{{ $email.ID }}/attachments/{{ $i }}" download="{{ $a.Name }}">{{ $a.Name }}</a></li> <li><a href="{{ $baseURL }}/emails/{{ $email.ID }}/attachments/{{ $i }}" download="{{ $a.Name }}">{{ $a.Name }}</a></li>
{{end}} {{end}}
</ul> </ul>
</div> </div>
@ -44,7 +45,7 @@
data-controller="iframe" data-controller="iframe"
data-action="load->iframe#onLoad" data-action="load->iframe#onLoad"
style="width:100%;{{if not .Email.HTML}}display:none;{{end}}" style="width:100%;{{if not .Email.HTML}}display:none;{{end}}"
src="{{ .Email.ID }}/html"> src="{{ .BaseURL }}/emails/{{ .Email.ID }}/html">
</iframe> </iframe>
<div data-target="tabs.tabContent" data-tabs-for="text" style="{{if .Email.HTML}}display:none;{{end}}width:100%;overflow:hidden;"> <div data-target="tabs.tabContent" data-tabs-for="text" style="{{if .Email.HTML}}display:none;{{end}}width:100%;overflow:hidden;">
<pre style="white-space:pre-line;">{{ .Email.Text }}</pre> <pre style="white-space:pre-line;">{{ .Email.Text }}</pre>

View File

@ -24,10 +24,11 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{{ $baseURL := .BaseURL }}
{{range .Emails}} {{range .Emails}}
<tr data-controller="inbox-entry" <tr data-controller="inbox-entry"
data-action="click->inbox-entry#onClick" data-action="click->inbox-entry#onClick"
data-inbox-entry-link="./emails/{{ .ID }}"> data-inbox-entry-link="{{ $baseURL }}/emails/{{ .ID }}">
<td class="email-subject"><div>{{ .Subject }}</div></td> <td class="email-subject"><div>{{ .Subject }}</div></td>
<td class="email-from"> <td class="email-from">
{{range .From}} {{range .From}}
@ -44,7 +45,7 @@
</td> </td>
<td class="email-actions"> <td class="email-actions">
<div class="buttons is-right"> <div class="buttons is-right">
<a href="./emails/{{ .ID }}" class="button is-small is-link">👁️ See</a> <a href="{{ $baseURL }}/emails/{{ .ID }}" class="button is-small is-link">👁️ See</a>
</div> </div>
</td> </td>
</tr> </tr>

View File

@ -22,6 +22,7 @@ type HTTPConfig struct {
Address string `yaml:"address" env:"FAKESMTP_HTTP_ADDRESS"` Address string `yaml:"address" env:"FAKESMTP_HTTP_ADDRESS"`
TemplateDir string `yaml:"templateDir" env:"FAKESMTP_HTTP_TEMPLATEDIR"` TemplateDir string `yaml:"templateDir" env:"FAKESMTP_HTTP_TEMPLATEDIR"`
PublicDir string `yaml:"publicDir" env:"FAKESMTP_HTTP_PUBLICDIR"` PublicDir string `yaml:"publicDir" env:"FAKESMTP_HTTP_PUBLICDIR"`
BaseURL string `yaml:"baseUrl" env:"FAKESMTP_HTTP_BASEURL"`
} }
type SMTPConfig struct { type SMTPConfig struct {

View File

@ -5,9 +5,11 @@ import (
"strconv" "strconv"
"time" "time"
"forge.cadoles.com/wpetit/fake-smtp/internal/config"
"forge.cadoles.com/wpetit/fake-smtp/internal/query" "forge.cadoles.com/wpetit/fake-smtp/internal/query"
"github.com/pkg/errors" "github.com/pkg/errors"
"gitlab.com/wpetit/goweb/middleware/container" "gitlab.com/wpetit/goweb/middleware/container"
"gitlab.com/wpetit/goweb/service"
"gitlab.com/wpetit/goweb/service/template" "gitlab.com/wpetit/goweb/service/template"
) )
@ -15,8 +17,8 @@ func extendTemplateData(w http.ResponseWriter, r *http.Request, data template.Da
ctn := container.Must(r.Context()) ctn := container.Must(r.Context())
data, err := template.Extend(data, data, err := template.Extend(data,
template.WithBuildInfo(w, r, ctn), template.WithBuildInfo(w, r, ctn),
withBaseURL(ctn),
) )
if err != nil { if err != nil {
panic(errors.Wrap(err, "could not extend template data")) panic(errors.Wrap(err, "could not extend template data"))
} }
@ -104,3 +106,16 @@ func createInboxQueryFromRequest(r *http.Request) (*query.GetInboxRequest, error
return inboxRequest, nil return inboxRequest, nil
} }
func withBaseURL(ctn *service.Container) template.DataExtFunc {
return func(data template.Data) (template.Data, error) {
conf, err := config.From(ctn)
if err != nil {
return nil, errors.WithStack(err)
}
data["BaseURL"] = conf.HTTP.BaseURL
return data, nil
}
}