Parcourir la source

added static test for directory sub-dir support

master
sp il y a 4 ans
Parent
révision
2cd53da5f0
7 fichiers modifiés avec 130 ajouts et 14 suppressions
  1. +8
    -4
      config.go
  2. +17
    -3
      config.json
  3. +22
    -1
      html/css/index.css
  4. +25
    -2
      html/index.html
  5. +22
    -0
      html/test/css/test.css
  6. +30
    -0
      html/test/index.html
  7. +6
    -4
      main.go

+ 8
- 4
config.go Voir le fichier

"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{}



+ 17
- 3
config.json Voir le fichier

{ {
"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/"
}
]
} }

+ 22
- 1
html/css/index.css Voir le fichier

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;
} }

+ 25
- 2
html/index.html Voir le fichier

<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> &#x2192; "./html/test/"
</li>
<li>
static url <a href="spa2/">/spa2 </a> &#x2192; "./html/test/"
</li>
<li>
static url <a href="test/">/test </a> &#x2192; "./html/test/"
</li>

</ol>
</div>


</body> </body>



+ 22
- 0
html/test/css/test.css Voir le fichier

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;
}

+ 30
- 0
html/test/index.html Voir le fichier

<head>
<title>Demo static "Test" </title>
<link rel="stylesheet" type="text/css" href="css/test.css">
</head>
<h1> Test </h1>
<h1> &#8595; </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> &#8595; </h1>
<ul>
<li>
<a href="/"> back to "/" &#x2192; "./html/" </a>
</li>
</ul>
</div>




+ 6
- 4
main.go Voir le fichier

} }


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() {

Chargement…
Annuler
Enregistrer