Skip to content

Commit

Permalink
add etc_registry support
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnest committed Sep 12, 2016
1 parent 9e1d0df commit 925aaf1
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 26 deletions.
Binary file removed config
Binary file not shown.
2 changes: 2 additions & 0 deletions config.go
Expand Up @@ -27,6 +27,8 @@ func loadConfig() {

if serverConfig.RegistryType == "zookeeper" {
reg = &ZooKeeperRegistry{}
} else if serverConfig.RegistryType == "etcd" {
reg = &EtcdRegistry{}
}

if reg != nil {
Expand Down
16 changes: 8 additions & 8 deletions config.json
@@ -1,9 +1,9 @@
{
"registry_type": "zookeeper",
"registry_url": "localhost:2181",
"service_base_url":"/rpcx",
"host": "0.0.0.0",
"port": 8972,
"user": "admin",
"password": "admin"
}
"registry_type": "etcd",
"registry_url": "http://127.0.0.1:2379",
"service_base_url": "/rpcx",
"host": "0.0.0.0",
"port": 8972,
"user": "admin",
"password": "admin"
}
9 changes: 9 additions & 0 deletions config_zk.json
@@ -0,0 +1,9 @@
{
"registry_type": "zookeeper",
"registry_url": "localhost:2181",
"service_base_url":"/rpcx",
"host": "0.0.0.0",
"port": 8972,
"user": "admin",
"password": "admin"
}
12 changes: 8 additions & 4 deletions etcd_service.go
Expand Up @@ -37,14 +37,18 @@ func (r *EtcdRegistry) fetchServices() []*Service {
for _, n := range resp.Node.Nodes {
for _, ep := range n.Nodes {

serviceName := strings.TrimPrefix(n.Key, serverConfig.ServiceBaseURL+"/")
serviceAddr := strings.TrimPrefix(ep.Key, n.Key+"/")
v, err := url.ParseQuery(ep.Value)
state := "n/a"
if err == nil && v.Get("state") != "" {
if err == nil {
state = v.Get("state")
if state == "" {
state = "active"
}
}

id := base64.StdEncoding.EncodeToString([]byte(n.Key + "@" + ep.Key))
service := &Service{Id: id, Name: n.Key, Address: ep.Key, Metadata: ep.Value, State: state}
id := base64.StdEncoding.EncodeToString([]byte(serviceName + "@" + serviceAddr))
service := &Service{Id: id, Name: serviceName, Address: serviceAddr, Metadata: ep.Value, State: state}
services = append(services, service)
}
}
Expand Down
69 changes: 61 additions & 8 deletions server.go
Expand Up @@ -2,8 +2,11 @@ package main

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"path/filepath"
Expand Down Expand Up @@ -42,7 +45,7 @@ func init() {
}
}

func renderTemplate(w http.ResponseWriter, name string, data map[string]interface{}) error {
func renderTemplate(w http.ResponseWriter, name string, data interface{}) error {
// Ensure the template exists in the map.
tmpl, ok := templates[name]
if !ok {
Expand All @@ -57,18 +60,40 @@ func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/services", http.StatusFound)
})
http.HandleFunc("/services", servicesHandler)
http.HandleFunc("/s/deactivate/", deactivateHandler)
http.HandleFunc("/s/activate/", activateHandler)
http.HandleFunc("/s/m/", modifyHandler)
http.HandleFunc("/hosts", hostsHandler)
http.HandleFunc("/services", recoverWrapper(servicesHandler))
http.HandleFunc("/s/deactivate/", recoverWrapper(deactivateHandler))
http.HandleFunc("/s/activate/", recoverWrapper(activateHandler))
http.HandleFunc("/s/m/", recoverWrapper(modifyHandler))
http.HandleFunc("/registry", recoverWrapper(registryHandler))

fs := http.FileServer(http.Dir("web"))
http.Handle("/static/", http.StripPrefix("/static/", fs))

http.ListenAndServe(serverConfig.Host+":"+strconv.Itoa(serverConfig.Port), nil)
}

func recoverWrapper(h func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if re := recover(); re != nil {
var err error
fmt.Println("Recovered in registryHandler", re)
switch t := re.(type) {
case string:
err = errors.New(t)
case error:
err = t
default:
err = errors.New("Unknown error")
}
w.WriteHeader(http.StatusOK)
renderTemplate(w, "error.html", err.Error())
}
}()
h(w, r)
}
}

