aboutsummaryrefslogtreecommitdiffstats
path: root/serve.go
blob: 6447c58c60fe90cf59469300b5d51de2880f8db7 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main

import (
	"code.google.com/p/gorilla/sessions"
	"fmt"
	"html/template"
	"log"
	"net/http"
	"path/filepath"
	"regexp"
	"time"
)

var (
	tmplHome, tmplView, tmplAccount, tmplUser, tmplBomView, tmplBomUpload *template.Template
)

var store = sessions.NewCookieStore([]byte(*sessionSecret))

func baseHandler(w http.ResponseWriter, r *http.Request) {
	var err error
	log.Printf("serving %s\n", r.URL.Path)

	bomUrlPattern := regexp.MustCompile("^/([a-zA-Z][a-zA-Z0-9_]*)/([a-zA-Z][a-zA-Z0-9_]*)/$")
	bomUploadUrlPattern := regexp.MustCompile("^/([a-zA-Z][a-zA-Z0-9_]*)/([a-zA-Z][a-zA-Z0-9_]*)/_upload/$")
	userUrlPattern := regexp.MustCompile("^/([a-zA-Z][a-zA-Z0-9_]*)/$")

	switch {
	case r.URL.Path == "/":
		err = homeController(w, r)
	case r.URL.Path == "/account/login/":
		err = loginController(w, r)
	case r.URL.Path == "/account/logout/":
		err = logoutController(w, r)
	//case r.URL.Path == "/account/newuser/":
	//	err = newUserController(w, r)
	case bomUploadUrlPattern.MatchString(r.URL.Path):
		match := bomUploadUrlPattern.FindStringSubmatch(r.URL.Path)
		err = bomUploadController(w, r, match[1], match[2])
	case bomUrlPattern.MatchString(r.URL.Path):
		match := bomUrlPattern.FindStringSubmatch(r.URL.Path)
		err = bomController(w, r, match[1], match[2])
	case userUrlPattern.MatchString(r.URL.Path):
		match := userUrlPattern.FindStringSubmatch(r.URL.Path)
		err = userController(w, r, match[1], "")
	default:
		// 404
		log.Println("warning: 404")
		http.NotFound(w, r)
		return
	}
	if err != nil {
		// this could cause multiple responses?
		log.Println("error, 500: " + err.Error())
		http.Error(w, "Internal error (check logs)", 500)
	}
}

func homeController(w http.ResponseWriter, r *http.Request) (err error) {
	session, _ := store.Get(r, "bommom")
	context := make(map[string]interface{})
	context["Session"] = session.Values
	log.Printf("%s\n", session.Values["UserName"])
	context["BomList"], err = bomstore.ListBoms("")
	if err != nil {
		return
	}
	err = tmplHome.Execute(w, context)
	return
}

func loginController(w http.ResponseWriter, r *http.Request) (err error) {
	session, _ := store.Get(r, "bommom")
	context := make(map[string]interface{})
	context["ActionLogin"] = true
	context["Session"] = session.Values
	if r.Method == "POST" {
		if isShortName(r.FormValue("UserName")) != true {
			context["Problem"] = "Ugh, need to use a SHORTNAME!"
			err = tmplAccount.Execute(w, context)
			return
		}
		audience := "http://bommom.com/"
		vResponse := VerifyPersonaAssertion(r.FormValue("assertion"), audience)
		if vResponse.Okay() {
			session.Values["UserName"] = r.FormValue("UserName")
			session.Values["Email"] = vResponse.Email
			session.Save(r, w)
			context["Session"] = session.Values
			http.Redirect(w, r, "/", 302)
			return
		} else {
			context["Problem"] = vResponse.Reason
			err = tmplAccount.Execute(w, context)
			return
		}
	}
	err = tmplAccount.Execute(w, context)
	return
}

func logoutController(w http.ResponseWriter, r *http.Request) (err error) {
	session, _ := store.Get(r, "bommom")
	context := make(map[string]interface{})
	delete(session.Values, "UserName")
	delete(session.Values, "Email")
	session.Save(r, w)
	context["Session"] = session.Values
	context["ActionLogout"] = true
	err = tmplAccount.Execute(w, context)
	return
}

func userController(w http.ResponseWriter, r *http.Request, user, extra string) (err error) {
	session, _ := store.Get(r, "bommom")
	if !isShortName(user) {
		http.Error(w, "invalid username: "+user, 400)
		return
	}
	if err != nil {
		// no such user
		http.NotFound(w, r)
		return
	}
	context := make(map[string]interface{})
	context["BomList"], err = bomstore.ListBoms(ShortName(user))
	if user == "common" {
		context["IsCommon"] = true
	}
	context["UserName"] = user
	context["Session"] = session.Values
	if err != nil {
		return
	}
	err = tmplUser.Execute(w, context)
	return
}

