phase3: HTTP server with converter, one-shot preview store, and middleware

This commit is contained in:
Sergey Filkin
2026-04-18 11:55:42 +03:00
parent d1682813ff
commit 843d8dc710
8 changed files with 1033 additions and 4 deletions
+42 -4
View File
@@ -1,12 +1,18 @@
package main
import (
"context"
"flag"
"fmt"
"io"
"os"
"os/signal"
"syscall"
"github.com/fserg/md-to-html/internal/converter"
"github.com/fserg/md-to-html/internal/server"
"github.com/fserg/md-to-html/internal/version"
webtemplate "github.com/fserg/md-to-html/web/template"
)
func main() {
@@ -24,7 +30,7 @@ func run(args []string, stdout, stderr io.Writer) int {
printUsage(stdout)
return 0
case "serve":
return runServe(args[1:], stdout)
return runServe(args[1:], stdout, stderr)
case "cli":
return runCLI(args[1:], stdout, stderr)
case "version":
@@ -36,13 +42,45 @@ func run(args []string, stdout, stderr io.Writer) int {
}
}
func runServe(args []string, stdout io.Writer) int {
func runServe(args []string, stdout, stderr io.Writer) int {
fs := flag.NewFlagSet("serve", flag.ContinueOnError)
fs.SetOutput(io.Discard)
if err := fs.Parse(args); err != nil {
return 2
}
fmt.Fprintln(stdout, "serve not implemented yet")
if fs.NArg() != 0 {
fmt.Fprintln(stderr, "usage: md-to-html serve")
return 2
}
cfg, err := server.LoadConfig()
if err != nil {
fmt.Fprintf(stderr, "load config: %v\n", err)
return 1
}
conv, err := converter.New(webtemplate.FS)
if err != nil {
fmt.Fprintf(stderr, "load converter: %v\n", err)
return 1
}
srv, err := server.New(cfg, conv)
if err != nil {
fmt.Fprintf(stderr, "create server: %v\n", err)
return 1
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
if err := srv.Run(ctx); err != nil {
fmt.Fprintf(stderr, "run server: %v\n", err)
return 1
}
_ = stdout
return 0
}
@@ -85,7 +123,7 @@ func printUsage(w io.Writer) {
md-to-html version
Commands:
serve Start the HTTP server stub
serve Start the HTTP server
cli Convert a Markdown file stub
version Print the build version
`)