-
Notifications
You must be signed in to change notification settings - Fork 198
/
httpsource.go
53 lines (45 loc) · 1.21 KB
/
httpsource.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
// Package httpsource provides a HTTP Source example.
//
// Try http://localhost:8080/large.jpg
// or any image available in https://github.com/pierrre/imageserver/tree/master/testdata
package main
import (
"flag"
"net/http"
imageserver_http "github.com/pierrre/imageserver/http"
_ "github.com/pierrre/imageserver/image/gif"
_ "github.com/pierrre/imageserver/image/jpeg"
_ "github.com/pierrre/imageserver/image/png"
imageserver_source_http "github.com/pierrre/imageserver/source/http"
)
const (
urlPrefix = "https://raw.githubusercontent.com/pierrre/imageserver/master/testdata/"
)
var (
flagHTTP = ":8080"
)
func main() {
parseFlags()
startHTTPServer()
}
func parseFlags() {
flag.StringVar(&flagHTTP, "http", flagHTTP, "HTTP")
flag.Parse()
}
func startHTTPServer() {
http.Handle("/", http.StripPrefix("/", newImageHTTPHandler()))
http.Handle("/favicon.ico", http.NotFoundHandler())
err := http.ListenAndServe(flagHTTP, nil)
if err != nil {
panic(err)
}
}
func newImageHTTPHandler() http.Handler {
return &imageserver_http.Handler{
Parser: &imageserver_http.SourcePrefixParser{
Parser: &imageserver_http.SourcePathParser{},
Prefix: urlPrefix,
},
Server: &imageserver_source_http.Server{},
}
}