53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"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
|
|
|
|
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)
|
|
|
|
}
|
|
// body := make([]byte, r.ContentLength)
|
|
// io.ReadFull(r.Body, body)
|
|
// log.Infof("Body %x", body)
|
|
|
|
request := ipp.NewRequest()
|
|
request.UnMarshal(r.Body)
|
|
fmt.Printf("%v", request)
|
|
response := ipp.NewResponse(ipp.SuccessfulOk, request.RequestId())
|
|
a := ipp.NewCharSetValue("attributes-charset", "utf-8")
|
|
response.AddOperatonAttribute(a)
|
|
data := response.Marshal()
|
|
log.Infof("% x", data)
|
|
w.Write(data)
|
|
|
|
}
|