Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d275f06

Browse files
committed
Implement optional asset, static and templates dir configurations
1 parent 521a369 commit d275f06

File tree

5 files changed

+78
-16
lines changed

5 files changed

+78
-16
lines changed

Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# license that can be found in the LICENSE file.
55

66
NAME = gopherpit
7+
ENV_PREFIX = $(shell echo $(NAME) | tr '[:lower:]' '[:upper:]')
78
VERSION ?= $(shell cat version)
89
DOCKER_IMAGE ?= gopherpit/gopherpit
910
GO_PACKAGE_PATH = gopherpit.com/gopherpit
@@ -120,12 +121,20 @@ lint:
120121

121122
.PHONY: run
122123
run:
124+
./dist/$(NAME) config
123125
./dist/$(NAME) --debug
124126

125127
.PHONY: develop
126-
develop:
128+
develop: generate-data all run
127129
echo -n -e "\033]0;$(NAME) - $@\007"
128-
reflex --only-files -s -r '(\.html|\.md|\.css|\.js|\.txt)$$' -- make generate-data all run
130+
131+
.PHONY: autoreload
132+
autoreload: all
133+
echo -n -e "\033]0;$(NAME) - $@\007"
134+
$(eval export $(ENV_PREFIX)_ASSETS_DIR=assets)
135+
$(eval export $(ENV_PREFIX)_STATIC_DIR=static)
136+
$(eval export $(ENV_PREFIX)_TEMPLATES_DIR=templates)
137+
reflex --only-files -s -r '(\.html|\.md|\.css|\.js|\.txt)$$' -- make generate-data run
129138

130139
.PHONY: generate-data
131140
generate-data:

