Skip to content

Commit

Permalink
- Refactor Websever to martini to handle unescaped app id in API
Browse files Browse the repository at this point in the history
- PUT allow creating missing app configuration
  • Loading branch information
j1n6 committed Apr 18, 2015
1 parent c60fdc5 commit f0b7e3c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
17 changes: 9 additions & 8 deletions api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"errors"
"io/ioutil"
"net/http"
"net/url"

"github.com/QubitProducts/bamboo/Godeps/_workspace/src/github.com/go-martini/martini"
zk "github.com/QubitProducts/bamboo/Godeps/_workspace/src/github.com/samuel/go-zookeeper/zk"
"github.com/QubitProducts/bamboo/Godeps/_workspace/src/github.com/zenazn/goji/web"
conf "github.com/QubitProducts/bamboo/configuration"
"github.com/QubitProducts/bamboo/services/service"
)
Expand Down Expand Up @@ -46,15 +45,18 @@ func (d *ServiceAPI) Create(w http.ResponseWriter, r *http.Request) {
responseJSON(w, serviceModel)
}

func (d *ServiceAPI) Put(c web.C, w http.ResponseWriter, r *http.Request) {
identifier, _ := url.QueryUnescape(c.URLParams["id"])
func (d *ServiceAPI) Put(params martini.Params, w http.ResponseWriter, r *http.Request) {

identity := params["_1"]
println(identity)

serviceModel, err := extractServiceModel(r)
if err != nil {
responseError(w, err.Error())
return
}

_, err1 := service.Put(d.Zookeeper, d.Config.Bamboo.Zookeeper, identifier, serviceModel.Acl)
_, err1 := service.Put(d.Zookeeper, d.Config.Bamboo.Zookeeper, identity, serviceModel.Acl)
if err1 != nil {
responseError(w, err1.Error())
return
Expand All @@ -63,9 +65,8 @@ func (d *ServiceAPI) Put(c web.C, w http.ResponseWriter, r *http.Request) {
responseJSON(w, serviceModel)
}

func (d *ServiceAPI) Delete(c web.C, w http.ResponseWriter, r *http.Request) {
identifier, _ := url.QueryUnescape(c.URLParams["id"])
err := service.Delete(d.Zookeeper, d.Config.Bamboo.Zookeeper, identifier)
func (d *ServiceAPI) Delete(params martini.Params, w http.ResponseWriter, r *http.Request) {
err := service.Delete(d.Zookeeper, d.Config.Bamboo.Zookeeper, params["_1"])
if err != nil {
responseError(w, err.Error())
return
Expand Down
48 changes: 32 additions & 16 deletions main/bamboo/bamboo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
"syscall"
"time"

"github.com/QubitProducts/bamboo/Godeps/_workspace/src/github.com/go-martini/martini"
"github.com/QubitProducts/bamboo/Godeps/_workspace/src/github.com/kardianos/osext"
"github.com/QubitProducts/bamboo/Godeps/_workspace/src/github.com/natefinch/lumberjack"
"github.com/QubitProducts/bamboo/Godeps/_workspace/src/github.com/samuel/go-zookeeper/zk"
"github.com/QubitProducts/bamboo/Godeps/_workspace/src/github.com/zenazn/goji"
"github.com/QubitProducts/bamboo/api"
"github.com/QubitProducts/bamboo/configuration"
"github.com/QubitProducts/bamboo/qzk"
Expand Down Expand Up @@ -69,7 +69,10 @@ func main() {
handlers := event_bus.Handlers{Conf: &conf, Zookeeper: zkConn}
eventBus.Register(handlers.MarathonEventHandler)
eventBus.Register(handlers.ServiceEventHandler)
eventBus.Publish(event_bus.MarathonEvent { EventType: "bamboo_startup", Timestamp: time.Now().Format(time.RFC3339) })
eventBus.Publish(event_bus.MarathonEvent{EventType: "bamboo_startup", Timestamp: time.Now().Format(time.RFC3339)})

// Handle gracefully exit
registerOSSignals()

// Start server
initServer(&conf, zkConn, eventBus)
Expand All @@ -82,24 +85,26 @@ func initServer(conf *configuration.Configuration, conn *zk.Conn, eventBus *even

conf.StatsD.Increment(1.0, "restart", 1)
// Status live information
goji.Get("/status", api.HandleStatus)

// State API
goji.Get("/api/state", stateAPI.Get)

// Service API
goji.Get("/api/services", serviceAPI.All)
goji.Post("/api/services", serviceAPI.Create)
goji.Put("/api/services/:id", serviceAPI.Put)
goji.Delete("/api/services/:id", serviceAPI.Delete)
goji.Post("/api/marathon/event_callback", eventSubAPI.Callback)
router := martini.Classic()
router.Get("/status", api.HandleStatus)

// API
router.Group("/api", func(api martini.Router) {
// State API
api.Get("/state", stateAPI.Get)
// Service API
api.Get("/services", serviceAPI.All)
api.Post("/services", serviceAPI.Create)
api.Put("/services/**", serviceAPI.Put)
api.Delete("/services/**", serviceAPI.Delete)
api.Post("/marathon/event_callback", eventSubAPI.Callback)
})

// Static pages
goji.Get("/*", http.FileServer(http.Dir(path.Join(executableFolder(), "webapp"))))
router.Use(martini.Static(path.Join(executableFolder(), "webapp")))

registerMarathonEvent(conf)

goji.Serve()
router.RunOnAddr(":8000")
}

// Get current executable folder path
Expand Down Expand Up @@ -176,3 +181,14 @@ func configureLog() {
}, os.Stdout))
}
}

func registerOSSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for _ = range c {
log.Println("Server Stopped")
os.Exit(0)
}
}()
}
2 changes: 1 addition & 1 deletion services/event_bus/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func handleHAPUpdate(conf *configuration.Configuration, conn *zk.Conn) bool {

err = execCommand(conf.HAProxy.ReloadCommand)
if err != nil {
log.Fatalf("HAProxy: update failed\n")
log.Println("HAProxy: update failed\n")
} else {
conf.StatsD.Increment(1.0, "reload.marathon", 1)
log.Println("HAProxy: Configuration updated")
Expand Down
7 changes: 6 additions & 1 deletion services/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,16 @@ func Create(conn *zk.Conn, zkConf conf.Zookeeper, appId string, domainValue stri

func Put(conn *zk.Conn, zkConf conf.Zookeeper, appId string, domainValue string) (*zk.Stat, error) {
path := concatPath(zkConf.Path, appId)
stats, err := conn.Set(path, []byte(domainValue), -1)
err := ensurePathExists(conn, path)
if err != nil {
return nil, err
}

stats, err := conn.Set(path, []byte(domainValue), -1)
if err != nil {
return nil, err
}

// Force triger an event on parent
conn.Set(zkConf.Path, []byte{}, -1)

Expand Down

0 comments on commit f0b7e3c

Please sign in to comment.