aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2019-08-05 22:43:27 +0700
committerShulhan <ms@kilabit.info>2019-08-05 22:43:27 +0700
commite146ea686fdecee45311eab1285e0e361bf501df (patch)
tree8a882e861b23cfd73009c12aca8c87079aaf9067
parent616712b8a644fc346681d112ce3f866315371c83 (diff)
downloadciigo-e146ea686fdecee45311eab1285e0e361bf501df.tar.xz
server: add parameter to change the root directory
Previously the default root directory to be served by ciigo is "content" directory inside the top directory. This change allow ciigo to serve any directory pointed by first parameter in NewServer(), and if its empty default to "content".
-rw-r--r--ciigo.go8
-rw-r--r--cmd/ciigo/main.go2
-rw-r--r--content/index.adoc2
-rw-r--r--generate.go8
-rw-r--r--generate_main.go7
-rw-r--r--server.go25
6 files changed, 26 insertions, 26 deletions
diff --git a/ciigo.go b/ciigo.go
index c725096..01763e3 100644
--- a/ciigo.go
+++ b/ciigo.go
@@ -24,6 +24,14 @@ const (
markupKindMarkdown
)
+var (
+ defExcludes = []string{
+ `.*\.adoc$`,
+ `.*\.md$`,
+ `^\..*`,
+ }
+)
+
func isExtensionMarkup(ext string) bool {
return ext == extAsciidoc || ext == extMarkdown
}
diff --git a/cmd/ciigo/main.go b/cmd/ciigo/main.go
index e42a592..23be27e 100644
--- a/cmd/ciigo/main.go
+++ b/cmd/ciigo/main.go
@@ -9,7 +9,7 @@ import (
)
func main() {
- srv := ciigo.NewServer(":8080")
+ srv := ciigo.NewServer("./content", ":8080")
srv.Start()
}
diff --git a/content/index.adoc b/content/index.adoc
index 8b96204..5be9b37 100644
--- a/content/index.adoc
+++ b/content/index.adoc
@@ -102,7 +102,7 @@ import (
)
func main() {
- srv := ciigo.NewServer(":8080")
+ srv := ciigo.NewServer("./content", ":8080")
srv.Start()
}
diff --git a/generate.go b/generate.go
index bfd97b0..1bb8032 100644
--- a/generate.go
+++ b/generate.go
@@ -28,13 +28,7 @@ func Generate(root, out string) {
htmlg.convertMarkupFiles(markupFiles, false)
- excs := []string{
- `.*\.adoc$`,
- `.*\.md$`,
- `^\..*`,
- }
-
- mfs, err := memfs.New(nil, excs, true)
+ mfs, err := memfs.New(nil, defExcludes, true)
if err != nil {
log.Fatal("ciigo: Generate: " + err.Error())
}
diff --git a/generate_main.go b/generate_main.go
index 61c5bed..e28df84 100644
--- a/generate_main.go
+++ b/generate_main.go
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+//go:generate go run generate_main.go
// +build ignore
package main
@@ -10,10 +11,6 @@ import (
"github.com/shuLhan/ciigo"
)
-const (
- dirRoot = "./content"
-)
-
func main() {
- ciigo.Generate(dirRoot, "cmd/ciigo/static.go")
+ ciigo.Generate("./content", "cmd/ciigo/static.go")
}
diff --git a/server.go b/server.go
index 25d4f78..03fd581 100644
--- a/server.go
+++ b/server.go
@@ -31,23 +31,23 @@ type Server struct {
// NewServer create an HTTP server to serve HTML files in directory "content".
// The address parameter is optional, if not set its default to ":8080".
//
-func NewServer(address string) (srv *Server) {
+func NewServer(root, address string) (srv *Server) {
var err error
+ if len(root) == 0 {
+ root = dirRoot
+ }
if len(address) == 0 {
address = ":8080"
}
- srv = new(Server)
-
- srv.opts = &libhttp.ServerOptions{
- Address: address,
- Root: dirRoot,
- Excludes: []string{
- `.*\.adoc$`,
- `.*\.md$`,
+ srv = &Server{
+ opts: &libhttp.ServerOptions{
+ Address: address,
+ Root: root,
+ Excludes: defExcludes,
+ Development: debug.Value > 0,
},
- Development: debug.Value > 0,
}
srv.http, err = libhttp.NewServer(srv.opts)
@@ -57,7 +57,7 @@ func NewServer(address string) (srv *Server) {
if srv.opts.Development {
srv.htmlg = newHTMLGenerator()
- srv.markupFiles = listMarkupFiles(dirRoot)
+ srv.markupFiles = listMarkupFiles(root)
}
return srv
@@ -82,7 +82,7 @@ func (srv *Server) Start() {
func (srv *Server) autoGenerate() {
srv.dw = &libio.DirWatcher{
- Path: dirRoot,
+ Path: srv.opts.Root,
Delay: time.Second,
Includes: []string{
`.*\.adoc$`,
@@ -91,6 +91,7 @@ func (srv *Server) autoGenerate() {
Excludes: []string{
`assets/.*`,
`.*\.html$`,
+ `^\..*`,
},
Callback: srv.onChangeMarkupFile,
}