github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/plugin/builtin/helloworld/hw.go (about)

     1  package git
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/evergreen-ci/evergreen/plugin"
     8  	"github.com/evergreen-ci/render"
     9  )
    10  
    11  func init() {
    12  	plugin.Publish(&HelloWorldPlugin{})
    13  }
    14  
    15  // GitPlugin handles fetching source code and applying patches
    16  // using the git version control system.
    17  type HelloWorldPlugin struct{}
    18  
    19  // Name implements Plugin Interface.
    20  func (self *HelloWorldPlugin) Name() string {
    21  	return "helloworld"
    22  }
    23  
    24  // GetRoutes returns an API route for serving patch data.
    25  func (self *HelloWorldPlugin) GetAPIHandler() http.Handler {
    26  	r := http.NewServeMux()
    27  	r.HandleFunc("/cmd1", func(w http.ResponseWriter, r *http.Request) {
    28  		fmt.Println("cmd1 got called with path:", r.URL.Path)
    29  	})
    30  	r.HandleFunc("/cmd2", func(w http.ResponseWriter, r *http.Request) {
    31  		fmt.Println("cmd2 got called with path:", r.URL.Path)
    32  	})
    33  	r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    34  		fmt.Println("hello world: command not found!")
    35  		http.Error(w, "not found", http.StatusNotFound)
    36  	})
    37  	return r
    38  
    39  }
    40  
    41  func (hwp *HelloWorldPlugin) GetUIHandler() http.Handler {
    42  	renderer := render.New(render.Options{})
    43  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    44  		// this handler returns the Id of the current user, as a sanity check that
    45  		// proves plugins have access to user information.
    46  		userId := "Not Logged In"
    47  		u := plugin.GetUser(r)
    48  		if u != nil {
    49  			userId = u.Id
    50  		}
    51  		renderer.WriteJSON(w, http.StatusOK, struct {
    52  			UserId string
    53  		}{userId})
    54  	})
    55  }
    56  
    57  func (self *HelloWorldPlugin) Configure(map[string]interface{}) error {
    58  	return nil
    59  }
    60  
    61  // GetPanelConfig is required to fulfill the Plugin interface. This plugin
    62  // does not have any UI hooks.
    63  func (self *HelloWorldPlugin) GetPanelConfig() (*plugin.PanelConfig, error) {
    64  	return &plugin.PanelConfig{
    65  		Panels: []plugin.UIPanel{
    66  			{
    67  				Page:      plugin.TaskPage,
    68  				Position:  plugin.PageCenter,
    69  				PanelHTML: "<!--hello world!-->",
    70  				DataFunc: func(context plugin.UIContext) (interface{}, error) {
    71  					return map[string]interface{}{}, nil
    72  				},
    73  			},
    74  		},
    75  	}, nil
    76  
    77  	return nil, nil
    78  }
    79  
    80  // NewCommand returns requested commands by name. Fulfills the Plugin interface.
    81  func (self *HelloWorldPlugin) NewCommand(cmdName string) (plugin.Command, error) {
    82  	return nil, &plugin.ErrUnknownCommand{cmdName}
    83  }