mikeflynn / go-alexa

A collection of Amazon Echo / Alexa tools for Go development.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

In addition to a single Run(), have a way to expose subrouters for existing routers

danverbraganza opened this issue · comments

Use case: A user with an existing http(s) server wants to chuck alexa on it as well, and serve it from the same process.

Solid idea! I've pushed a branch called issue-5 (2de33f6) that breaks out the Run() function in to Run() and Init() with Init() allows you to add the application routes to a router you pass in to the function. It still requires a gorilla/mux router (*mux.Router) but this simple example I worked up works...

package main

import (
    "github.com/gorilla/mux"
    "net/http"

    alexa "github.com/mikeflynn/go-alexa/skillserver"
)

func YourHandler(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Gorilla!\n"))
}

func EchoIntentHandler(echoReq *alexa.EchoRequest, echoResp *alexa.EchoResponse) {
    echoResp.OutputSpeech("Hello world from my new Echo test app!").Card("Hello World", "This is a test card.")
}

func main() {
    var Applications = map[string]interface{}{
        "/echo/helloworld": alexa.EchoApplication{ // Route
            AppID:    "xxxxxxxx", // Echo App ID from Amazon Dashboard
            OnIntent: EchoIntentHandler,
            OnLaunch: EchoIntentHandler,
        },
    }

    r := mux.NewRouter()
    r.HandleFunc("/", YourHandler)

    alexa.Init(Applications, r)

    http.ListenAndServe(":8100", r)
}

Take a look at the branch when you get a chance and let me know if that works for you.

Cool, hopefully I'll get a chance to try it out next week. The way I hacked around it was to run the go-alexa server in a goroutine, and using net/http/util's NewSingleHostReverseProxy

I'm going to go ahead and merge this change in to master since it doesn't change how the primary use case works. Let me know if there's more to do here when you get a chance to play with it.

Awesome, just had a chance to check it, and it works perfectly.

It's a great change from the workaround I hacked together where I was using httputil.NewSingleHostReverseProxy() to proxy to the alexa server running in a different goroutine.

Good to hear! I love that hack but glad this simple change made things easier for you.