can serve (hackish)

This commit is contained in:
Matthew Dillon 2015-01-05 11:50:42 -09:00
parent 2f3aa58af6
commit 28f29f3d68
2 changed files with 30 additions and 103 deletions

View file

@ -2,9 +2,11 @@ package api
import ( import (
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"path/filepath"
"github.com/dgrijalva/jwt-go" "github.com/dgrijalva/jwt-go"
) )
@ -26,12 +28,14 @@ var (
func init() { func init() {
var err error var err error
dir, _ := filepath.Abs(filepath.Dir(privKeyPath))
fmt.Println(dir)
signKey, err = ioutil.ReadFile(privKeyPath) signKey, err = ioutil.ReadFile(privKeyPath)
if err != nil { if err != nil {
// Before exploding, check up one level... // Before exploding, check up one level...
signKey, err = ioutil.ReadFile("../" + privKeyPath) signKey, err = ioutil.ReadFile("../../" + privKeyPath)
if err != nil { if err != nil {
log.Fatalf("Error reading private key: ", err) log.Fatalf("Error reading private key: ", err)
return return
@ -41,7 +45,7 @@ func init() {
verifyKey, err = ioutil.ReadFile(pubKeyPath) verifyKey, err = ioutil.ReadFile(pubKeyPath)
if err != nil { if err != nil {
// Before exploding, check up one level... // Before exploding, check up one level...
verifyKey, err = ioutil.ReadFile("../" + pubKeyPath) verifyKey, err = ioutil.ReadFile("../../" + pubKeyPath)
if err != nil { if err != nil {
log.Fatalf("Error reading public key: ", err) log.Fatalf("Error reading public key: ", err)
return return

View file

@ -1,126 +1,49 @@
package main package main
import ( import (
"flag"
"fmt"
"log" "log"
"net/http" "net/http"
"os" "os"
"github.com/codegangsta/cli"
"github.com/thermokarst/bactdb/api" "github.com/thermokarst/bactdb/api"
"github.com/thermokarst/bactdb/datastore" "github.com/thermokarst/bactdb/datastore"
) )
func init() {
flag.Usage = func() {
fmt.Fprintln(os.Stderr, `bactdb is a database for bacteria.
Usage:
bactdb [options] command [arg...]
The commands are:
`)
for _, c := range subcmds {
fmt.Fprintf(os.Stderr, " %-24s %s\n", c.name, c.description)
}
fmt.Fprintln(os.Stderr, `
Use "bactdb command -h" for more information about a command.
The options are:
`)
flag.PrintDefaults()
os.Exit(1)
}
}
func main() { func main() {
flag.Parse() app := cli.NewApp()
app.Name = "bactdb"
app.Usage = "a database for bacteria"
if flag.NArg() == 0 { app.Commands = []cli.Command{
flag.Usage() {
} Name: "serve",
log.SetFlags(0) ShortName: "s",
Usage: "Start web server",
subcmd := flag.Arg(0) Flags: []cli.Flag{
for _, c := range subcmds { cli.StringFlag{
if c.name == subcmd { Name: "http",
c.run(flag.Args()[1:]) Value: ":8901",
return Usage: "HTTP service address",
} },
},
Action: cmdServe,
},
} }
fmt.Fprintf(os.Stderr, "unknown subcmd %q\n", subcmd) app.Run(os.Args)
fmt.Fprintln(os.Stderr, `Run "bactdb -h" for usage.`)
os.Exit(1)
} }
type subcmd struct { func cmdServe(c *cli.Context) {
name string httpAddr := c.String("http")
description string
run func(args []string)
}
var subcmds = []subcmd{
{"serve", "start web server", serveCmd},
{"createdb", "create the database schema", createDBCmd},
}
func serveCmd(args []string) {
fs := flag.NewFlagSet("serve", flag.ExitOnError)
httpAddr := flag.String("http", ":8901", "HTTP service address")
fs.Usage = func() {
fmt.Fprintln(os.Stderr, `usage: bactdb serve [options]
Starts the web server that serves the API.
The options are:
`)
fs.PrintDefaults()
os.Exit(1)
}
fs.Parse(args)
if fs.NArg() != 0 {
fs.Usage()
}
datastore.Connect() datastore.Connect()
m := http.NewServeMux() m := http.NewServeMux()
m.Handle("/api/", http.StripPrefix("/api", api.Handler())) m.Handle("/api/", http.StripPrefix("/api", api.Handler()))
log.Print("Listening on ", httpAddr)
log.Print("Listening on ", *httpAddr) err := http.ListenAndServe(httpAddr, m)
err := http.ListenAndServe(*httpAddr, m)
if err != nil { if err != nil {
log.Fatal("ListenAndServe:", err) log.Fatal("ListenAndServe: ", err)
} }
} }
func createDBCmd(args []string) {
fs := flag.NewFlagSet("createdb", flag.ExitOnError)
drop := fs.Bool("drop", false, "drop DB before creating")
fs.Usage = func() {
fmt.Fprintln(os.Stderr, `usage: bactdb createdb [options]
Creates the necessary DB schema.
The options are:
`)
fs.PrintDefaults()
os.Exit(1)
}
fs.Parse(args)
if fs.NArg() != 0 {
fs.Usage()
}
datastore.Connect()
migrationsPath := "./datastore/migrations"
if *drop {
datastore.Drop(migrationsPath)
}
datastore.Create(migrationsPath)
}