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

View File

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

11
main.go
View File

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