func servicesHandler(w http.ResponseWriter, r *http.Request) {
data := make(map[string]interface{})
data["services"] = reg.fetchServices()
Expand Down Expand Up @@ -121,9 +146,37 @@ func modifyHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/services", http.StatusFound)
}

func hostsHandler(w http.ResponseWriter, r *http.Request) {
func registryHandler(w http.ResponseWriter, r *http.Request) {
oldConfig := serverConfig
defer func() {
if re := recover(); re != nil {
bytes, err := json.MarshalIndent(&oldConfig, "", "\t")
if err == nil {
err = ioutil.WriteFile("./config.json", bytes, 0644)
loadConfig()
}

panic(re)
}
}()

if r.Method == "POST" {
registryType := r.FormValue("registry_type")
registryURL := r.FormValue("registry_url")
basePath := r.FormValue("base_path")

serverConfig.RegistryType = registryType
serverConfig.RegistryURL = registryURL
serverConfig.ServiceBaseURL = basePath

bytes, err := json.MarshalIndent(&serverConfig, "", "\t")
if err == nil {
err = ioutil.WriteFile("./config.json", bytes, 0644)
loadConfig()
}
}

renderTemplate(w, r.URL.Path[1:]+".html", nil)
renderTemplate(w, r.URL.Path[1:]+".html", serverConfig)
}

type Registry interface {
Expand Down
9 changes: 7 additions & 2 deletions templates/bases/hosts.html → templates/bases/error.html
Expand Up @@ -13,13 +13,18 @@
<div class="container">

<div class="row">
<div class="col-lg-12 text-center">
<h1>Host list</h1>
<div class="col-lg-12">
<h1>Error</h1>
<hr>
<p>
{{.}}
</p>
</div>
</div>

</div>

{{template "footer"}}
</body>

</html>
76 changes: 76 additions & 0 deletions templates/bases/registry.html
@@ -0,0 +1,76 @@
<!DOCTYPE html>
<html>

<head>
{{template "header"}}
</head>

<body>

{{template "nav"}}

<!-- Page Content -->
<div class="container">

<div class="row">
<div class="col-lg-12">
<h1>Registry</h1>
<hr>
<form class="form-horizontal" method="POST" action="/registry">
<div class="form-group">
<label for="registry_type" class="col-sm-2 control-label">Registry Type</label>
<div class="col-sm-6">
<select class="form-control" id="registry_type" name="registry_type">
{{if eq .RegistryType "zookeeper"}}
<option selected="selected">zookeeper</option>
{{else}}
<option>zookeeper</option>
{{end}}
{{if eq .RegistryType "etcd"}}
<option selected="selected">etcd</option>
{{else}}
<option>etcd</option>
{{end}}
</select>
</div>
</div>
<div class="form-group">
<label for="registry_url" class="col-sm-2 control-label">Registy_URL</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="registry_url" name="registry_url" value="{{.RegistryURL}}">
</div>
</div>
<div class="form-group">
<label for="base_path" class="col-sm-2 control-label">Base Path</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="base_path" name="base_path" value="{{.ServiceBaseURL}}">
</div>
</div>
<div class="form-group">
<label for="host" class="col-sm-2 control-label">Host</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="host" name="host" value="{{.Host}}" disabled>
</div>
</div>
<div class="form-group">
<label for="port" class="col-sm-2 control-label">Port</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="port" name="port" value="{{.Port}}" disabled>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>

</div>
</div>

</div>

{{template "footer"}}
</body>

</html>
1 change: 1 addition & 0 deletions templates/bases/services.html
Expand Up @@ -16,6 +16,7 @@
<div class="row">
<div class="col-lg-12 text-center">
<h1>Services list</h1>
<hr>
<table id="servicesTable" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
<thead>
<tr>
Expand Down
6 changes: 3 additions & 3 deletions templates/includes/nav.html
Expand Up @@ -15,9 +15,9 @@
<li>
<a href="/services">Services</a>
</li>
<!--<li>
<a href="/hosts">Hosts</a>
</li>-->
<li>
<a href="/registry">Registry</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
Expand Down
5 changes: 4 additions & 1 deletion zookeeper_service.go
Expand Up @@ -53,8 +53,11 @@ func (r *ZooKeeperRegistry) fetchServices() []*Service {

v, err := url.ParseQuery(metadata)
state := "n/a"
if err == nil && v.Get("state") != "" {
if err == nil {
state = v.Get("state")
if state == "" {
state = "active"
}
}

id := base64.StdEncoding.EncodeToString([]byte(s + "@" + ep))
Expand Down

0 comments on commit 925aaf1

Please sign in to comment.