Files
ippserver/server/main.go
2021-01-09 12:10:43 +01:00

70 lines
1.6 KiB
Go

package main
import (
"context"
"ippserver/packages/ipp"
"ippserver/packages/mdnsserver"
"net/http"
log "github.com/sirupsen/logrus"
)
func main() {
customFormatter := new(log.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
log.SetFormatter(customFormatter)
customFormatter.FullTimestamp = true
log.SetLevel(log.InfoLevel)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go mdnsserver.Run(ctx)
http.HandleFunc("/ipp/print", handle)
log.Info("http server started on :1234")
err := http.ListenAndServe(":1234", nil)
if err != nil {
log.Fatal("ListenAndServe: " + err.Error())
}
}
func handle(w http.ResponseWriter, r *http.Request) {
log.Infoln("handle")
if r.Method != http.MethodPost {
http.Error(w, "Unsupported method", http.StatusMethodNotAllowed)
}
log.Info(r.Header)
//body := make([]byte, r.ContentLength)
//io.ReadFull(r.Body, body)
//log.Infof("Body %x", body)
request := ipp.NewRequest(0, 0)
//rdata := make([]byte, r.ContentLength)
//io.ReadFull(r.Body, rdata)
//log.Printf("Data %x", rdata)
//fmt.Printf("data % #0x", rdata)
//buf := bytes.NewBuffer(rdata)
request.UnMarshal(r.Body)
log.Infof("Request: \n%v\n", request)
var response *ipp.Response
switch request.Operation() {
case ipp.GetPrinterAttributes:
response = handleGetPrinterAttributes(request)
case ipp.PrintJob:
response = handlePrintJob(request, r.Body)
case ipp.GetJobs:
response = handleGetJobs(request)
default:
response = ipp.NewResponse(ipp.ClientErrorBadRequest, request.RequestID())
}
log.Infof("Response:\n%v\n", response)
data := response.Marshal()
//log.Debugf("% x", data)
w.Write(data)
}