Skip to content

Commit

Permalink
major refactor/reorg of code
Browse files Browse the repository at this point in the history
  • Loading branch information
untoldone committed Feb 18, 2015
1 parent 7b2e970 commit 4f37793
Show file tree
Hide file tree
Showing 21 changed files with 249 additions and 221 deletions.
15 changes: 15 additions & 0 deletions api/conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package api

import (
"github.com/gocodo/bloomdb"
)

var bloomConn *bloomdb.BloomDatabase

func Conn() *bloomdb.BloomDatabase {
if bloomConn == nil {
bloomConn = bloomdb.CreateDB()
}

return bloomConn
}
10 changes: 5 additions & 5 deletions lib/params_error.go → api/params_error.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package bloomapi
package api

type paramsError struct {
type ParamsError struct {
Name string `json:"name"`
Message string `json:"message"`
Params map[string]string `json:"parameters"`
}

func (e paramsError) Error() string {
func (e ParamsError) Error() string {
return e.Message
}

func NewParamsError(message string, params map[string]string) paramsError {
return paramsError{
func NewParamsError(message string, params map[string]string) ParamsError {
return ParamsError{
"ParameterError",
message,
params,
Expand Down
4 changes: 2 additions & 2 deletions lib/render.go → api/render.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bloomapi
package api

import (
"net/http"
Expand All @@ -7,7 +7,7 @@ import (

var defaultRenderer = render.New(render.Options{})

func renderJSON(w http.ResponseWriter, req *http.Request, status int, v interface{}) {
func Render(w http.ResponseWriter, req *http.Request, status int, v interface{}) {
vars := req.URL.Query()

if callback, ok := vars["callback"]; ok {
Expand Down
8 changes: 4 additions & 4 deletions bloomapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"fmt"
"github.com/spf13/viper"
"github.com/untoldone/bloomapi/lib"
"github.com/untoldone/bloomapi/cmd"
)

func showUsage() {
Expand Down Expand Up @@ -35,11 +35,11 @@ func main() {

switch arg {
case "server":
bloomapi.Server()
cmd.Server()
case "bootstrap":
bloomapi.Bootstrap()
cmd.Bootstrap()
case "drop":
bloomapi.Drop()
cmd.Drop()
default:
fmt.Println("Invalid command:", arg)
showUsage()
Expand Down
27 changes: 25 additions & 2 deletions bootstrap.sql → cmd/bootstrap.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
CREATE TABLE sources
package cmd

import (
"log"
"github.com/gocodo/bloomdb"
)

func Bootstrap() {
bdb := bloomdb.CreateDB()

conn, err := bdb.SqlConnection()
if err != nil {
log.Fatal(err)
}
defer conn.Close()

_, err = conn.Exec(bootstrapSql)
if err != nil {
log.Fatal(err)
}
}

var bootstrapSql =
`CREATE TABLE sources
(
id uuid,
name character varying(255),
Expand Down Expand Up @@ -31,4 +54,4 @@ CREATE TABLE search_types
last_updated timestamp,
last_checked timestamp,
CONSTRAINT search_types_id_key UNIQUE (id)
);
);`
19 changes: 9 additions & 10 deletions lib/drop.go → cmd/drop.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
package bloomapi
package cmd

import (
"log"
"io/ioutil"
"github.com/gocodo/bloomdb"
)

func Drop() {
bloomdb := bloomdb.CreateDB()

file, err := ioutil.ReadFile("drop.sql")
if err != nil {
log.Fatal("Failed to read file.", err)
}

metaSql := string(file[:])
conn, err := bloomdb.SqlConnection()
if err != nil {
log.Fatal("Failed to get database connection.", err)
}
defer conn.Close()

_, err = conn.Exec(metaSql)
_, err = conn.Exec(dropSql)
if err != nil {
log.Fatal("Failed to create metadata tables.", err)
}
}
}

var dropSql =
`DROP TABLE IF EXISTS sources;
DROP TABLE IF EXISTS source_tables;
DROP TABLE IF EXISTS source_versions;
DROP TABLE IF EXISTS search_types;`
39 changes: 39 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"log"
"github.com/codegangsta/negroni"
"github.com/spf13/viper"
"github.com/gorilla/mux"

"github.com/untoldone/bloomapi/middleware"
"github.com/untoldone/bloomapi/handler"
"github.com/untoldone/bloomapi/handler_compat"
)

func Server() {
port := viper.GetString("bloomapiPort")
n := negroni.Classic()

// Middleware setup
n.Use(middleware.NewAuthentication())

// Router setup
router := mux.NewRouter()

// Current API
router.HandleFunc("/api/sources", handler.SourcesHandler).Methods("GET")
router.HandleFunc("/api/search/{source}", handler.SearchSourceHandler).Methods("GET")
router.HandleFunc("/api/sources/{source}/{id}", handler.ItemHandler).Methods("GET")

// For Backwards Compatibility Feb 13, 2015
router.HandleFunc("/api/search", handler_compat.NpiSearchHandler).Methods("GET")
router.HandleFunc("/api/search/npi", handler_compat.NpiSearchHandler).Methods("GET")
router.HandleFunc("/api/npis/{npi:[0-9]+}", handler_compat.NpiItemHandler).Methods("GET")
router.HandleFunc("/api/sources/npi/{npi:[0-9]+}", handler_compat.NpiItemHandler).Methods("GET")

n.UseHandler(router)

log.Println("Running Server")
n.Run(":" + port)
}
4 changes: 0 additions & 4 deletions drop.sql

This file was deleted.

16 changes: 8 additions & 8 deletions lib/item_handler.go → handler/item_handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bloomapi
package handler

import (
"net/http"
Expand All @@ -7,37 +7,37 @@ import (
"github.com/gorilla/mux"
"github.com/mattbaird/elastigo/lib"
"log"

"github.com/untoldone/bloomapi/api"
)

func ItemHandler (w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
source := strings.ToLower(vars["source"])
id := vars["id"]

conn := bdb.SearchConnection()
conn := api.Conn().SearchConnection()

result, err := conn.Get("source", source, id, nil)
log.Println(err, elastigo.RecordNotFound)
if err != nil && err.Error() == elastigo.RecordNotFound.Error() {
renderJSON(w, req, http.StatusNotFound, "item not found")
api.Render(w, req, http.StatusNotFound, "item not found")
return
} else if err != nil {
log.Println(err)
renderJSON(w, req, http.StatusInternalServerError, "Internal Server Error")
api.Render(w, req, http.StatusInternalServerError, "Internal Server Error")
return
}

var found map[string]interface{}
err = json.Unmarshal(*result.Source, &found)
if err != nil {
log.Println(err)
renderJSON(w, req, http.StatusInternalServerError, "Internal Server Error")
api.Render(w, req, http.StatusInternalServerError, "Internal Server Error")
return
}

body := map[string]interface{} { "result": found }
keysToStrings(body)

renderJSON(w, req, http.StatusOK, body)
api.Render(w, req, http.StatusOK, body)
return
}
Loading

0 comments on commit 4f37793

Please sign in to comment.