Alasdair
Alasdair

Reputation: 14131

Golang: Right way to serve both http & https from Go web app with Goji?

Is this the right way to for a single Go web app (using Goji) to handle both http and https traffic?

package main

import (
    "fmt"
    "net/http"
    "github.com/zenazn/goji/graceful"
    "github.com/zenazn/goji/web"
)

func main() {
    r := web.New()
    //https://127.0.0.1:8000/r
    r.Get("/r", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, %s!", "r")
    })

    go graceful.ListenAndServeTLS(":8000", "cert.pem", "key.pem", r)

    r1 := web.New()
    //  http://127.0.0.1:8001/r1
    r1.Get("/r1", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, %s!", "r1")
    })

    graceful.ListenAndServe(":8001", r1)
}

Or what is the best method for having both ports 8000 and 8001 listened to by a single Go web app?

Upvotes: 3

Views: 3860

Answers (1)

OneOfOne
OneOfOne

Reputation: 99351

You don't need to create a new object, you can simply pass r to graceful.ListenAndServe(":8001", r), unless of course you do a different action that depends on https.

Upvotes: 3

Related Questions