-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor to introduce a simplistic URL router, logging, and html temp…
…lates with a data driven table
- Loading branch information
1 parent
a46d229
commit c8636ee
Showing
4 changed files
with
57 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
"net/http" | ||
) | ||
|
||
// HexColors wraps a list of colors as hexadecimal strings | ||
type HexColors struct { | ||
Colors []string | ||
} | ||
|
||
// GetHexTemplate returns the parsed file as a template object | ||
func GetHexTemplate() *template.Template { | ||
return template.Must(template.ParseFiles("hexcolorstemplate.html")) | ||
} | ||
|
||
func hexController(w http.ResponseWriter, r *http.Request) { | ||
colors := []string{} | ||
for i := 255; i <= 16711680; i = i * 256 { | ||
s := fmt.Sprintf("%06X", i) | ||
colors = append(colors, s) | ||
} | ||
data := HexColors{colors} | ||
hexTemplate.Execute(w, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<html> | ||
<head><style> | ||
table, th, td { | ||
border: 2px solid black; | ||
} | ||
</style></head> | ||
<body> | ||
<table> | ||
{{range .Colors}} | ||
<tr> | ||
<td>{{.}}</td><td style="background-color: {{.}}; ">__</td> | ||
</tr> | ||
{{end}} | ||
</table> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ body{ | |
</style> | ||
</head> | ||
<body> | ||
hi | ||
hi , try <a href="/hexcolors">hexcolors</a> | ||
</body> | ||
</html> | ||
`)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters