summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2020-05-01 13:58:48 +0700
committerShulhan <ms@kilabit.info>2020-05-01 13:58:48 +0700
commit8713b9674b608dd2c7e95215d4b7e8a298898611 (patch)
tree7ac82be926b84f686497323e685dd2928420233e
parent9789c9f4528aa9e5e5e7d25009b2264e47298d2a (diff)
downloadciigo-8713b9674b608dd2c7e95215d4b7e8a298898611.tar.xz
all: simplify serving content using function Serve
Previously to serve the generated content we call two fucntions: NewServer() and Server.Start(). This changes unexported the internal server, and expose only the Serve() function with the same parameter as NewServer().
-rw-r--r--ciigo.go28
-rw-r--r--cmd/ciigo-example/main.go4
-rw-r--r--cmd/ciigo/main.go3
-rw-r--r--server.go37
4 files changed, 39 insertions, 33 deletions
diff --git a/ciigo.go b/ciigo.go
index dbee606..1b0b96e 100644
--- a/ciigo.go
+++ b/ciigo.go
@@ -22,8 +22,9 @@ import (
)
const (
+ defAddress = ":8080"
+ defDir = "."
dirAssets = "assets"
- dirRoot = "./content"
extAsciidoc = ".adoc"
extMarkdown = ".md"
)
@@ -81,14 +82,14 @@ func Convert(dir, htmlTemplate string) {
//
// Generate a static Go file to be used for building binary.
//
-// It will convert all markup files inside root directory into HTML files,
+// It will convert all markup files inside directory "dir" into HTML files,
// recursively; and read all the HTML files and files in "content/assets" and
// convert them into Go file in "out".
//
// If htmlTemplate is empty it will default to use embedded HTML template.
// See template_index_html.go for template format.
//
-func Generate(root, out, htmlTemplate string) {
+func Generate(dir, out, htmlTemplate string) {
contentHTML := templateIndexHTML
if len(htmlTemplate) > 0 {
@@ -100,7 +101,7 @@ func Generate(root, out, htmlTemplate string) {
}
htmlg := newHTMLGenerator(htmlTemplate, contentHTML)
- fileMarkups := listFileMarkups(root)
+ fileMarkups := listFileMarkups(dir)
htmlg.convertFileMarkups(fileMarkups, len(htmlTemplate) == 0)
@@ -109,9 +110,9 @@ func Generate(root, out, htmlTemplate string) {
log.Fatal("ciigo.Generate: " + err.Error())
}
- err = mfs.Mount(root)
+ err = mfs.Mount(dir)
if err != nil {
- log.Fatalf("ciigo.Generate: Mount %s: %s", root, err.Error())
+ log.Fatalf("ciigo.Generate: Mount %s: %s", dir, err.Error())
}
if len(htmlTemplate) > 0 {
@@ -127,6 +128,21 @@ func Generate(root, out, htmlTemplate string) {
}
}
+//
+// Serve the content at directory "dir" using HTTP server at specific
+// "address".
+//
+func Serve(dir, address, htmlTemplate string) {
+ if len(dir) == 0 {
+ dir = defDir
+ }
+ if len(address) == 0 {
+ address = defAddress
+ }
+ srv := newServer(dir, address, htmlTemplate)
+ srv.start()
+}
+
func isExtensionMarkup(ext string) bool {
return ext == extAsciidoc || ext == extMarkdown
}
diff --git a/cmd/ciigo-example/main.go b/cmd/ciigo-example/main.go
index a636b0e..30f4e82 100644
--- a/cmd/ciigo-example/main.go
+++ b/cmd/ciigo-example/main.go
@@ -13,7 +13,5 @@ import (
)
func main() {
- srv := ciigo.NewServer("./content", ":8080", "./templates/html.tmpl")
-
- srv.Start()
+ ciigo.Serve("./content", ":8080", "./templates/html.tmpl")
}
diff --git a/cmd/ciigo/main.go b/cmd/ciigo/main.go
index dd48822..1747749 100644
--- a/cmd/ciigo/main.go
+++ b/cmd/ciigo/main.go
@@ -76,8 +76,7 @@ func main() {
ciigo.Generate(dir, *outputFile, *htmlTemplate)
case "serve":
debug.Value = 2
- srv := ciigo.NewServer(dir, *address, *htmlTemplate)
- srv.Start()
+ ciigo.Serve(dir, *address, *htmlTemplate)
default:
usage()
os.Exit(1)
diff --git a/server.go b/server.go
index 762d391..c87ae4d 100644
--- a/server.go
+++ b/server.go
@@ -22,9 +22,9 @@ import (
)
//
-// Server contains the HTTP server.
+// server contains the HTTP server.
//
-type Server struct {
+type server struct {
http *libhttp.Server
opts *libhttp.ServerOptions
htmlg *htmlGenerator
@@ -33,23 +33,16 @@ type Server struct {
}
//
-// NewServer create an HTTP server to serve HTML files in directory "root".
+// newServer create an HTTP server to serve HTML files in directory "root".
//
// The address parameter is optional, if not set its default to ":8080".
// The htmlTemplate parameter is optional, if not set its default to
// embedded HTML template.
//
-func NewServer(root, address, htmlTemplate string) (srv *Server) {
+func newServer(root, address, htmlTemplate string) (srv *server) {
var err error
- if len(root) == 0 {
- root = dirRoot
- }
- if len(address) == 0 {
- address = ":8080"
- }
-
- srv = &Server{
+ srv = &server{
opts: &libhttp.ServerOptions{
Address: address,
Root: root,
@@ -87,9 +80,9 @@ func NewServer(root, address, htmlTemplate string) (srv *Server) {
}
//
-// Start the web server.
+// start the web server.
//
-func (srv *Server) Start() {
+func (srv *server) start() {
if srv.opts.Development {
srv.autoGenerate()
}
@@ -103,7 +96,7 @@ func (srv *Server) Start() {
}
}
-func (srv *Server) autoGenerate() {
+func (srv *server) autoGenerate() {
srv.dw = &libio.DirWatcher{
Path: srv.opts.Root,
Delay: time.Second,
@@ -132,7 +125,7 @@ func (srv *Server) autoGenerate() {
}
}
-func (srv *Server) initHTMLGenerator(htmlTemplate string) {
+func (srv *server) initHTMLGenerator(htmlTemplate string) {
if len(htmlTemplate) == 0 {
srv.htmlg = newHTMLGenerator("", templateIndexHTML)
return
@@ -149,17 +142,17 @@ func (srv *Server) initHTMLGenerator(htmlTemplate string) {
if srv.opts.Development {
bhtml, err = ioutil.ReadFile(htmlTemplate)
if err != nil {
- log.Fatal("Server.initHTMLGenerator: " + err.Error())
+ log.Fatal("server.initHTMLGenerator: " + err.Error())
}
} else {
tmplNode, err := srv.http.Memfs.Get(htmlTemplate)
if err != nil {
- log.Fatalf("Server.initHTMLGenerator: Memfs.Get %s: %s",
+ log.Fatalf("server.initHTMLGenerator: Memfs.Get %s: %s",
htmlTemplate, err.Error())
}
bhtml, err = tmplNode.Decode()
if err != nil {
- log.Fatal("Server.initHTMLGenerator: " + err.Error())
+ log.Fatal("server.initHTMLGenerator: " + err.Error())
}
}
@@ -171,7 +164,7 @@ func (srv *Server) initHTMLGenerator(htmlTemplate string) {
// onChangeFileMarkup watch the markup files inside the "content" directory,
// and re-generate them into HTML file when changed.
//
-func (srv *Server) onChangeFileMarkup(ns *libio.NodeState) {
+func (srv *server) onChangeFileMarkup(ns *libio.NodeState) {
if ns.State == libio.FileStateDeleted {
fmt.Printf("ciigo: onChangeFileMarkup: %q deleted\n", ns.Node.SysPath)
return
@@ -225,7 +218,7 @@ func (srv *Server) onChangeFileMarkup(ns *libio.NodeState) {
srv.htmlg.convert(fmarkup, fhtml, true)
}
-func (srv *Server) onChangeHTMLTemplate(ns *libio.NodeState) {
+func (srv *server) onChangeHTMLTemplate(ns *libio.NodeState) {
if ns.State == libio.FileStateDeleted {
fmt.Printf("watchHTMLTemplate: file %q deleted\n", ns.Node.SysPath)
return
@@ -244,7 +237,7 @@ func (srv *Server) onChangeHTMLTemplate(ns *libio.NodeState) {
srv.htmlg.convertFileMarkups(srv.fileMarkups, true)
}
-func (srv *Server) onSearch(res http.ResponseWriter, req *http.Request, reqBody []byte) (
+func (srv *server) onSearch(res http.ResponseWriter, req *http.Request, reqBody []byte) (
resBody []byte, err error,
) {
var bufSearch, buf bytes.Buffer