Skip to content

Commit

Permalink
API new implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdiba committed Sep 27, 2015
1 parent 8046273 commit abba1b1
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 73 deletions.
128 changes: 128 additions & 0 deletions src/web-api/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package main

import (
"github.com/streadway/amqp"
"log"
"net/http"
)

func ScanHandler(w http.ResponseWriter, r *http.Request) {

var (
status int
err error
)

defer func() {
if nil != err {
http.Error(w, err.Error(), status)
}
}()

domain := r.FormValue("target")

if validateDomain(domain) {

conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
if err != nil {
log.Fatal(err)
}
defer conn.Close()

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

status = http.StatusOK

log.Println("Publishing", domain)
err = ch.Publish(
"amq.direct", // exchange
"scan_ready", // routing key
false, // mandatory
false,
amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: "text/plain",
Body: []byte(domain),
})

} else {
status = http.StatusBadRequest
return
}

}

func ResultHandler(w http.ResponseWriter, r *http.Request) {

var (
status int
err error
)

defer func() {
if nil != err {
http.Error(w, err.Error(), status)
}
}()

domain := r.FormValue("target")

if validateDomain(domain) {

status = http.StatusOK

} else {
status = http.StatusBadRequest
return
}

}

func CertificateHandler(w http.ResponseWriter, r *http.Request) {

var (
status int
err error
)

defer func() {
if nil != err {
http.Error(w, err.Error(), status)
}
}()

domain := r.FormValue("target")

if validateDomain(domain) {

raw := r.FormValue("raw")

rawCert := false

if raw == "true" {
rawCert = true
}

log.Println("rawCert:", rawCert)

status = http.StatusOK

} else {
status = http.StatusBadRequest
return
}

}

func validateDomain(domain string) bool {

// TODO
// Need to validate the domain, in a way,
// before passing it to the retriever queue

return true
}
25 changes: 25 additions & 0 deletions src/web-api/router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"github.com/gorilla/mux"
"net/http"
)

func NewRouter() *mux.Router {

router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
var handler http.Handler

handler = route.HandlerFunc

router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(handler)

}

return router
}
33 changes: 33 additions & 0 deletions src/web-api/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import "net/http"

type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}

type Routes []Route

var routes = Routes{
Route{
"Scan",
"POST",
"/api/v1/scan",
ScanHandler,
},
Route{
"Results",
"GET",
"/api/v1/results",
ResultHandler,
},
Route{
"Certificate",
"GET",
"/api/v1/certificate",
CertificateHandler,
},
}
76 changes: 3 additions & 73 deletions src/web-api/web-api.go
Original file line number Diff line number Diff line change
@@ -1,86 +1,16 @@
package main

import (
"github.com/gorilla/mux"
"github.com/streadway/amqp"
"log"
"net/http"
"fmt"
)

func main() {
r := mux.NewRouter()

// try to filter files to download, example only
r.HandleFunc("/website/{domain}", DownloadHandler)

//-
http.Handle("/", r)
router := NewRouter()

// wait for clients
http.ListenAndServe(":8083", nil)
}
err := http.ListenAndServe(":8083", router)

func failOnError(err error, msg string) {
if err != nil {
log.Fatalf("%s: %s", msg, err)
panic(fmt.Sprintf("%s: %s", msg, err))
}
log.Fatal(err)
}

func DownloadHandler(res http.ResponseWriter, req *http.Request) {

var (
status int
err error
)

defer func() {
if nil != err {
http.Error(res, err.Error(), status)
}
}()

vars := mux.Vars(req)
domain := vars["domain"]

fmt.Print(domain)

if validateDomain(domain){

conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
failOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()

ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer ch.Close()

status = http.StatusOK
err = ch.Publish(
"", // exchange
"scan_ready_queue", // routing key
false, // mandatory
false,
amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: "text/plain",
Body: []byte(domain),
})


}else{
status = http.StatusBadRequest
return
}

}

func validateDomain( domain string ) ( bool ){

// TODO
// Need to validate the domain, in a way,
// before passing it to the retriever queue

return true
}

0 comments on commit abba1b1

Please sign in to comment.