cmd/gopherpit/start.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ func startCmd(daemon bool) {
377377
Headers: options.Headers,
378378
SessionCookieName: options.SessionCookieName,
379379
StorageDir: options.StorageDir,
380+
AssetsDir: options.AssetsDir,
381+
StaticDir: options.StaticDir,
382+
TemplatesDir: options.TemplatesDir,
380383
GoogleAnalyticsID: options.GoogleAnalyticsID,
381384
RememberMeDays: userOptions.RememberMeDays,
382385
DefaultFrom: emailOptions.DefaultFrom,

server/config/gopherpit.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ type GopherPitOptions struct {
3232
PidFileMode marshal.Mode `json:"pid-file-mode" yaml:"pid-file-mode" envconfig:"PID_FILE_MODE"`
3333
StorageFileMode marshal.Mode `json:"storage-file-mode" yaml:"storage-file-mode" envconfig:"STORAGE_FILE_MODE"`
3434
StorageDir string `json:"storage-dir" yaml:"storage-dir" envconfig:"STORAGE_DIR"`
35+
AssetsDir string `json:"assets-dir" yaml:"assets-dir" envconfig:"ASSETS_DIR"`
36+
StaticDir string `json:"static-dir" yaml:"static-dir" envconfig:"STATIC_DIR"`
37+
TemplatesDir string `json:"templates-dir" yaml:"templates-dir" envconfig:"TEMPLATES_DIR"`
3538
MaintenanceFilename string `json:"maintenance-filename" yaml:"maintenance-filename" envconfig:"MAINTENANCE_FILENAME"`
3639
GoogleAnalyticsID string `json:"google-analytics-id" yaml:"google-analytics-id" envconfig:"GOOGLE_ANALYTICS_ID"`
3740
ContactRecipientEmail string `json:"contact-recipient-email" yaml:"contact-recipient-email" envconfig:"CONTACT_RECIPIENT_EMAIL"`
@@ -63,6 +66,9 @@ func NewGopherPitOptions() *GopherPitOptions {
6366
PidFileMode: 0644,
6467
StorageFileMode: 0644,
6568
StorageDir: filepath.Join(BaseDir, "storage"),
69+
AssetsDir: "",
70+
StaticDir: "",
71+
TemplatesDir: "",
6672
MaintenanceFilename: "maintenance",
6773
GoogleAnalyticsID: "",
6874
ContactRecipientEmail: Name + "@localhost",
@@ -93,6 +99,24 @@ func (o *GopherPitOptions) VerifyAndPrepare() (err error) {
9399
return
94100
}
95101
}
102+
if o.AssetsDir != "" {
103+
if _, err = os.Stat(o.AssetsDir); os.IsNotExist(err) {
104+
err = fmt.Errorf("Assets directory %s does not exist", o.AssetsDir)
105+
return
106+
}
107+
}
108+
if o.StaticDir != "" {
109+
if _, err = os.Stat(o.StaticDir); os.IsNotExist(err) {
110+
err = fmt.Errorf("Static directory %s does not exist", o.TemplatesDir)
111+
return
112+
}
113+
}
114+
if o.TemplatesDir != "" {
115+
if _, err = os.Stat(o.TemplatesDir); os.IsNotExist(err) {
116+
err = fmt.Errorf("Templates directory %s does not exist", o.TemplatesDir)
117+
return
118+
}
119+
}
96120
ln, err := net.Listen("tcp", o.Listen)
97121
if err != nil {
98122
return

server/routers_fe.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ func newFrontendRouter(s *Server) http.Handler {
3030
return web.NewStaticFilesHandler(h, "/", http.Dir(filepath.Join(s.StorageDir, "static")))
3131
},
3232
func(h http.Handler) http.Handler {
33+
var fs http.FileSystem
34+
if s.StaticDir == "" {
35+
fs = &assetfs.AssetFS{Asset: static.Asset, AssetDir: static.AssetDir, AssetInfo: static.AssetInfo}
36+
} else {
37+
fs = http.Dir(s.StaticDir)
38+
}
3339
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
34-
fs := &assetfs.AssetFS{Asset: static.Asset, AssetDir: static.AssetDir, AssetInfo: static.AssetInfo}
3540
web.NewStaticFilesHandler(h, "/", fs).ServeHTTP(w, r)
3641
})
3742
},

server/server.go

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ type Options struct {
8484
Headers map[string]string
8585
SessionCookieName string
8686
StorageDir string
87+
AssetsDir string
88+
StaticDir string
89+
TemplatesDir string
8790
GoogleAnalyticsID string
8891
RememberMeDays int
8992
DefaultFrom string
@@ -168,27 +171,44 @@ func New(o Options) (s *Server, err error) {
168171
}
169172
s.salt = salt
170173
}
171-
172-
// Create assets server
173-
assetsServer := fileServer.New("/assets", "", &fileServer.Options{
174-
Hasher: fileServer.MD5Hasher{HashLength: 8},
175-
NoHashQueryStrings: true,
176-
RedirectTrailingSlash: true,
177-
IndexPage: "index.html",
178-
Filesystem: &assetfs.AssetFS{
174+
var assetsFS http.FileSystem
175+
if o.AssetsDir == "" {
176+
assetsFS = &assetfs.AssetFS{
179177
Asset: assets.Asset,
180178
AssetDir: assets.AssetDir,
181179
AssetInfo: assets.AssetInfo,
182-
},
180+
}
181+
} else {
182+
assetsFS = http.Dir(o.AssetsDir)
183+
}
184+
185+
// Create assets server
186+
assetsServer := fileServer.New("/assets", "", &fileServer.Options{
187+
Hasher: fileServer.MD5Hasher{HashLength: 8},
188+
NoHashQueryStrings: true,
189+
RedirectTrailingSlash: true,
190+
IndexPage: "index.html",
191+
Filesystem: assetsFS,
183192
NotFoundHandler: http.HandlerFunc(s.htmlNotFoundHandler),
184193
ForbiddenHandler: http.HandlerFunc(s.htmlForbiddenHandler),
185194
InternalServerErrorHandler: http.HandlerFunc(s.htmlInternalServerErrorHandler),
186195
})
187196

188197
// Parse static HTML documents used as loadable fragments in templates
189-
fragments, err := parseMarkdownData("fragments")
190-
if err != nil {
191-
return nil, fmt.Errorf("parse fragments: %v", err)
198+
var fileReadFunc templates.FileReadFunc
199+
var fragments map[string]interface{}
200+
if o.TemplatesDir == "" {
201+
fileReadFunc = dataTemplates.Asset
202+
fragments, err = parseMarkdownData("fragments")
203+
if err != nil {
204+
return nil, fmt.Errorf("parse fragments: %v", err)
205+
}
206+
} else {
207+
fileReadFunc = ioutil.ReadFile
208+
fragments, err = parseMarkdown(filepath.Join(o.TemplatesDir, "fragments"))
209+
if err != nil {
210+
return nil, fmt.Errorf("parse fragments: %v", err)
211+
}
192212
}
193213

194214
s.html, err = templates.New(
@@ -214,7 +234,8 @@ func New(o Options) (s *Server, err error) {
214234
templates.WithFunction("is_gopherpit_domain", func(domain string) bool {
215235
return strings.HasSuffix(domain, "."+o.Domain)
216236
}),
217-
templates.WithFileReadFunc(dataTemplates.Asset),
237+
templates.WithBaseDir(o.TemplatesDir),
238+
templates.WithFileReadFunc(fileReadFunc),
218239
templates.WithTemplatesFromFiles(htmlTemplates),
219240
)
220241
if err != nil {

0 commit comments

Comments
 (0)