-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
132 lines (108 loc) · 3.09 KB
/
http.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
package main
import (
"fmt"
"log"
"net/http"
"strings"
"time"
)
type Meta struct {
Title string
Author string
Email string
}
type Index struct {
Meta Meta
Titles []string
}
type Post struct {
Meta Meta
Content string
}
func SendFeed(w http.ResponseWriter, r *http.Request) {
log.Printf("%s: %s %s (%dB)", r.RemoteAddr, r.Method, r.Host, r.ContentLength)
if r.Method != "GET" {
http.Error(w, "invalid request method", http.StatusMethodNotAllowed)
return
}
index, err := MakeIndex()
if err != nil {
log.Println("error making index:", err.Error())
http.Error(w, "error making index", http.StatusInternalServerError)
return
}
feed, err := MakeFeed(index)
if err != nil {
log.Println("error making feed:", err.Error())
http.Error(w, "error making feed", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/rss+xml")
w.Write([]byte(feed))
}
func SendIndex(w http.ResponseWriter, r *http.Request) {
log.Printf("%s: %s %s (%dB)", r.RemoteAddr, r.Method, r.Host, r.ContentLength)
if r.Method != "GET" {
http.Error(w, "invalid request method", http.StatusMethodNotAllowed)
return
}
index, err := MakeIndex()
if err != nil {
log.Println("error making index:", err.Error())
http.Error(w, "error making index", http.StatusInternalServerError)
return
}
if err := INDEX_TMPL.Execute(w, index); err != nil {
log.Println(err.Error())
http.Error(w, "error executing template", http.StatusInternalServerError)
}
}
func SendPost(w http.ResponseWriter, r *http.Request) {
log.Printf("%s: %s %s (%dB)", r.RemoteAddr, r.Method, r.Host, r.ContentLength)
if r.Method != "GET" {
http.Error(w, "invalid request method", http.StatusMethodNotAllowed)
return
}
path := strings.TrimPrefix(r.URL.Path, "/posts/")
post, err := MakePost(path)
if err != nil {
log.Println("error making post:", err.Error())
http.Error(w, "error making post", http.StatusInternalServerError)
return
}
if err := POST_TMPL.Execute(w, post); err != nil {
log.Println("error executing template:", err.Error())
http.Error(w, "error executing template", http.StatusInternalServerError)
}
}
func TakePost(w http.ResponseWriter, r *http.Request) {
log.Printf("%s: %s %s (%dB)", r.RemoteAddr, r.Method, r.Host, r.ContentLength)
if r.Method != "POST" {
http.Error(w, "invalid request method", http.StatusMethodNotAllowed)
return
}
if !IsKeyValid(r.Header.Get("Authorization")) {
log.Println("invalid key")
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
title := r.FormValue("title")
if title == "" {
log.Println("empty title")
http.Error(w, "empty title", http.StatusBadRequest)
return
}
content := r.FormValue("content")
if content == "" {
log.Println("empty content")
http.Error(w, "empty content", http.StatusBadRequest)
return
}
name := fmt.Sprintf("%s %s", time.Now().Format(time.DateOnly), title)
if err := savePost(name, []byte(content)); err != nil {
log.Println("error saving post:", err.Error())
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}