github.com/bcampbell/scrapeomat@v0.0.0-20220820232205-23e64141c89e/cmd/slurpserver/main.go (about)

     1  package main
     2  
     3  //go:generate go-bindata templates static
     4  
     5  // run server to provide API and web interface upon a scrapeomat database
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"log"
    11  	"os"
    12  
    13  	"github.com/bcampbell/scrapeomat/store/sqlstore"
    14  	_ "github.com/lib/pq"
    15  	_ "github.com/mattn/go-sqlite3"
    16  )
    17  
    18  var opts struct {
    19  	verbosity int
    20  	driver    string
    21  	connStr   string
    22  	port      int
    23  	prefix    string
    24  	browse    bool
    25  }
    26  
    27  func main() {
    28  	//	flag.IntVar(&opts.verbosity, "v", 1, "verbosity of output (0=errors only 1=info 2=debug)")
    29  	flag.StringVar(&opts.connStr, "db", "", "database connection string (or set SCRAPEOMAT_DB")
    30  	flag.StringVar(&opts.driver, "driver", "", "database driver name (defaults to sqlite3 if SCRAPEOMAT_DRIVER is unset)")
    31  	flag.StringVar(&opts.prefix, "prefix", "", `url prefix (eg "/ukarticles") to allow multiple servers on same port`)
    32  	flag.BoolVar(&opts.browse, "browse", false, `enable html browsing of articles`)
    33  	flag.IntVar(&opts.port, "port", 12345, "port to run server on")
    34  	flag.IntVar(&opts.verbosity, "v", 0, "verbosity (0=errors only, 1=info, 2=debug)")
    35  	flag.Parse()
    36  
    37  	errLog := log.New(os.Stderr, "ERR: ", 0)
    38  	var infoLog Logger
    39  	if opts.verbosity > 0 {
    40  		infoLog = log.New(os.Stderr, "INF: ", 0)
    41  	} else {
    42  		infoLog = nullLogger{}
    43  	}
    44  
    45  	db, err := sqlstore.NewWithEnv(opts.driver, opts.connStr)
    46  	if err != nil {
    47  		fmt.Fprintf(os.Stderr, "ERROR opening db: %s\n", err)
    48  		os.Exit(1)
    49  	}
    50  	defer db.Close()
    51  
    52  	db.ErrLog = errLog
    53  	if opts.verbosity >= 2 {
    54  		db.DebugLog = log.New(os.Stderr, "store: ", 0)
    55  	}
    56  
    57  	// run server
    58  	srv, err := NewServer(db, opts.browse, opts.port, opts.prefix, infoLog, errLog)
    59  	if err != nil {
    60  		fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
    61  		os.Exit(1)
    62  	}
    63  
    64  	err = srv.Run()
    65  	if err != nil {
    66  		fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
    67  		os.Exit(1)
    68  	}
    69  }