-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverd.go
127 lines (106 loc) · 2.78 KB
/
serverd.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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/ghodss/yaml"
"github.com/gorilla/mux"
"image"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"log"
"main/mapimage"
"net/http"
_ "net/http/pprof"
"os"
)
type db struct {
images []mapimage.MapImage
}
var maps db
type ImageConfig struct {
Id string `json:"id"`
Name string `json:"name"`
ReferencePoints []mapimage.MapImagePair `json:"referencePoints"`
Filename string `json:"filename"`
}
func DisallowUnknownFields(d *json.Decoder) *json.Decoder {
d.DisallowUnknownFields()
return d
}
func init() {
config, err := os.Open("./images/config.yaml")
if err != nil {
log.Fatal(err)
}
defer config.Close()
buf, err := ioutil.ReadAll(config)
if err != nil {
log.Fatal(err)
}
var loadedImages []ImageConfig
err = yaml.UnmarshalStrict(buf, &loadedImages, DisallowUnknownFields)
if err != nil {
log.Fatal(err)
}
maps.images = make([]mapimage.MapImage, 0)
for _, loadedImage := range loadedImages {
f, err := os.Open(fmt.Sprintf("./images/%s", loadedImage.Filename))
if err == nil {
if fileConfig, format, err := image.DecodeConfig(f); err == nil {
f.Seek(0, 0)
approxSize := (3 * fileConfig.Width * fileConfig.Height) / 1024 / 1024
log.Printf("%v is approx %v MB, in format %v\n", loadedImage.Filename, approxSize, format)
var mi mapimage.MapImage
if approxSize > 1000 {
log.Printf("Using VIPS for %v\n", loadedImage.Filename)
mi = mapimage.NewVIPSImageInfo(
loadedImage.Id,
loadedImage.Name,
loadedImage.ReferencePoints,
f)
} else {
log.Printf("Using Go Image for %v\n", loadedImage.Filename)
mi = mapimage.NewImageInfo(
loadedImage.Id,
loadedImage.Name,
loadedImage.ReferencePoints,
f)
log.Printf(" >> MinZoom: %v MaxZoom: %v\n", mi.MinZoom(), mi.MaxZoom())
}
mi = mapimage.FilesystemCachedImage(mi)
maps.images = append(maps.images, mi)
}
} else {
f.Close()
}
}
log.Println("Running")
}
func (obj db) ListAll() []mapimage.MapImage {
items := make([]mapimage.MapImage, 0)
for idx, _ := range obj.images {
items = append(items, obj.images[idx])
}
return items
}
func (obj db) GetById(id string) (mapimage.MapImage, error) {
for _, ii := range obj.images {
if ii.Id() == id {
return ii, nil
}
}
return nil, errors.New("not found")
}
func main() {
router := mux.NewRouter()
api := router.PathPrefix("/api").Subrouter()
mapimage.AttachApi(maps, api, "/imageinfo", "/file")
// NB: the path is just hardcoded here!
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./ui/build")))
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
log.Fatal(http.ListenAndServe(":8000", router))
}