forked from CrunchyData/pg_featureserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
158 lines (133 loc) · 4.2 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
/*
Copyright 2019 Crunchy Data Solutions, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"strings"
"github.com/CrunchyData/pg_featureserv/api"
"github.com/CrunchyData/pg_featureserv/ui"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
//--- simple request handler error framework
// see https://blog.golang.org/error-handling-and-go
type appError struct {
Error error
Message string
Code int
}
type appHandler func(http.ResponseWriter, *http.Request) *appError
func appErrorMsg(err error, msg string, code int) *appError {
return &appError{err, msg, code}
}
func appErrorInternal(err error, msg string) *appError {
return &appError{err, msg, http.StatusInternalServerError}
}
func appErrorInternalFmt(err error, format string, v ...interface{}) *appError {
msg := fmt.Sprintf(format, v...)
return &appError{err, msg, http.StatusInternalServerError}
}
func appErrorNoFound(err error, msg string) *appError {
return &appError{err, msg, http.StatusNotFound}
}
func appErrorNotFoundFmt(err error, format string, v string) *appError {
msg := fmt.Sprintf(format, v)
return &appError{err, msg, http.StatusNotFound}
}
//========================
func serveURLBase(r *http.Request) string {
// Preferred host:port
php := r.Host
php = strings.TrimRight(php, "/")
// Preferred scheme
ps := "http"
// Preferred base path
pbp := "/"
// Preferred scheme / host / port / base
pshpb := fmt.Sprintf("%v://%v%v", ps, php, pbp)
return pshpb
}
func getRequestVar(varname string, r *http.Request) string {
vars := mux.Vars(r)
nameFull := vars[varname]
name := api.PathStripFormat(nameFull)
return name
}
// urlPathFormat provides a URL for the given base, path and format
func urlPathFormat(urlBase string, path string, format string) string {
var pathType string
if path == "" {
pathType = ""
if format == api.FormatHTML {
pathType = "home.html"
}
} else {
pathType = path + "." + format
}
url := fmt.Sprintf("%v%v", urlBase, pathType)
/*
if !supportedContentType(contentType) {
panic(fmt.Sprintf("unsupported content type: %v", contentType))
}
*/
return url
}
func urlPathFormatQuery(urlBase string, path string, format string, query string) string {
url := urlPathFormat(urlBase, path, format)
if query != "" {
url = fmt.Sprintf("%v?%v", url, query)
}
return url
}
func writeJSON(w http.ResponseWriter, contype string, content interface{}) *appError {
encodedContent, err := json.Marshal(content)
if err != nil {
log.Printf("JSON encoding error: %v", err.Error())
return appErrorInternal(err, api.ErrMsgEncoding)
}
writeResponse(w, contype, encodedContent)
return nil
}
func writeHTML(w http.ResponseWriter, content interface{}, context interface{}, templ *template.Template) *appError {
encodedContent, err := ui.RenderHTML(templ, content, context)
if err != nil {
log.Printf("HTML encoding error: %v", err.Error())
return appErrorInternal(err, api.ErrMsgEncoding)
}
writeResponse(w, api.ContentTypeHTML, encodedContent)
return nil
}
func writeResponse(w http.ResponseWriter, contype string, encodedContent []byte) {
w.Header().Set("Content-Type", contype) //api.ContentType(format))
w.WriteHeader(http.StatusOK)
w.Write(encodedContent)
}
// Sets response 'status', and writes a json-encoded object with property "description" having value "msg".
func writeError(w http.ResponseWriter, code string, msg string, status int) {
w.WriteHeader(status)
result, err := json.Marshal(struct {
Code string `json:"code"`
Description string `json:"description"`
}{
Code: code,
Description: msg,
})
if err != nil {
w.Write([]byte(fmt.Sprintf("problem marshaling error: %v", msg)))
} else {
w.Write(result)
}
}