Skip to content

Commit

Permalink
config: use global config + random cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Rooth committed Aug 19, 2014
1 parent d91c8b8 commit 0dcc79e
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/Godeps/_workspace/github.com/coreos-inc/bridge
/frontend/public/bower_components
/frontend/public/dist
/config.ini
9 changes: 3 additions & 6 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ var (
k8proxy *proxy.K8Proxy
)

func init() {
// TODO(sym3tri): move to config
p, err := proxy.NewK8Proxy("http://localhost:9909/api/v1beta1", "api/bridge/v1")
func Setup(r *mux.Router) {
var err error
k8proxy, err = proxy.NewK8Proxy("api/bridge/v1")
if err != nil {
panic("failed to initialize k8 proxy")
}
k8proxy = p
}

func Setup(r *mux.Router) {
basePath := "/" + path.Join("api", Name, Version)
log.Printf("subrouter basePath=%s", basePath)

Expand Down
2 changes: 0 additions & 2 deletions api/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func controllerGet(w http.ResponseWriter, r *http.Request) {
if err != nil {
w.WriteHeader(http.StatusBadGateway)
}
//http.ServeFile(w, r, "api/mock/controller.json")
}

// List Controllers api endpoint.
Expand All @@ -26,5 +25,4 @@ func controllerList(w http.ResponseWriter, r *http.Request) {
if err != nil {
w.WriteHeader(http.StatusBadGateway)
}
//http.ServeFile(w, r, "api/mock/controller-list.json")
}
2 changes: 0 additions & 2 deletions api/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func podGet(w http.ResponseWriter, r *http.Request) {
if err != nil {
w.WriteHeader(http.StatusBadGateway)
}
//http.ServeFile(w, r, "api/mock/pod.json")
}

// List Pods api endpoint.
Expand All @@ -26,5 +25,4 @@ func podList(w http.ResponseWriter, r *http.Request) {
if err != nil {
w.WriteHeader(http.StatusBadGateway)
}
//http.ServeFile(w, r, "api/mock/pod-list.json")
}
2 changes: 0 additions & 2 deletions api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func serviceGet(w http.ResponseWriter, r *http.Request) {
if err != nil {
w.WriteHeader(http.StatusBadGateway)
}
//http.ServeFile(w, r, "api/mock/service-list.json")
}

// List Services api endpoint.
Expand All @@ -26,5 +25,4 @@ func serviceList(w http.ResponseWriter, r *http.Request) {
if err != nil {
w.WriteHeader(http.StatusBadGateway)
}
//http.ServeFile(w, r, "api/mock/service.json")
}
3 changes: 3 additions & 0 deletions config.ini.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public-dir = ./frontend/public
address = 0.0.0.0:9000
k8-url = http://localhost:8080
18 changes: 18 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package config

import (
"github.com/coreos-inc/bridge/Godeps/_workspace/src/github.com/rakyll/globalconf"
)

func Init() error {
conf, err := globalconf.NewWithOptions(&globalconf.Options{
Filename: "./config.ini",
EnvPrefix: "BRIDGE_",
})
if err != nil {
return err
}

conf.ParseAll()
return nil
}
19 changes: 19 additions & 0 deletions config/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

import (
"flag"
)

var (
PublicDir = flag.String("public-dir",
"./frontend/public",
"Directory containing static web assets.")

Address = flag.String("address",
"0.0.0.0:9090",
"Address and port to listen on.")

K8Url = flag.String("k8-url",
"http://localhost:8080",
"Url of the kubernetes api server.")
)
6 changes: 6 additions & 0 deletions dev/dev.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package main

import (
"log"

"github.com/coreos-inc/bridge/Godeps/_workspace/src/github.com/coreos/devweb/slave"

"github.com/coreos-inc/bridge/config"
"github.com/coreos-inc/bridge/server"
)

func main() {
if err := config.Init(); err != nil {
log.Fatal(err)
}
server.Handle()
slave.Main()
}
2 changes: 1 addition & 1 deletion devweb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
source build

go install github.com/coreos/devweb
PATH=$PATH:$PWD ./bin/devweb -addr :9000 github.com/coreos-inc/bridge/dev
PATH=$PATH:$PWD ./bin/devweb -addr :9090 github.com/coreos-inc/bridge/dev
17 changes: 8 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"log"
"net/http"
"os"

"github.com/coreos-inc/bridge/config"
"github.com/coreos-inc/bridge/server"
)

Expand All @@ -13,15 +13,14 @@ var (
)

func main() {
server.Handle()

listenAddress = os.Getenv("ADDRESS")
if listenAddress == "" {
listenAddress = "0.0.0.0:9000"
if err := config.Init(); err != nil {
log.Fatal(err)
}

log.Printf("listening on: %s", listenAddress)
if err := http.ListenAndServe(listenAddress, nil); err != nil {
log.Fatal("error on ListenAndServe: %s", err)
server.Handle()

log.Printf("listening on: %s", *config.Address)
if err := http.ListenAndServe(*config.Address, nil); err != nil {
log.Fatal(err)
}
}
7 changes: 5 additions & 2 deletions proxy/k8proxy.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package proxy

import (
"fmt"
"net/http"
"net/url"

"github.com/coreos-inc/bridge/config"
)

const (
Expand All @@ -13,8 +16,8 @@ type K8Proxy struct {
baseProxy
}

func NewK8Proxy(apiUrl, localPrefix string) (*K8Proxy, error) {
remoteUrl, err := url.Parse(apiUrl)
func NewK8Proxy(localPrefix string) (*K8Proxy, error) {
remoteUrl, err := url.Parse(fmt.Sprintf("%s/api/v1beta1", *config.K8Url))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion schema/v1-gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var _ = strings.Replace
const apiId = "bridge:v1"
const apiName = "schema"
const apiVersion = "v1"
const basePath = "http://localhost:9000/api/bridge/v1/"
const basePath = "http://localhost:9090/api/bridge/v1/"

func New(client *http.Client) (*Service, error) {
if client == nil {
Expand Down
2 changes: 1 addition & 1 deletion schema/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"x32": ""
},
"labels": [],
"rootUrl": "http://localhost:9000",
"rootUrl": "http://localhost:9090",
"servicePath": "/api/bridge/v1/",
"batchPath": "batch",
"parameters": {},
Expand Down
19 changes: 6 additions & 13 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,36 @@ import (
"github.com/coreos-inc/bridge/Godeps/_workspace/src/github.com/gorilla/mux"

"github.com/coreos-inc/bridge/api"
"github.com/coreos-inc/bridge/config"
)

const (
staticPrefix = "/static"
)

var (
publicDir string
indexTemplate *template.Template
)

// Serve the front-end index page.
func IndexHandler(w http.ResponseWriter, r *http.Request) {
// TODO (sym3tri): config option to cache template.
indexTemplate = template.Must(template.ParseFiles(path.Join(publicDir, "index.html")))
indexTemplate = template.Must(template.ParseFiles(path.Join(*config.PublicDir, "index.html")))
if err := indexTemplate.ExecuteTemplate(w, "index.html", nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func init() {
publicDir = os.Getenv("PUBLIC_DIR")
if publicDir == "" {
publicDir = "./frontend/public"
}

if _, err := os.Stat(path.Join(publicDir, "index.html")); err != nil {
fmt.Println("Static files do not exist in provided PUBLIC_DIR env variable.")
func Handle() {
if _, err := os.Stat(path.Join(*config.PublicDir, "index.html")); err != nil {
fmt.Println("index.html not found in configured public-dir")
os.Exit(1)
}
}

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

// Simple static file server for requests containing static prefix.
r.PathPrefix(staticPrefix).Handler(http.StripPrefix(staticPrefix, http.FileServer(http.Dir(publicDir))))
r.PathPrefix(staticPrefix).Handler(http.StripPrefix(staticPrefix, http.FileServer(http.Dir(*config.PublicDir))))

// Endpoints for API XHR requests.
api.Setup(r)
Expand Down

0 comments on commit 0dcc79e

Please sign in to comment.