diff --git a/README.md b/README.md index 5a7bab6d2cc1362628bda2de7384e02b5dcc8e26..41fd492dbfc3a405bfa192933cffe66186a99283 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ - `public` folder gets automatically created on `jbmafp --build`. - All files in `static` folder will be moved to the root of `public` folder. - When you provide `url` in your markdown files, this will create these files in the root of `public` folder. No nesting allowed. +- Comes with a small embedded HTTP server you can invoke with `jbmafo --server` + which will server contents from `public` folder. Good for testing stuff. ## Install diff --git a/main.go b/main.go index 226e3d6f3c86986ecd6a41751aaabe9facda13c7..88d21c1316535b31998cb2f350df0f3b6c77c85a 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ "bytes" "fmt" "html/template" "log" + "net/http" "os" "path" "path/filepath" @@ -89,6 +90,13 @@ func cleanHTMLTags(htmlString string) string { p := bluemonday.StrictPolicy() cleanString := p.Sanitize(htmlString) return cleanString +} + +func simpleServer(projectRoot string) { + fs := http.FileServer(http.Dir(path.Join(projectRoot, "public"))) + http.Handle("/", fs) + log.Println("Server started on http://localhost:6969") + log.Fatal(http.ListenAndServe(":6969", nil)) } func initializeProject(projectRoot string) { @@ -353,13 +361,14 @@ projectRoot = "./" } var args struct { - Init bool `arg:"-i,--init" help:"initialize new project"` - Build bool `arg:"-b,--build" help:"build the website"` + Init bool `arg:"-i,--init" help:"initialize new project"` + Build bool `arg:"-b,--build" help:"build the website"` + Server bool `arg:"-s,--server" help:"simple embedded HTTP server"` } arg.MustParse(&args) - if !args.Init && !args.Build { + if !args.Init && !args.Build && !args.Server { fmt.Println("No arguments provided. Try using `jbmafp --help`") os.Exit(0) } @@ -370,5 +379,9 @@ } if args.Build { buildProject(projectRoot) + } + + if args.Server { + simpleServer(projectRoot) } }