Added small embedded server

Author Mitja Felicijan <m@mitjafelicijan.com> 2023-07-08 21:51:20 +0200
Committer Mitja Felicijan <m@mitjafelicijan.com> 2023-07-08 21:51:20 +0200
Commit 47742e9fcaec60798f7d67202e4fa0c25c64b3a1 (patch)
-rw-r--r-- README.md 2
-rw-r--r-- main.go 19
2 files changed, 18 insertions, 3 deletions
diff --git a/README.md b/README.md
...
22
- All files in `static` folder will be moved to the root of `public` folder.
22
- All files in `static` folder will be moved to the root of `public` folder.
23
- When you provide `url` in your markdown files, this will create these files in
23
- When you provide `url` in your markdown files, this will create these files in
24
  the root of `public` folder. No nesting allowed.
24
  the root of `public` folder. No nesting allowed.
  
25
- Comes with a small embedded HTTP server you can invoke with `jbmafo --server`
  
26
  which will server contents from `public` folder. Good for testing stuff.
25
  
27
  
26
## Install
28
## Install
27
  
29
  
...
diff --git a/main.go b/main.go
...
5
	"fmt"
5
	"fmt"
6
	"html/template"
6
	"html/template"
7
	"log"
7
	"log"
  
8
	"net/http"
8
	"os"
9
	"os"
9
	"path"
10
	"path"
10
	"path/filepath"
11
	"path/filepath"
...
89
	p := bluemonday.StrictPolicy()
90
	p := bluemonday.StrictPolicy()
90
	cleanString := p.Sanitize(htmlString)
91
	cleanString := p.Sanitize(htmlString)
91
	return cleanString
92
	return cleanString
  
93
}
  
94
  
  
95
func simpleServer(projectRoot string) {
  
96
	fs := http.FileServer(http.Dir(path.Join(projectRoot, "public")))
  
97
	http.Handle("/", fs)
  
98
	log.Println("Server started on http://localhost:6969")
  
99
	log.Fatal(http.ListenAndServe(":6969", nil))
92
}
100
}
93
  
101
  
94
func initializeProject(projectRoot string) {
102
func initializeProject(projectRoot string) {
...
353
	}
361
	}
354
  
362
  
355
	var args struct {
363
	var args struct {
356
		Init  bool `arg:"-i,--init" help:"initialize new project"`
364
		Init   bool `arg:"-i,--init" help:"initialize new project"`
357
		Build bool `arg:"-b,--build" help:"build the website"`
365
		Build  bool `arg:"-b,--build" help:"build the website"`
  
366
		Server bool `arg:"-s,--server" help:"simple embedded HTTP server"`
358
	}
367
	}
359
  
368
  
360
	arg.MustParse(&args)
369
	arg.MustParse(&args)
361
  
370
  
362
	if !args.Init && !args.Build {
371
	if !args.Init && !args.Build && !args.Server {
363
		fmt.Println("No arguments provided. Try using `jbmafp --help`")
372
		fmt.Println("No arguments provided. Try using `jbmafp --help`")
364
		os.Exit(0)
373
		os.Exit(0)
365
	}
374
	}
...
370
  
379
  
371
	if args.Build {
380
	if args.Build {
372
		buildProject(projectRoot)
381
		buildProject(projectRoot)
  
382
	}
  
383
  
  
384
	if args.Server {
  
385
		simpleServer(projectRoot)
373
	}
386
	}
374
}
387
}