func bomController(w http.ResponseWriter, r *http.Request, user, name string) (err error) {
	session, _ := store.Get(r, "bommom")
	if !isShortName(user) {
		http.Error(w, "invalid username: "+user, 400)
		return
	}
	if !isShortName(name) {
		http.Error(w, "invalid bom name: "+name, 400)
		return
	}

	context := make(map[string]interface{})
	context["BomMeta"], context["Bom"], err = bomstore.GetHead(ShortName(user), ShortName(name))
	context["Session"] = session.Values
	if err != nil {
		http.Error(w, "404 couldn't open bom: "+user+"/"+name, 404)
		return nil
	}
    err = pricingSource.AttachMarketInfoBom(context["Bom"].(*Bom))
    if err != nil {
        log.Println("error attaching market info: " + err.Error())
    }
	err = tmplBomView.Execute(w, context)
	return
}

func bomUploadController(w http.ResponseWriter, r *http.Request, user, name string) (err error) {
	session, _ := store.Get(r, "bommom")

	if !isShortName(user) {
		http.Error(w, "invalid username: "+user, 400)
		return
	}
	if !isShortName(name) {
		http.Error(w, "invalid bom name: "+name, 400)
		return
	}
	context := make(map[string]interface{})
	context["Session"] = session.Values
	context["user"] = ShortName(user)
	context["name"] = ShortName(name)
	context["BomMeta"], context["Bom"], err = bomstore.GetHead(ShortName(user), ShortName(name))

	switch r.Method {
	case "POST":
		err := r.ParseMultipartForm(1024 * 1024 * 2)
		if err != nil {
			log.Println(err)
			http.Error(w, err.Error(), 400)
			return nil
		}
		file, fileheader, err := r.FormFile("bomfile")
		if err != nil {
			log.Println(err)
			context["error"] = "bomfile was nil!"
			err = tmplBomUpload.Execute(w, context)
			return err
		}
		if file == nil {
			log.Println("bomfile was nil")
			context["error"] = "bomfile was nil!"
			err = tmplBomUpload.Execute(w, context)
			return err
		}
		versionStr := r.FormValue("version")
		if len(versionStr) == 0 || isShortName(versionStr) == false {
			context["error"] = "Version must be specified and a ShortName!"
			context["version"] = versionStr
			err = tmplBomUpload.Execute(w, context)
			return err
		}

		//contentType := fileheader.Header["Content-Type"][0]
		var b *Bom
		var bm *BomMeta

		switch filepath.Ext(fileheader.Filename) {
		case ".json":
			bm, b, err = LoadBomFromJSON(file)
			if err != nil {
				context["error"] = "Problem loading JSON file"
				err = tmplBomUpload.Execute(w, context)
				return err
			}
		case ".csv":
			b, err = LoadBomFromCSV(file)
			bm = &BomMeta{}
			if err != nil {
				context["error"] = "Problem loading CSV file: " + err.Error()
				err = tmplBomUpload.Execute(w, context)
				return err
			}
		case ".xml":
			bm, b, err = LoadBomFromXML(file)
			if err != nil {
				context["error"] = "Problem loading XML file"
				err = tmplBomUpload.Execute(w, context)
				return err
			}
		default:
			context["error"] = "Unknown file type: " + string(fileheader.Filename)
			log.Fatal(context["error"])
			err = tmplBomUpload.Execute(w, context)
			return err
		}
		bm.Owner = user
		bm.Name = name
		b.Progeny = "File uploaded from " + fileheader.Filename
		b.Created = time.Now()
		b.Version = string(versionStr)
		if err := bomstore.Persist(bm, b, ShortName(versionStr)); err != nil {
			context["error"] = "Problem saving to datastore: " + err.Error()
			err = tmplBomUpload.Execute(w, context)
		}
		http.Redirect(w, r, "//"+user+"/"+name+"/", 302)
		return err
	case "GET":
		err = tmplBomUpload.Execute(w, context)
		return err
	default:
		http.Error(w, "bad method", 405)
		return nil
	}
	return
}

func serveCmd() {
	var err error

	// load and parse templates
	baseTmplPath := *templatePath + "/base.html"
	tmplHome = template.Must(template.ParseFiles(*templatePath+"/home.html", baseTmplPath))
	tmplAccount = template.Must(template.ParseFiles(*templatePath+"/account.html", baseTmplPath))
	//tmplLogout = template.Must(template.ParseFiles(*templatePath+"/logout.html", baseTmplPath))
	//tmplNewUser = template.Must(template.ParseFiles(*templatePath+"/newuser.html", baseTmplPath))
	tmplUser = template.Must(template.ParseFiles(*templatePath+"/user.html", baseTmplPath))
	tmplBomView = template.Must(template.ParseFiles(*templatePath+"/bom_view.html", baseTmplPath))
	tmplBomUpload = template.Must(template.ParseFiles(*templatePath+"/bom_upload.html", baseTmplPath))
	if err != nil {
		log.Fatal(err)
	}

	openBomStore()
	openAuthStore()
    openPricingSource()

	// serve template static assets (images, CSS, JS)
	http.Handle("/static/", http.FileServer(http.Dir(*templatePath+"/")))
	http.Handle("/favicon.ico", http.FileServer(http.Dir(*templatePath+"/static/")))

	// fall through to default handler
	http.HandleFunc("/", baseHandler)

	listenString := fmt.Sprintf("%s:%d", *listenHost, *listenPort)
	http.ListenAndServe(listenString, nil)
	fmt.Println("Serving at " + listenString)
}