| @@ -6,14 +6,18 @@ import ( | |||
| "io/ioutil" | |||
| ) | |||
| type configuration struct { | |||
| Host string | |||
| Port string | |||
| DocRoot string | |||
| type configStaticHtml struct { | |||
| Dir string | |||
| StaticUrl string | |||
| StripPrefix string | |||
| } | |||
| type configuration struct { | |||
| Host string | |||
| Port string | |||
| Static []configStaticHtml | |||
| } | |||
| var configFile = "config.json" | |||
| var config = configuration{} | |||
| @@ -1,7 +1,21 @@ | |||
| { | |||
| "Host":"localhost", | |||
| "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/" | |||
| } | |||
| ] | |||
| } | |||
| @@ -1,3 +1,24 @@ | |||
| H1 { | |||
| p { | |||
| 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; | |||
| } | |||
| @@ -3,7 +3,30 @@ | |||
| <link rel="stylesheet" type="text/css" href="css/index.css"> | |||
| </head> | |||
| <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> | |||
| @@ -0,0 +1,22 @@ | |||
| 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; | |||
| } | |||
| @@ -0,0 +1,30 @@ | |||
| <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> | |||
| @@ -23,10 +23,12 @@ func main() { | |||
| } | |||
| 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() { | |||