Housekeeping
Move docs and commands out into their own files
This commit is contained in:
parent
b04eb8a04a
commit
73a1e1fd21
5 changed files with 3 additions and 3 deletions
35
cmd/rove-server/main.go
Normal file
35
cmd/rove-server/main.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
fmt.Println("Initialising...")
|
||||
|
||||
// Set up the close handler
|
||||
c := make(chan os.Signal)
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
go func() {
|
||||
<-c
|
||||
fmt.Println("SIGTERM recieved, exiting...")
|
||||
os.Exit(0)
|
||||
}()
|
||||
|
||||
// Create a new router
|
||||
router := NewRouter()
|
||||
|
||||
fmt.Println("Initialised")
|
||||
|
||||
// Listen and serve the http requests
|
||||
fmt.Println("Serving HTTP")
|
||||
if err := http.ListenAndServe(":80", router); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
32
cmd/rove-server/router.go
Normal file
32
cmd/rove-server/router.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func NewRouter() (router *mux.Router) {
|
||||
// Create a new router
|
||||
router = mux.NewRouter().StrictSlash(true)
|
||||
|
||||
// Set up the handlers
|
||||
router.HandleFunc("/status", HandleStatus)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// RouterStatus is a struct that contains information on the status of the server
|
||||
type RouterStatus struct {
|
||||
Ready bool `json:"ready"`
|
||||
}
|
||||
|
||||
// HandleStatus handles HTTP requests to the /status endpoint
|
||||
func HandleStatus(w http.ResponseWriter, r *http.Request) {
|
||||
var status = RouterStatus{
|
||||
Ready: true,
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(status)
|
||||
}
|
22
cmd/rove-server/router_test.go
Normal file
22
cmd/rove-server/router_test.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHandleStatus(t *testing.T) {
|
||||
request, _ := http.NewRequest(http.MethodGet, "/status", nil)
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
HandleStatus(response, request)
|
||||
|
||||
var status RouterStatus
|
||||
json.NewDecoder(response.Body).Decode(&status)
|
||||
|
||||
if status.Ready != true {
|
||||
t.Errorf("got false for /status")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue