SoFunction
Updated on 2025-03-03

golang Iris to run multiple applications

In Iris, there is a way to run multiple applications at the same time:

The application mentioned here is just an Iris framework instance, which can have completely different routing definitions, middleware, etc.

Different ports and different applications

package main

import (
    "log"
    "net/http"
    "time"

    "/kataras/iris/v12"
    "/kataras/iris/v12/middleware/recover"

    "/x/sync/errgroup"
)

var g 

func startApp1() error {
    app := ().SetName("app1")
    (())
    ("/", func(ctx ) {
        ({
            "code":  ,
            "message": "Welcome server 1",
        })
    })

    ()
   return (":8080")
}

func startApp2() error {
    app := ().SetName("app2")
    (())
    ("/", func(ctx ) {
        ({
            "code":  ,
            "message": "Welcome server 2",
        })
    })

    return (":8081")
}

func main() {
    (startApp1)
    (startApp2)

    if err := (); err != nil {
        (err)
    }
}

In this example, we listened for two ports80808081, we can access these two applications through these two ports.

Different domain names and different applications

This is a bit similar to nginx, where the same port can listen to requests for multiple domain names at the same time:

package main

import (
	"/kataras/iris/v12"
	"/kataras/iris/v12/apps"
	"/kataras/iris/v12/middleware/recover"
)

func main() {
	app3 := ().SetName("app3")
	(())
	("/", func(ctx ) {
		("ha3")
	})

	app4 := ().SetName("app4")
	(())
	("/", func(ctx ) {
		("ha4")
	})

	switcher := ({
		{
			Pattern: "",
			Target:  app3,
		},
		{
			Pattern: "",
			Target:  app4,
		},
	})
	(":9010")
}

In this example, monitor9010port, but throughandWhen I visited, the results were different.

This is the end of this article about the implementation of golang Iris running multiple applications. For more related golang Iris running multiple applications, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!