go-examples/avatar/main.go

61 lines
1.2 KiB
Go
Raw Normal View History

2017-07-07 12:29:36 +00:00
package main
import "fmt"
import "log"
import "os"
import "strings"
var users = map[string]map[string]string{
"meutel": map[string]string{
"avatar_url": "https://git.meutel.net/avatars/1",
"full_name": "Meutel",
},
"ARCHIVES": map[string]string{
"avatar_url": "https://git.meutel.net/avatars/2",
},
}
const defaultAvatar = "https://git.meutel.net/img/avatar_default.png"
func avatarUrl(username string) string {
if url, ok := users[username]["avatar_url"]; ok {
return url
} else {
return defaultAvatar
}
}
func fullName(username string) (string, bool) {
if user, ok := users[username]; ok {
if name, ok2 := user["full_name"]; ok2 {
return name, true
} else {
return username, true
}
} else {
return "Unknown", false
}
}
func main() {
if len(os.Args) < 2 {
log.Fatalln("Needs user name argument")
}
name := os.Args[1]
// avatarId := 1
html := `<!DOCTYPE html>
<html>
<head><title>Profile</title></head>
<body>
<h1>{{ name }}</h1>
<img src="{{ avatarUrl }}"/>
</body>
</html>`
full, exists := fullName(name)
if !exists {
fmt.Fprintln(os.Stderr, "Unknown user "+name)
}
html = strings.Replace(html, "{{ name }}", full, 1)
html = strings.Replace(html, "{{ avatarUrl }}", avatarUrl(name), 1)
fmt.Println(html)
}