server

package
v0.0.0-...-3454be3 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2014 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

package server implements a Google Cloud Endpoints server.

The server does simple transforms on requests that come in to /_ah/api and then re-dispatches them to /_ah/spi. It does not do any authentication, quota checking, DoS checking, etc.

In addition, the server loads api configs from /_ah/spi/BackendService.getApiConfigs prior to each call, in case the configuration has changed.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EndpointsServer

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

HTTP handler for requests to the built-in api-server handlers.

Example
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/rwl/endpoints/server"
	"github.com/rwl/go-endpoints/endpoints"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"net/url"
	"time"
)

// Request type for the Message.Echo method.
type EchoReq struct {
	Message string `json:"message"`
	Delay   int    `json:"delay" endpoints:"d=2"`
}

// Response type for the MessageService.Echo method.
type EchoResp struct {
	Message string    `json:"message"`
	Date    time.Time `json:"date"`
}

// EchoService echoes a message after a delay.
type EchoService struct {
}

// Echo responds with the given message after the specified delay.
func (s *EchoService) Echo(_ *http.Request, req *EchoReq, resp *EchoResp) error {
	if req.Delay < 0 {
		req.Delay = 0
	}

	time.Sleep(time.Duration(req.Delay) * time.Second)

	resp.Message = req.Message
	resp.Date = time.Now()
	return nil
}

func main() {
	spi := endpoints.NewServer("")

	echoService := &EchoService{}
	echoApi, _ := spi.RegisterService(echoService, "echo",
		"v1", "Echo API", true)

	info := echoApi.MethodByName("Echo").Info()
	info.Name = "message.echo"
	info.HttpMethod = "GET"
	info.Path = "echo"
	info.Desc = "Echo messages."

	mux := http.NewServeMux()
	ts := httptest.NewServer(mux)
	defer ts.Close()

	spi.HandleHttp(mux)

	// The URL that SPI requests should be dispatched to.
	u, _ := url.Parse(ts.URL)
	api := server.NewEndpointsServer(u)

	api.HandleHttp(mux)

	message := map[string]interface{}{
		"message": "The quick red fox jumped over the lazy brown dog",
		//"delay":   2,
	}

	body, _ := json.Marshal(message)
	buf := bytes.NewBuffer(body)

	uu := ts.URL + "/_ah/api/echo/v1/echo?delay=0"

	req, _ := http.NewRequest("GET", uu, buf)
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, _ := client.Do(req)

	bytes, _ := ioutil.ReadAll(resp.Body)
	resp.Body.Close()

	var jsonResp map[string]interface{}
	json.Unmarshal(bytes, &jsonResp)
	fmt.Print(jsonResp["message"])
}
Output:

The quick red fox jumped over the lazy brown dog

func NewEndpointsServer

func NewEndpointsServer(URL *url.URL) *EndpointsServer

NewEndpointsServer returns a new EndpointsServer that will dispatch SPI requests to the given URL.

func NewEndpointsServerRoot

func NewEndpointsServerRoot(root string, URL *url.URL) *EndpointsServer

func (*EndpointsServer) HandleApiExplorerRequest

func (ed *EndpointsServer) HandleApiExplorerRequest(w http.ResponseWriter, r *http.Request)

Handler for requests to /explorer.

func (*EndpointsServer) HandleApiStaticRequest

func (ed *EndpointsServer) HandleApiStaticRequest(w http.ResponseWriter, r *http.Request)

Handler for requests to /static/.*.

func (*EndpointsServer) HandleHttp

func (ed *EndpointsServer) HandleHttp(mux *http.ServeMux)

Configures the server to handler API requests to the default paths. If mux is not specified then http.DefaultServeMux is used.

func (*EndpointsServer) ServeHTTP

func (ed *EndpointsServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

EndpointsServer implements the http.Handler interface.

func (*EndpointsServer) SetURL

func (ed *EndpointsServer) SetURL(u *url.URL)

Sets the URL for SPI dispatches to have the form http://ipaddr:port with no trailing slash.

Jump to

Keyboard shortcuts

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