Serve service extension (Legacy)

ℹ️
This topic refers to legacy configuration syntax. Serve service extensions are now defined as API endpoints.

A Serve Service Extension can completely change the default behavior of App Gateways to serve traffic in arbitrary ways when needed. In the example below, we have a Serve Service Extension that provides an endpoint to check if a user is on vacation.

...
appgateways:
- name: example
  serveSE:
    funcName: Serve
    file: /etc/maverics/extensions/serve.go

/etc/maverics/extensions/serve.go

package main

import (
	"fmt"
	"net/http"

	"maverics"
	"maverics/log"
)

// Serve serves a '/on-vacation' endpoint that can be queried to determine if an
// employee is on vacation. It is expected the username will be passed on the request
// via a 'username' query parameter.
func Serve(ag *maverics.AppGateway) error {
	http.HandleFunc("/on-vacation", func(rw http.ResponseWriter, req *http.Request) {
		ldapIDP, isSet := ag.IDPs["ldap"]
		if !isSet {
			log.Error("msg", "ldap connector 'ldap' not found")
			return
		}

		username := req.URL.Query().Get("username")
		if username == "" {
			http.Error(
				rw,
				`{"error": "username not found on request"}`,
				http.StatusBadRequest,
			)
			return
		}

		attrs, err := ldapIDP.Query(
			map[string]string{"username": username},
			[]string{"onVacation"},
		)
		if err != nil {
			log.Error("msg", fmt.Sprintf("failed to query LDAP: %s", err))
			http.Error(
				rw,
				`{"error": "internal server error"}`,
				http.StatusInternalServerError,
			)
			return
		}

		if val, isSet := attrs["onVacation"]; isSet && val == "yes" {
			_, _ = fmt.Fprintf(rw, `{"onVacation": true}`)
			return
		}

		_, _ = fmt.Fprintf(rw, `{"onVacation": false}`)
	})

	return nil
}