-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
43 lines (37 loc) · 957 Bytes
/
handlers.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
// Copyright (C) 2016 romrom@tutanota.com
// Use of this source code is governed by the GPLv3
// license that can be found in the LICENSE file.
package main
import (
"database/sql"
"log"
"net/http"
"time"
_ "github.com/mattn/go-sqlite3"
)
type DBHandler func(w http.ResponseWriter, r *http.Request, db *sql.DB) error
func (h DBHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var env = GetBackEnv()
start := time.Now()
err := h(w, r, env.db)
if err != nil {
http.Error(w, `500 internal server error`, http.StatusInternalServerError)
log.Printf("%s\t%s\t%s",
r.Method,
r.RequestURI,
err,
)
return
} else {
log.Printf("%s\t%s\t(%s)",
r.Method,
r.RequestURI,
time.Since(start),
)
}
}
func JsonHeader(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET")
}