Changeset 61 in code for trunk/cmd


Ignore:
Timestamp:
Apr 21, 2023, 11:06:28 PM (2 years ago)
Author:
Izuru Yakumo
Message:

Add serve function, update documentation accordingly

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/cmd/aya/main.go

    r60 r61  
    55        "fmt"
    66        "io"
    7         "io/ioutil"
     7        "net/http"
    88        "os"
    99        "os/exec"
     
    6060func run(vars Vars, cmd string, args ...string) (string, error) {
    6161        // First check if partial exists (.html)
    62         if b, err := ioutil.ReadFile(filepath.Join(AYADIR, cmd+".html")); err == nil {
     62        if b, err := os.ReadFile(filepath.Join(AYADIR, cmd+".html")); err == nil {
    6363                return string(b), nil
    6464        }
     
    9191// If no empty newline is found - file is treated as content-only.
    9292func getVars(path string, globals Vars) (Vars, string, error) {
    93         b, err := ioutil.ReadFile(path)
     93        b, err := os.ReadFile(path)
    9494        if err != nil {
    9595                return nil, "", err
     
    360360        }
    361361}
     362// Serve the public directory over HTTP
     363func servePubDir() {
     364        rootdir := http.Dir(PUBDIR)
     365        http.Handle("/", http.FileServer(rootdir))
     366        log.Fatal(http.ListenAndServe(":8000", nil))
     367}
    362368
    363369func init() {
     
    373379        fmt.Printf("Where <command> is:\n")
    374380        fmt.Printf("\tbuild\tGenerate site\n")
     381        fmt.Printf("\tserve\tServe %v over HTTP\n", PUBDIR)
     382        fmt.Printf("\tvar\tQuery variable(s) from a markdown file\n")
     383        fmt.Printf("\tversion\tPrint program version and exit\n")
    375384        fmt.Printf("\twatch\t(Re)generate site while looking for changes\n")
    376         fmt.Printf("\tvar\tQuery a variable from a markdown file\n")
    377         fmt.Printf("\tversion\tPrint version and exit\n")
    378385        fmt.Printf("\n")
    379386        fmt.Printf("Other commands may be dynamically added by plugins found in %v\n", AYADIR)
     
    398405                        fmt.Println("ERROR: too many arguments")
    399406                }
    400         case "watch":
    401                 buildAll(true)
     407        case "serve":
     408                servePubDir()
    402409        case "var":
    403410                if len(args) == 0 {
     
    421428                }
    422429        case "version":
    423                 fmt.Printf("%v\n", aya.Version)
     430                fmt.Printf("%v\n", aya.FullVersion())
    424431                os.Exit(0)
     432        case "watch":
     433                buildAll(true)
    425434        default:
    426435                if s, err := run(globals(), cmd, args...); err != nil {
Note: See TracChangeset for help on using the changeset viewer.