-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from adambaumeister/api
Api
- Loading branch information
Showing
11 changed files
with
213 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,49 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/adambaumeister/goflow/config" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
type API struct { | ||
c chan string | ||
c chan string | ||
config *config.GlobalConfig | ||
} | ||
|
||
func Start() { | ||
type JsonMessage struct { | ||
Msg string | ||
} | ||
|
||
func Start(gc *config.GlobalConfig) { | ||
a := API{} | ||
a.config = gc | ||
|
||
http.HandleFunc("/", a.getHandler) | ||
http.HandleFunc("/test", a.Test) | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
http.HandleFunc("/status", a.Test) | ||
log.Fatal(http.ListenAndServe(a.config.Api, nil)) | ||
|
||
} | ||
|
||
func (a *API) getHandler(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintf(w, "API works!") | ||
} | ||
|
||
func (a *API) Test(w http.ResponseWriter, r *http.Request) { | ||
TestNFFrontend() | ||
var s string | ||
b := a.config.GetBackends() | ||
for _, be := range b { | ||
s = s + be.Test() + "\n" | ||
} | ||
jm := JsonMessage{ | ||
Msg: s, | ||
} | ||
j, err := json.Marshal(jm) | ||
if err != nil { | ||
fmt.Println("error:", err) | ||
} | ||
|
||
w.Write(j) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
type Commands struct { | ||
Paths map[string]Path | ||
HelpString string | ||
} | ||
|
||
type Path interface { | ||
Get() | ||
Help() string | ||
} | ||
|
||
func (c *Commands) Get() { | ||
s := "Goflow command line client help\n------------------\n\n" | ||
for cmd, p := range c.Paths { | ||
s = s + fmt.Sprintf("%v : %v\n", cmd, p.Help()) | ||
} | ||
fmt.Printf(s) | ||
} | ||
func (c *Commands) Help() string { | ||
return "Display this help message." | ||
} | ||
|
||
/* | ||
HTTP Path do a call to the API to present their results | ||
*/ | ||
type HttpPath struct { | ||
Url string | ||
Data []byte | ||
HelpString string | ||
} | ||
|
||
func (p *HttpPath) Get() { | ||
resp, err := http.Get("http://127.0.0.1:8880" + p.Url) | ||
if err != nil { | ||
panic(err) | ||
} | ||
jm := JsonMessage{} | ||
body, _ := ioutil.ReadAll(resp.Body) | ||
json.Unmarshal(body, &jm) | ||
fmt.Printf("%v\n", jm.Msg) | ||
} | ||
|
||
func (p *HttpPath) Help() string { | ||
return p.HelpString | ||
} | ||
|
||
func (c *Commands) Parse() { | ||
c.Paths = make(map[string]Path) | ||
|
||
// Setup the routes | ||
testPath := HttpPath{ | ||
Url: "/status", | ||
HelpString: "Displays the status of configured Backends", | ||
} | ||
|
||
c.Paths["status"] = &testPath | ||
c.Paths["help"] = c | ||
if len(os.Args) > 1 { | ||
c.Paths[os.Args[1]].Get() | ||
os.Exit(0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.