split out logger into proper package

This commit is contained in:
David 2020-09-16 15:17:54 -04:00
parent 87b909a31c
commit b2d6756605
3 changed files with 11 additions and 9 deletions

View File

@ -5,12 +5,13 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"mime" "mime"
"net" "net"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"git.yetaga.in/alazyreader/castor/logger"
) )
var responseCodes = map[string]int{ var responseCodes = map[string]int{
@ -157,12 +158,12 @@ func (h FileHandler) Handle(w *Response, r *Request) {
io.Copy(w, source) io.Copy(w, source)
} }
func RecoveryHandler(next Handler) Handler { func RecoveryHandler(log logger.Logger, next Handler) Handler {
return HandlerFunc(func(w *Response, r *Request) { return HandlerFunc(func(w *Response, r *Request) {
defer func() { defer func() {
err := recover() err := recover()
if err != nil { if err != nil {
log.Println(err) log.Info(err)
w.WriteStatus(responseCodes["TEMPORARYFAILURE"], "Internal Error") w.WriteStatus(responseCodes["TEMPORARYFAILURE"], "Internal Error")
return return
} }

View File

@ -1,4 +1,4 @@
package main package logger
import "log" import "log"

11
main.go
View File

@ -9,25 +9,26 @@ import (
"git.yetaga.in/alazyreader/castor/certificates" "git.yetaga.in/alazyreader/castor/certificates"
"git.yetaga.in/alazyreader/castor/handlers" "git.yetaga.in/alazyreader/castor/handlers"
"git.yetaga.in/alazyreader/castor/logger"
) )
func handleConnection(log Logger, conn net.Conn, h handlers.Handler) { func handleConnection(log logger.Logger, conn net.Conn, h handlers.Handler) {
defer conn.Close() defer conn.Close()
scanner := bufio.NewScanner(conn) scanner := bufio.NewScanner(conn)
if ok := scanner.Scan(); !ok { if ok := scanner.Scan(); !ok {
log.Info(scanner.Err()) log.Debug(scanner.Err())
} }
u, err := url.Parse(scanner.Text()) u, err := url.Parse(scanner.Text())
if err != nil { if err != nil {
log.Info(err) log.Debug(err)
} }
w := handlers.NewResponse(conn) w := handlers.NewResponse(conn)
r := handlers.NewRequest(u) r := handlers.NewRequest(u)
handlers.RecoveryHandler(h).Handle(w, r) handlers.RecoveryHandler(log, h).Handle(w, r)
} }
func main() { func main() {
log := NewLogger(true) log := logger.NewLogger(true)
mime.AddExtensionType(".gemini", "text/gemini") mime.AddExtensionType(".gemini", "text/gemini")
mime.AddExtensionType(".gmi", "text/gemini") mime.AddExtensionType(".gmi", "text/gemini")