87 lines
1.6 KiB
Go
87 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
var testTemplate *template.Template
|
|
|
|
type ViewData struct {
|
|
User User
|
|
}
|
|
|
|
type User struct {
|
|
ID int
|
|
Email string
|
|
URL string
|
|
}
|
|
|
|
func main() {
|
|
var err error
|
|
testTemplate, err = template.New("./hello.gohtml").Funcs(template.FuncMap{
|
|
"getToken": TemplateToken,
|
|
"getAction": TemplateAction,
|
|
"hasPermission": func(user User, feature string) bool {
|
|
if user.ID == 1 && feature == "feature-a" {
|
|
return true
|
|
}
|
|
return false
|
|
},
|
|
}).ParseFiles("./hello.gohtml")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
http.HandleFunc("/", handler)
|
|
if err = http.ListenAndServe(":3000", nil); err != nil {
|
|
log.Println("server stopped")
|
|
}
|
|
}
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
user := User{
|
|
ID: 1,
|
|
Email: "jon@calhoun.io",
|
|
URL: "https://cloud.dev8.on-premise.ru/shared/incoming/?token=oG9Z9Ej9k6hhNu2R2U9MZz9undYTuDstbsaTxQSLA7ZDPM5XY5iDnzm79S8x7gSi&action=accept",
|
|
}
|
|
vd := ViewData{user}
|
|
err := testTemplate.Execute(w, vd)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
var (
|
|
errNoToken = errors.New("no_token")
|
|
)
|
|
|
|
func TemplateToken(uri string) (string, error) {
|
|
str, err := url.Parse(uri)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
token := str.Query().Get("token")
|
|
if token == "" {
|
|
return "", errNoToken
|
|
}
|
|
return token, nil
|
|
|
|
}
|
|
func TemplateAction(uri string) (string, error) {
|
|
str, err := url.Parse(uri)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
token := str.Query().Get("action")
|
|
if token == "" {
|
|
return "", err
|
|
}
|
|
return token, nil
|
|
}
|