Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple goji instances #33

Closed
bored-engineer opened this issue Jun 17, 2014 · 2 comments
Closed

Multiple goji instances #33

bored-engineer opened this issue Jun 17, 2014 · 2 comments

Comments

@bored-engineer
Copy link

I didn't find any documentation on how to run multiple goji instances within the same go program. That would be a nice addition, even if just documented here on this issue.

@elithrar
Copy link
Contributor

I assume you’re after a way to serve multiple Goji instances on different ports from the same binary?

If so it's pretty similar to vanilla net/http—create two servers and run them in goroutines. Here's a (really) basic example:

package main

import (
    "log"
    "net/http"
    "sync"

    "github.com/zenazn/goji/web"
)

func main() {

    a := web.New()
    a.Get("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Server A root.")) })

    b := web.New()
    b.Get("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Server B root.")) })

    var wg sync.WaitGroup

    wg.Add(1)
    go func() {
        log.Println("Server A")
        http.ListenAndServe(":9000", a)
    }()

    wg.Add(1)
    go func() {
        log.Println("Server B")
        http.ListenAndServe(":9001", b)
    }()

    wg.Wait()
}

@bored-engineer
Copy link
Author

That's exactly what I was looking for, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants