| "io/ioutil" | "io/ioutil" | ||||
| ) | ) | ||||
| type configuration struct { | |||||
| Host string | |||||
| Port string | |||||
| DocRoot string | |||||
| type configStaticHtml struct { | |||||
| Dir string | |||||
| StaticUrl string | StaticUrl string | ||||
| StripPrefix string | StripPrefix string | ||||
| } | } | ||||
| type configuration struct { | |||||
| Host string | |||||
| Port string | |||||
| Static []configStaticHtml | |||||
| } | |||||
| var configFile = "config.json" | var configFile = "config.json" | ||||
| var config = configuration{} | var config = configuration{} | ||||
| { | { | ||||
| "Host":"localhost", | "Host":"localhost", | ||||
| "Port":"8080", | "Port":"8080", | ||||
| "DocRoot": "./html/", | |||||
| "StaticUrl": "/spa/", | |||||
| "StripPrefix" : "/spa/" | |||||
| "Static": [ | |||||
| { | |||||
| "Dir": "./html/", | |||||
| "StaticUrl": "/", | |||||
| "StripPrefix" : "/" | |||||
| }, | |||||
| { | |||||
| "Dir": "./html/test/", | |||||
| "StaticUrl": "/spa1/", | |||||
| "StripPrefix" : "/spa1/" | |||||
| }, | |||||
| { | |||||
| "Dir": "./html/test/", | |||||
| "StaticUrl": "/spa2/", | |||||
| "StripPrefix" : "/spa2/" | |||||
| } | |||||
| ] | |||||
| } | } |
| H1 { | |||||
| p { | |||||
| background-color:yellow; | background-color:yellow; | ||||
| } | |||||
| h1 { | |||||
| text-align: center; | |||||
| text-decoration: #1ABC9C; | |||||
| } | |||||
| div { | |||||
| width: 300px; | |||||
| margin: auto; | |||||
| text-align: left; | |||||
| } | |||||
| hr { | |||||
| width: 100px; | |||||
| border: 10px solid green; | |||||
| border-radius: 5px; | |||||
| } | |||||
| li{ | |||||
| padding-bottom:15px; | |||||
| } | } |
| <link rel="stylesheet" type="text/css" href="css/index.css"> | <link rel="stylesheet" type="text/css" href="css/index.css"> | ||||
| </head> | </head> | ||||
| <body> | <body> | ||||
| <h1> this is static file </h1> | |||||
| <a href="css/index.css"> test link index.css </a> | |||||
| <h1> Static Html Site </h1> | |||||
| <hr> | |||||
| <div class="center"> | |||||
| <p > | |||||
| This is a static website located on / | |||||
| </p> | |||||
| <ol> | |||||
| <li> | |||||
| <a href="css/index.css"> css/index.css </a> | |||||
| </li> | |||||
| <li> | |||||
| static url <a href="spa1/"> /spa1 </a> → "./html/test/" | |||||
| </li> | |||||
| <li> | |||||
| static url <a href="spa2/">/spa2 </a> → "./html/test/" | |||||
| </li> | |||||
| <li> | |||||
| static url <a href="test/">/test </a> → "./html/test/" | |||||
| </li> | |||||
| </ol> | |||||
| </div> | |||||
| </body> | </body> | ||||
| p { | |||||
| background-color:lightblue; | |||||
| padding: 20px 5px; | |||||
| } | |||||
| h1 { | |||||
| text-align: center; | |||||
| text-decoration: overline; | |||||
| } | |||||
| div { | |||||
| width: 300px; | |||||
| margin: auto; | |||||
| text-align: left; | |||||
| background-color: yellow; | |||||
| } | |||||
| hr { | |||||
| width: 100px; | |||||
| border: 6px solid lightcoral; | |||||
| border-radius: 5px; | |||||
| } |
| <head> | |||||
| <title>Demo static "Test" </title> | |||||
| <link rel="stylesheet" type="text/css" href="css/test.css"> | |||||
| </head> | |||||
| <h1> Test </h1> | |||||
| <h1> ↓ </h1> | |||||
| <hr> | |||||
| <div class="center"> | |||||
| <p > | |||||
| This is a static website located on /test but virtually accessed by /spa1 and /spa2 | |||||
| </p> | |||||
| <hr> | |||||
| <ul> | |||||
| <li> | |||||
| <a href="css/test.css"> css/test.css </a> | |||||
| </li> | |||||
| </ul> | |||||
| <h1> ↓ </h1> | |||||
| <ul> | |||||
| <li> | |||||
| <a href="/"> back to "/" → "./html/" </a> | |||||
| </li> | |||||
| </ul> | |||||
| </div> | |||||
| } | } | ||||
| func setupRootFileServer() { | func setupRootFileServer() { | ||||
| fs := http.FileServer(http.Dir(config.DocRoot + "test")) | |||||
| http.Handle(config.StaticUrl, http.StripPrefix(config.StripPrefix, fs)) | |||||
| //root of doc | |||||
| for idx, node := range config.Static { | |||||
| log.Printf("setting up %d static with %+v\n", idx, node) | |||||
| fs := http.FileServer(http.Dir(node.Dir)) | |||||
| http.Handle(node.StaticUrl, http.StripPrefix(node.StripPrefix, fs)) | |||||
| } | |||||
| } | } | ||||
| func setupHTTPHandler() { | func setupHTTPHandler() { |