cmd/sgblog/gopher.go | 20 ++------------------ cmd/sgblog/http.go | 20 +++----------------- cmd/sgblog/main.go | 36 +++++++++++++++++++++++++++++++++--- common.go | 2 +- diff --git a/cmd/sgblog/gopher.go b/cmd/sgblog/gopher.go index f1707c2936e7a35ef556cd117ca51d4dcdc43b0e40a63674448835f702af2895..ef628791ed9bd637c572e84d1af8731b9a7ce610d3e8da9f080029ce312b6842 100644 --- a/cmd/sgblog/gopher.go +++ b/cmd/sgblog/gopher.go @@ -19,11 +19,9 @@ package main import ( "bufio" - "encoding/json" "errors" "fmt" "io" - "io/ioutil" "log" "net/url" "os" @@ -34,7 +32,6 @@ "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" - "github.com/hjson/hjson-go" "go.stargrave.org/sgblog" ) @@ -86,22 +83,9 @@ CommentsNum int Topics []string } -func serveGopher() { - cfgPath := os.Args[2] - cfgRaw, err := ioutil.ReadFile(cfgPath) +func serveGopher(cfgPath string) { + cfg, err := readCfg(cfgPath) if err != nil { - log.Fatalln(err) - } - var cfgGeneral map[string]interface{} - if err = hjson.Unmarshal(cfgRaw, &cfgGeneral); err != nil { - log.Fatalln(err) - } - cfgRaw, err = json.Marshal(cfgGeneral) - if err != nil { - log.Fatalln(err) - } - var cfg *Cfg - if err = json.Unmarshal(cfgRaw, &cfg); err != nil { log.Fatalln(err) } if cfg.GopherDomain == "" { diff --git a/cmd/sgblog/http.go b/cmd/sgblog/http.go index 6ada8e1bd1c2085ec51f5ea5673a19b4576458db98adea35cd245fdb8d952729..bbda7cba47a85dba97feb2918cb44a5abc7b051ff17a4205173ea2c41bcf9c34 100644 --- a/cmd/sgblog/http.go +++ b/cmd/sgblog/http.go @@ -22,14 +22,12 @@ "bytes" "compress/gzip" "crypto/sha1" "encoding/hex" - "encoding/json" "encoding/xml" "errors" "fmt" "hash" "html" "io" - "io/ioutil" "log" "net/url" "os" @@ -40,7 +38,6 @@ "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" - "github.com/hjson/hjson-go" "go.stargrave.org/sgblog" "go.stargrave.org/sgblog/cmd/sgblog/atom" "golang.org/x/crypto/blake2b" @@ -260,22 +257,11 @@ cfgPath := os.Getenv("SGBLOG_CFG") if cfgPath == "" { log.Fatalln("SGBLOG_CFG is not set") } - cfgRaw, err := ioutil.ReadFile(cfgPath) + cfg, err := readCfg(cfgPath) if err != nil { - makeErr(err) + log.Fatalln(err) } - var cfgGeneral map[string]interface{} - if err = hjson.Unmarshal(cfgRaw, &cfgGeneral); err != nil { - makeErr(err) - } - cfgRaw, err = json.Marshal(cfgGeneral) - if err != nil { - makeErr(err) - } - var cfg *Cfg - if err = json.Unmarshal(cfgRaw, &cfg); err != nil { - makeErr(err) - } + pathInfo, exists := os.LookupEnv("PATH_INFO") if !exists { pathInfo = "/" diff --git a/cmd/sgblog/main.go b/cmd/sgblog/main.go index 0d6e79f2fbf72cf64c204bca43f48e1a598122e0e4ed5b8cb68a6ae1ddbf6b55..12bb4f1b5cb0d774fa4e147332c9b4ce57e1c44385070711c5de36cd50d1a926 100644 --- a/cmd/sgblog/main.go +++ b/cmd/sgblog/main.go @@ -21,9 +21,10 @@ import ( "bytes" "crypto/sha1" + "encoding/json" + "flag" "fmt" "io/ioutil" - "os" "regexp" "sort" "strings" @@ -32,6 +33,7 @@ "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" + "github.com/hjson/hjson-go" "go.cypherpunks.ru/recfile" ) @@ -195,9 +197,37 @@ } return &headHash, nil } +func readCfg(cfgPath string) (*Cfg, error) { + cfgRaw, err := ioutil.ReadFile(cfgPath) + if err != nil { + return nil, err + } + var cfgGeneral map[string]interface{} + if err = hjson.Unmarshal(cfgRaw, &cfgGeneral); err != nil { + return nil, err + } + cfgRaw, err = json.Marshal(cfgGeneral) + if err != nil { + return nil, err + } + var cfg *Cfg + if err = json.Unmarshal(cfgRaw, &cfg); err != nil { + return nil, err + } + return cfg, nil +} + func main() { - if len(os.Args) == 3 && os.Args[1] == "-gopher" { - serveGopher() + gopherCfgPath := flag.String("gopher", "", "Path to gopher-related configuration file") + flag.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), `Usage of sgblog: + sgblog -- run CGI HTTP backend + sgblog -gopher /path/to/cfg.hjson -- run UCSPI/inetd Gopher backend +`) + } + flag.Parse() + if *gopherCfgPath != "" { + serveGopher(*gopherCfgPath) } else { serveHTTP() } diff --git a/common.go b/common.go index ab42dd7b09e6d68ad67805a0b8df6e295c9b2c347dc72d25c3e08322c9079716..bdc594d109258b173686231a8c7c288ac5fb924a4b958e33e9c849af3395a58d 100644 --- a/common.go +++ b/common.go @@ -2,6 +2,6 @@ // SGBlog -- Git-backed CGI/inetd blogging/phlogging engine package sgblog const ( - Version = "0.16.0" + Version = "0.17.0" WhenFmt = "2006-01-02 15:04:05Z07:00" )