1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| package main
import ( "bytes" "encoding/json" "flag" "io/ioutil" "log" "net/http" "os/exec" "path" "sync" )
var ( notesPath string hexoPath string lock sync.Mutex )
func init() { flag.StringVar(¬esPath, "notes", "./notes", "path where store notes") flag.StringVar(&hexoPath, "hexo", "./test/source/_posts/", "path where store hexo source post") }
type User struct { Name string `json:"name,omitempty"` Email string `json:"email,omitempty"` Username string `json:"username,omitempty"` }
type Commit struct { ID string `json:"id"` Tree_id string `json:"tree_id"` Distinct bool `json:"distinct"` Message string `json:"message,omitempty"` Timestamp string `json:"timestamp,omitempty"` Url string `json:"url,omitempty"` Author *User `json:"author,omitempty"` Committer *User `json:"committer,omitempty"` Added []string `json:"added,omitempty"` Removed []string `json:"removed,omitempty"` Modified []string `json:"modified,omitempty"` }
func runCmd(cmdStr string, args []string) error { cmd := exec.Command(cmdStr, args...) var out bytes.Buffer cmd.Stdout = &out err := cmd.Run() if err != nil { log.Printf("run %s failed: %s", cmdStr, err.Error()) return err } return nil }
func updateHandle(files []string) { for _, f := range files { _, fname := path.Split(f) runCmd("cp", []string{"-f", notesPath + "/" + f, hexoPath + "/" + fname}) } }
func removeHandle(files []string) { for _, f := range files { _, fname := path.Split(f) runCmd("rm", []string{"-f", hexoPath + "/" + fname}) } }
func syncGit() bool { err := runCmd("/bin/bash", []string{"-c", "cd " + notesPath + "; git pull"}) if err != nil { log.Printf("sync notes from git failed: %s\n", err.Error()) return false } return true }
func changes(data []byte) ([]string, []string) { var copys []string var rms []string var commit Commit
err := json.Unmarshal(data, &commit) if err != nil { log.Printf("Invalid commit: %s", string(data)) return nil, nil }
if len(commit.Added) > 0 { copys = commit.Added } if len(commit.Modified) > 0 { copys = append(copys, commit.Modified...) } if len(commit.Removed) > 0 { rms = commit.Removed }
log.Printf("copys: %v, removes: %v\n", copys, rms) return copys, rms }
func handleCommits(commits []interface{}) { var updates []string var removes []string for _, commit := range commits { data, err := json.Marshal(commit) if err != nil { log.Printf("Invalid commit: %v", commit) continue } us, rs := changes(data) updates = append(updates, us...) removes = append(removes, rs...) }
lock.Lock() defer lock.Unlock() if !syncGit() { return } updateHandle(updates) removeHandle(removes) }
func main() { flag.Parse() log.Printf("notes path: %s\n", notesPath) log.Printf("hexo path: %s\n", hexoPath)
helloHandler := func(w http.ResponseWriter, req *http.Request) { var fullData map[string]interface{}
robots, err := ioutil.ReadAll(req.Body) req.Body.Close() if err != nil { log.Fatal(err) }
if err := json.Unmarshal([]byte(robots), &fullData); err != nil { log.Fatal(err) } commits, ok := fullData["commits"] if !ok { log.Printf("Cannot found commits in %s", fullData) return } if t, ok := commits.([]interface{}); ok { handleCommits(t) } log.Println("get message") }
http.HandleFunc("/notes", helloHandler) log.Fatal(http.ListenAndServe(":8088", nil)) }
|