AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
File: subdomains/wildcard/hosts ~~~ # Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # # For example: # # 102.54.94.97 rhino.acme.com # source server # 38.25.63.10 x.acme.com # x client host # localhost name resolution is handled within DNS itself. 127.0.0.1 localhost ::1 localhost #-iris-For development machine, you have to configure your dns also for online, search google how to do it if you don't know 127.0.0.1 mydomain.com 127.0.0.1 username1.mydomain.com 127.0.0.1 username2.mydomain.com 127.0.0.1 username3.mydomain.com 127.0.0.1 username4.mydomain.com 127.0.0.1 username5.mydomain.com #-END iris- ~~~ File: subdomains/wildcard/main.go ~~~ //包装主要是关于如何捕获动态子域名的一个例子——通配符. // 在第一个例子中(子域名1)我们看到了如何为静态子域名创建路由,你知道你将拥有的子域名. // 在这里,我们将看到一个如何捕获未知子域的示例,如username.mydomain.com:8080. package main import ( "github.com/kataras/iris" ) // 首先在您的服务器机器(dns/。)上注册一个动态通配符子域,如果您使用windows,请检查。hosts。. //运行这个文件并尝试重定向: http://username1.mydomain.com:8080/ , http://username2.mydomain.com:8080/ , http://username1.mydomain.com/something, http://username1.mydomain.com/something/sadsadsa func main() { app := iris.New() /* 请注意,您可以同时使用这两种子域名 (命名和通配符(*.) ) admin.mydomain.com, :对其他政党来说(*.) 但这不是这个例子的目的 admin := app.Party("admin.") { // admin.mydomain.com admin.Get("/", func(ctx iris.Context) { ctx.Writef("INDEX FROM admin.mydomain.com") }) // admin.mydomain.com/hey admin.Get("/hey", func(ctx iris.Context) { ctx.Writef("HEY FROM admin.mydomain.com/hey") }) // admin.mydomain.com/hey2 admin.Get("/hey2", func(ctx iris.Context) { ctx.Writef("HEY SECOND FROM admin.mydomain.com/hey") }) }*/ //没有顺序,你也可以在末端注册子域名. dynamicSubdomains := app.Party("*.") { dynamicSubdomains.Get("/", dynamicSubdomainHandler) dynamicSubdomains.Get("/something", dynamicSubdomainHandler) dynamicSubdomains.Get("/something/{paramfirst}", dynamicSubdomainHandlerWithParam) } app.Get("/", func(ctx iris.Context) { ctx.Writef("Hello from mydomain.com path: %s", ctx.Path()) }) app.Get("/hello", func(ctx iris.Context) { ctx.Writef("Hello from mydomain.com path: %s", ctx.Path()) }) // http://mydomain.com:8080 // http://username1.mydomain.com:8080 // http://username2.mydomain.com:8080/something // http://username3.mydomain.com:8080/something/yourname app.Run(iris.Addr("mydomain.com:8080")) // for beginners: look ../hosts file } func dynamicSubdomainHandler(ctx iris.Context) { username := ctx.Subdomain() ctx.Writef("Hello from dynamic subdomain path: %s, here you can handle the route for dynamic subdomains, handle the user: %s", ctx.Path(), username) // if http://username4.mydomain.com:8080/ prints: // Hello from dynamic subdomain path: /, here you can handle the route for dynamic subdomains, handle the user: username4 } func dynamicSubdomainHandlerWithParam(ctx iris.Context) { username := ctx.Subdomain() ctx.Writef("Hello from dynamic subdomain path: %s, here you can handle the route for dynamic subdomains, handle the user: %s", ctx.Path(), username) ctx.Writef("The paramfirst is: %s", ctx.Params().Get("paramfirst")) } ~~~