summary refs log tree commit diff stats
path: root/svc/handlers.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-11 17:41:02 -0400
committerGitHub <noreply@github.com>2019-06-11 17:41:02 -0400
commitd4af885c40ba55ea0ed9adade98afe0658099c47 (patch)
tree1c1f14428e39c8322c03d77912914647ce826d06 /svc/handlers.go
parent1349bbc6e6627f357065602e085be762aab06383 (diff)
parent3d2994b74c1105b4c6c8fe0c10d47d6d3541907a (diff)
downloadgetwtxt-d4af885c40ba55ea0ed9adade98afe0658099c47.tar.gz
Merge pull request #3 from getwtxt/merge-indexHandler-cssHandler
merged indexHandler with cssHandler, updated tests
Diffstat (limited to 'svc/handlers.go')
-rw-r--r--svc/handlers.go69
1 files changed, 32 insertions, 37 deletions
diff --git a/svc/handlers.go b/svc/handlers.go
index f83d4b2..d3b8d8a 100644
--- a/svc/handlers.go
+++ b/svc/handlers.go
@@ -17,59 +17,58 @@ func getEtag(modtime time.Time) string {
 	return fmt.Sprintf("%x", sha256.Sum256(shabytes))
 }
 
-// handles "/"
-func indexHandler(w http.ResponseWriter, r *http.Request) {
+func sendStaticEtag(w http.ResponseWriter, isCSS bool) {
+	if isCSS {
+		etag := getEtag(staticCache.cssMod)
+		w.Header().Set("ETag", "\""+etag+"\"")
+		w.Header().Set("Content-Time", txtutf8)
+		return
+	}
+	etag := getEtag(staticCache.indexMod)
+	w.Header().Set("ETag", "\""+etag+"\"")
+	w.Header().Set("Content-Time", htmlutf8)
+}
 
+// handles "/" and "/css"
+func staticHandler(w http.ResponseWriter, r *http.Request) {
 	pingAssets()
 
 	// Take the hex-encoded sha256 sum of the index template's mod time
 	// to send as an ETag. If an error occurred when grabbing the file info,
 	// the ETag will be empty.
 	staticCache.mu.RLock()
-	etag := getEtag(staticCache.indexMod)
-	w.Header().Set("ETag", "\""+etag+"\"")
-	w.Header().Set("Content-Type", htmlutf8)
-
-	_, err := w.Write(staticCache.index)
-	staticCache.mu.RUnlock()
-	if err != nil {
-		errHTTP(w, r, err, http.StatusInternalServerError)
-		return
+	switch r.URL.Path {
+	case "/css":
+		sendStaticEtag(w, true)
+		_, err := w.Write(staticCache.css)
+		if err != nil {
+			staticCache.mu.RUnlock()
+			errHTTP(w, r, err, http.StatusInternalServerError)
+			return
+		}
+	default:
+		sendStaticEtag(w, false)
+		_, err := w.Write(staticCache.index)
+		if err != nil {
+			staticCache.mu.RUnlock()
+			errHTTP(w, r, err, http.StatusInternalServerError)
+			return
+		}
 	}
-
-	log200(r)
-}
-
-// Serving the stylesheet virtually because
-// files aren't served directly in getwtxt.
-func cssHandler(w http.ResponseWriter, r *http.Request) {
-
-	pingAssets()
-
-	staticCache.mu.RLock()
-	etag := getEtag(staticCache.cssMod)
-	w.Header().Set("ETag", "\""+etag+"\"")
-	w.Header().Set("Content-Type", cssutf8)
-
-	_, err := w.Write(staticCache.css)
 	staticCache.mu.RUnlock()
-	if err != nil {
-		errHTTP(w, r, err, http.StatusInternalServerError)
-		return
-	}
 
 	log200(r)
 }
 
 // handles "/api"
 func apiBaseHandler(w http.ResponseWriter, r *http.Request) {
-	indexHandler(w, r)
+	staticHandler(w, r)
 }
 
 // handles "/api/plain"
 // maybe add json/xml support later
 func apiFormatHandler(w http.ResponseWriter, r *http.Request) {
-	indexHandler(w, r)
+	staticHandler(w, r)
 }
 
 func apiAllTweetsHandler(w http.ResponseWriter, r *http.Request) {
@@ -95,7 +94,6 @@ func apiAllTweetsHandler(w http.ResponseWriter, r *http.Request) {
 
 // handles "/api/plain/(users|mentions|tweets)"
 func apiEndpointHandler(w http.ResponseWriter, r *http.Request) {
-
 	errLog("Error when parsing query values: ", r.ParseForm())
 
 	if r.FormValue("q") != "" || r.FormValue("url") != "" {
@@ -165,7 +163,6 @@ func apiEndpointPOSTHandler(w http.ResponseWriter, r *http.Request) {
 
 // handles "/api/plain/tags"
 func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) {
-
 	out, err := twtxtCache.QueryInStatus("#")
 	if err != nil {
 		errHTTP(w, r, err, http.StatusInternalServerError)
@@ -190,12 +187,10 @@ func apiTagsBaseHandler(w http.ResponseWriter, r *http.Request) {
 
 // handles "/api/plain/tags/[a-zA-Z0-9]+"
 func apiTagsHandler(w http.ResponseWriter, r *http.Request) {
-
 	vars := mux.Vars(r)
 	tags := vars["tags"]
 
 	out := compositeStatusQuery("#"+tags, r)
-
 	out = registry.ReduceToPage(1, out)
 	data := parseQueryOut(out)
 
id='n325' href='#n325'>325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490