Преглед изворни кода

Migrate to proto description for MailHeader

Alexey Edelev пре 5 година
родитељ
комит
2bada054ff
3 измењених фајлова са 47 додато и 19 уклоњено
  1. 15 0
      build.sh
  2. 12 0
      gostfix.proto
  3. 20 19
      main.go

+ 15 - 0
build.sh

@@ -0,0 +1,15 @@
+export GOPATH=$PWD
+export PATH=$PATH:$PWD/bin
+export GOBIN=$PWD/bin
+export RPC_PATH=$PWD
+
+go get github.com/golang/protobuf/protoc-gen-go
+go install ./src/github.com/golang/protobuf/protoc-gen-go
+
+# mkdir -p $RPC_PATH
+rm -f $RPC_PATH/*.pb.go
+protoc -I$RPC_PATH --go_out=plugins=grpc:$RPC_PATH $RPC_PATH/gostfix.proto
+
+go get -v
+go build -o $GOBIN/gostfix
+

+ 12 - 0
gostfix.proto

@@ -0,0 +1,12 @@
+syntax = "proto3";
+
+package main;
+
+message MailHeader {
+	string From = 1;
+	string To = 2;
+	string Cc = 3;
+	string Bcc = 4;
+	string Date = 5;
+	string Subject = 6;
+}

+ 20 - 19
main.go

@@ -53,20 +53,20 @@ const (
 	UserRegExp          = "^[a-zA-Z][\\w0-9\\._]*"
 )
 
-type Email struct {
-	From        string
-	To          string
-	Cc          string
-	Bcc         string
-	Date        string
-	Subject     string
-	ContentType string
-	Body        string
-}
-
-func NewEmail() *Email {
-	return &Email{
-		ContentType: "plain/text",
+// type Email struct {
+// 	From        string
+// 	To          string
+// 	Cc          string
+// 	Bcc         string
+// 	Date        string
+// 	Subject     string
+// 	ContentType string
+// 	Body        string
+// }
+
+func NewEmail() *MailHeader {
+	return &MailHeader{
+		// ContentType: "plain/text",
 	}
 }
 
@@ -148,18 +148,19 @@ func (e *GofixEngine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 			scanner := bufio.NewScanner(file)
 			activeBoundary := ""
 			var previousHeader *string = nil
-			var emails []*Email
+			var emails []*MailHeader
+			lastContentType := "plain/text"
 			for email := NewEmail(); scanner.Scan(); {
 				if scanner.Text() == "" {
 					if state == StateHeaderScan {
-						boundaryCapture := boundaryFinder.FindStringSubmatch(email.ContentType)
+						boundaryCapture := boundaryFinder.FindStringSubmatch(lastContentType)
 						if len(boundaryCapture) == 2 {
 							activeBoundary = boundaryCapture[1]
 						} else {
 							activeBoundary = ""
 						}
 						state = StateBodyScan
-						// fmt.Printf("--------------------------Start body scan content type:%s boundary: %s -------------------------\n", email.ContentType, activeBoundary)
+						// fmt.Printf("--------------------------Start body scan content type:%s boundary: %s -------------------------\n", lastContentType, activeBoundary)
 					} else if state == StateBodyScan {
 						// fmt.Printf("--------------------------Previous email-------------------------\n%v\n", email)
 
@@ -192,7 +193,7 @@ func (e *GofixEngine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 						case "date":
 							previousHeader = &email.Date
 						case "content-type":
-							previousHeader = &email.ContentType
+							previousHeader = &lastContentType
 						default:
 							previousHeader = nil
 						}
@@ -208,7 +209,7 @@ func (e *GofixEngine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 						continue
 					}
 				} else {
-					email.Body += scanner.Text() + "\n"
+					// email.Body += scanner.Text() + "\n"
 					if activeBoundary != "" {
 						capture := boundaryEndFinder.FindStringSubmatch(scanner.Text())
 						if len(capture) == 2 {