-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaultserver.go
126 lines (108 loc) · 3.26 KB
/
defaultserver.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
package main
import (
"fmt"
"github.com/httpreserve/httpreserve"
"github.com/justinas/alice"
"html/template"
"log"
"net/http"
)
// Primary handler for httpreserve requests
func httpreserveapp(w http.ResponseWriter, r *http.Request) {
handleHttpreserve(w, r)
return
}
// Function to handle calls to save a page to internet archive
func iaSaveApp(w http.ResponseWriter, r *http.Request) {
handleSubmitToInternetArchive(w, r)
return
}
// 404 response handler for all non supported function
func notFound(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusNotFound)
fmt.Fprintln(w, "Sorry, this is not a supported function for this application.")
}
// Handle response when a page is requested by the browser
func indexhandler(w http.ResponseWriter, r *http.Request) {
//404...
if r.URL.String() != "/" {
notFound(w, r)
return
}
//Otherwise...
switch r.Method {
case http.MethodOptions:
handleOptions(w, r)
return
case http.MethodHead:
fallthrough
case http.MethodPost:
fallthrough
case http.MethodGet:
//deliver a default HTML to the web-browser
w.Header().Set("Content-Type", "text/html")
//fmt.Fprintln(w, httpreservePages)
t, _ := template.ParseFiles("static/index.htm")
t.Execute(w, nil)
return
default:
fmt.Fprintln(w, r.Method+" is unsupported from root.")
return
}
}
// Logger middleware to return information to stderr we're
// interested in...
func logger(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s requested %s, method %s", r.RemoteAddr, r.URL, r.Method)
h.ServeHTTP(w, r)
})
}
// Part of our Handler Adapter methods
// TODO: learn more about to document further
type headerSetter struct {
key, val string
handler http.Handler
}
// Part of middleware layer to create default header responses
func (hs headerSetter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set(hs.key, hs.val)
hs.handler.ServeHTTP(w, r)
}
// Set default headers for any single response from httpreserve
func newHeaderSetter(key, val string) func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return headerSetter{key, val, h}
}
}
// Configure our default server mechanism for httpreserve
func configureDefault() http.Handler {
fs := http.FileServer(http.Dir("static"))
h := http.NewServeMux()
//Routes and handlers...
h.HandleFunc("/httpreserve", httpreserveapp)
h.HandleFunc("/save", iaSaveApp)
h.HandleFunc("/", indexhandler)
h.Handle("/static/", http.StripPrefix("/static/", fs))
// Middleware chain to handle various generic HTTP functions
// TODO: Learn what other middleware we may need...
middlewareChain := alice.New(
newHeaderSetter("Server", httpreserve.VersionText()), // USERAGENT IN MAIN PACKAGE
logger,
).Then(h)
return middlewareChain
}
// References contributing to this code...
// https://cryptic.io/go-http/
// https://github.com/justinas/alice
// DefaultServer is our call to standup a default server
// for the httpreserve resolver service to be queried by our other apps.
func DefaultServer(port string) error {
middleWare := configureDefault()
err := http.ListenAndServe(":"+port, middleWare)
if err != nil {
return err
}
return nil
}