server

package
v0.0.0-...-1aaae71 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2019 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option interface {
	option.Interface
}

func WithPrefix

func WithPrefix(s string) Option

WithPrefix allows you to specify a custom prefix in the URL path for the server. By default, "/api" is used

type Server

type Server struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"context"
	"encoding/json"
	"net/http"
	"os"
	"time"

	slack "github.com/lestrrat-go/slack"
	"github.com/lestrrat-go/slack/server"
)

func main() {
	s := server.New()
	s.Handle("channels.info", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// You can do what you want in this handler. Here, we're just
		// going to parse the request, and then print it out
		if err := r.ParseForm(); err != nil {
			http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
			return
		}

		var c slack.ChannelsInfoCall
		if err := c.FromValues(r.Form); err != nil {
			http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
			return
		}

		json.NewEncoder(os.Stdout).Encode(c)

		w.WriteHeader(http.StatusNoContent)
	}))

	var hs http.Server
	hs.Addr = ":9090"
	hs.Handler = s
	go hs.ListenAndServe()

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	hs.Shutdown(ctx)
}
Output:

func New

func New(options ...Option) *Server

func (*Server) GetHandler

func (s *Server) GetHandler(method string) (http.Handler, bool)

func (*Server) Handle

func (s *Server) Handle(method string, h http.Handler)

Handle sets the http.Handler for the given slack method.

func (*Server) ServeHTTP

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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