-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (57 loc) · 1.65 KB
/
main.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/gorilla/mux"
_ "github.com/mattn/go-sqlite3"
)
var db *sql.DB
//ErrorResponse represents error response json structure
type ErrorResponse struct {
ErrMessage string `json:"errMessage"`
}
func init() {
var err error
db, err = sql.Open("sqlite3", getenv("DBPATH", "ipn.db"))
if err != nil {
log.Fatal(err)
}
}
func main() {
r := mux.NewRouter()
r.Methods("GET").Path("/").HandlerFunc(index)
r.Methods("GET", "HEAD").Path("/static/{file:[a-zA-Z0-9-_./]+}").HandlerFunc(static)
r.Methods("POST").Path("/note").HandlerFunc(create)
r.Methods("GET").Path("/note/{id:[0-9]+}").HandlerFunc(read)
r.Methods("PUT").Path("/note/{id:[0-9]+}").HandlerFunc(update)
r.Methods("DELETE").Path("/note/{id:[0-9]+}").HandlerFunc(delete)
r.Methods("GET").Path("/notes").HandlerFunc(list)
r.HandleFunc("/expire", expire)
fmt.Println("started http server on port 8080 @", time.Now().Format("15:04:05 02/01/2006"))
if err := http.ListenAndServe(":8080", r); err != nil {
log.Fatal(err)
}
}
func encodeHTTPResponse(w http.ResponseWriter, response interface{}) error {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
return json.NewEncoder(w).Encode(response)
}
func encodeHTTPError(w http.ResponseWriter, err error) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{
ErrMessage: err.Error(),
})
}
func getenv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}