rest

package
v0.0.0-...-ced4f1f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2019 License: BSD-2-Clause Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ServeHome

func ServeHome(w http.ResponseWriter, r *http.Request)

ServeHome serves the home page file handler.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client maintains a mapping to the server, sites and websocket connection.

func (*Client) Broadcast

func (c *Client) Broadcast()

Broadcast processes server messages to the websocket client connection.

Broadcast should always be started as a goroutine for each websocket client which handles all the information sending to the client.

func (*Client) CheckSites

func (c *Client) CheckSites()

CheckSites iterates over the client sites to perform health check.

func (*Client) Listen

func (c *Client) Listen()

Listen processes incoming client messages.

Listen should always be started as a goroutine for each websocket client which handles all incoming client requests.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server contains information about the server.

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"os"
	"path"
	"runtime"
	"strings"

	"github.com/gorilla/websocket"
	"github.com/lordmangila/status-checker/pkg/rest"
)

var rs *rest.Server

func init() {

	_, filename, _, _ := runtime.Caller(0)

	dir := path.Join(path.Dir(filename), "../..")

	err := os.Chdir(dir)
	if err != nil {
		panic(err)
	}

	rs = rest.NewServer()
	go rs.Run()

	rs.SetRoutes()

	go func() {
		rs.ListenListenAndServe()
	}()
}

func main() {
	// Create a new test Server.
	server := httptest.NewServer(http.HandlerFunc(rs.ServeWS))
	defer server.Close()

	// Connect to the websocket address.
	wsAddr := "ws" + strings.TrimPrefix(server.URL, "http")
	conn, _, err := websocket.DefaultDialer.Dial(wsAddr, nil)
	if nil != err {
		conn.Close()
	}

	// Send an invalid url to the server.
	conn.WriteMessage(1, []byte("invalidurl"))

	// Read failed response from the websocket.
	_, message, _ := conn.ReadMessage()
	fmt.Println(string(message))

	// Register https://www.google.com/ to the client's sites.
	conn.WriteMessage(1, []byte("https://www.google.com/"))

	// Read response from the websocket for google.
	_, message, _ = conn.ReadMessage()
	fmt.Println(string(message))

	// Register https://www.github.com/ to the client's sites.
	conn.WriteMessage(1, []byte("https://www.github.com/"))

	// This will be done twice as there are already 2 sites registered to the client.
	// Read response from the websocket.
	// 1st response will show google status.
	_, message, _ = conn.ReadMessage()
	fmt.Println(string(message))

	// Read response from the websocket.
	// 2nd response will show github status.
	_, message, _ = conn.ReadMessage()
	fmt.Println(string(message))

}
Output:

{"URL":"invalidurl","StatusCode":0,"Active":false,"Valid":false,"Error":"Invalid URI: invalidurl"}
{"URL":"https://www.google.com/","StatusCode":200,"Active":true,"Valid":true,"Error":""}
{"URL":"https://www.google.com/","StatusCode":200,"Active":true,"Valid":true,"Error":""}
{"URL":"https://www.github.com/","StatusCode":200,"Active":true,"Valid":true,"Error":""}

func NewServer

func NewServer() *Server

NewServer initializes an new websocket server.

func (*Server) ListenListenAndServe

func (s *Server) ListenListenAndServe()

ListenListenAndServe initiates the handlers.

func (*Server) Run

func (s *Server) Run()

Run ...

func (*Server) ServeWS

func (s *Server) ServeWS(w http.ResponseWriter, r *http.Request)

ServeWS serves the websocket handler.

func (*Server) SetRoutes

func (s *Server) SetRoutes()

SetRoutes sets the routes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL