Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
UlricQin committed Dec 17, 2015
0 parents commit 7634639
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

*.swp
*.swo
*.log
.idea
.DS_Store
/var
/falcon-mail-provider*
/mail-provider*
/cfg.local.json
/gitversion

10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mail-provider
=============

把smtp封装为一个简单http接口,配置到sender中用来发送报警邮件

## 使用方法

```
curl http://$ip:4000/sender/mail -d "tos=a@a.com,b@b.com&subject=xx&content=yy"
```
13 changes: 13 additions & 0 deletions cfg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"debug": true,
"http": {
"listen": "0.0.0.0:4000",
"token": "",
},
"smtp": {
"addr": "mail.example.com:25",
"username": "falcon@example.com",
"password": "123456",
"from": "falcon@example.com"
}
}
70 changes: 70 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package config

import (
"encoding/json"
"fmt"
"log"
"sync"

"github.com/toolkits/file"
)

type HttpConfig struct {
Listen string `json:"listen"`
Token string `json:"token"`
}

type SmtpConfig struct {
Addr string `json:"addr"`
Username string `json:"username"`
Password string `json:"password"`
From string `json:"from"`
}

type GlobalConfig struct {
Debug bool `json:"debug"`
Http *HttpConfig `json:"http"`
Smtp *SmtpConfig `json:"smtp"`
}

var (
ConfigFile string
config *GlobalConfig
configLock = new(sync.RWMutex)
)

func Config() *GlobalConfig {
configLock.RLock()
defer configLock.RUnlock()
return config
}

func Parse(cfg string) error {
if cfg == "" {
return fmt.Errorf("use -c to specify configuration file")
}

if !file.IsExist(cfg) {
return fmt.Errorf("configuration file %s is nonexistent", cfg)
}

ConfigFile = cfg

configContent, err := file.ToTrimString(cfg)
if err != nil {
return fmt.Errorf("read configuration file %s fail %s", cfg, err.Error())
}

var c GlobalConfig
err = json.Unmarshal([]byte(configContent), &c)
if err != nil {
return fmt.Errorf("parse configuration file %s fail %s", cfg, err.Error())
}

configLock.Lock()
defer configLock.Unlock()
config = &c

log.Println("load configuration file", cfg, "successfully")
return nil
}
5 changes: 5 additions & 0 deletions config/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package config

const (
VERSION = "0.0.1"
)
129 changes: 129 additions & 0 deletions control
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/bin/bash

WORKSPACE=$(cd $(dirname $0)/; pwd)
cd $WORKSPACE

mkdir -p var

module=mail-provider
app=falcon-$module
conf=cfg.json
localconf=cfg.local.json
pidfile=var/app.pid
logfile=var/app.log

function check_pid() {
if [ -f $pidfile ];then
pid=`cat $pidfile`
if [ -n $pid ]; then
running=`ps -p $pid|grep -v "PID TTY" |wc -l`
return $running
fi
fi
return 0
}

function start() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo -n "$app now is running already, pid="
cat $pidfile
return 1
fi

c=$conf
if [ -f $localconf ];then
c=$localconf
fi
nohup ./$app -c $c &> $logfile &
echo $! > $pidfile
echo "$app started..., pid=$!"
}

function stop() {
pid=`cat $pidfile`
kill $pid
echo "$app stoped..."
}

function restart() {
stop
sleep 1
start
}

function status() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo "started"
else
echo "stoped"
fi
}

function tailf() {
tail -f $logfile
}

function build() {
go build
if [ $? -ne 0 ]; then
exit $?
fi
mv $module $app
./$app -v
}

function pack() {
build
version=`./$app -v`
tar zcvf $app-$version.tar.gz control cfg.json $app
}

function packbin() {
build
version=`./$app -v`
tar zcvf $app-bin-$version.tar.gz $app
}

function help() {
echo "$0 pid|reload|build|pack|packbin|start|stop|restart|status|tail"
}

function pid() {
cat $pidfile
}

function reload() {
build
restart
tailf
}

if [ "$1" == "" ]; then
help
elif [ "$1" == "stop" ];then
stop
elif [ "$1" == "start" ];then
start
elif [ "$1" == "restart" ];then
restart
elif [ "$1" == "status" ];then
status
elif [ "$1" == "tail" ];then
tailf
elif [ "$1" == "build" ];then
build
elif [ "$1" == "pack" ];then
pack
elif [ "$1" == "packbin" ];then
packbin
elif [ "$1" == "pid" ];then
pid
elif [ "$1" == "reload" ];then
reload
else
help
fi
17 changes: 17 additions & 0 deletions http/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package http

import (
"net/http"

"github.com/open-falcon/mail-provider/config"
)

func configCommonRoutes() {
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
})

http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(config.VERSION))
})
}
27 changes: 27 additions & 0 deletions http/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package http

import (
"log"
"net/http"
_ "net/http/pprof"

"github.com/open-falcon/mail-provider/config"
)

func init() {
configCommonRoutes()
configProcRoutes()
}

func Start() {
addr := config.Config().Http.Listen
if addr == "" {
return
}
s := &http.Server{
Addr: addr,
MaxHeaderBytes: 1 << 30,
}
log.Println("http listening", addr)
log.Fatalln(s.ListenAndServe())
}
36 changes: 36 additions & 0 deletions http/mail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package http

import (
"net/http"
"strings"

"github.com/open-falcon/mail-provider/config"
"github.com/toolkits/smtp"
"github.com/toolkits/web/param"
)

func configProcRoutes() {

http.HandleFunc("/sender/mail", func(w http.ResponseWriter, r *http.Request) {
cfg := config.Config()
token := param.String(r, "token", "")
if cfg.Http.Token != token {
http.Error(w, "no privilege", http.StatusForbidden)
return
}

tos := param.MustString(r, "tos")
subject := param.MustString(r, "subject")
content := param.MustString(r, "content")
tos = strings.Replace(tos, ",", ";", -1)

s := smtp.New(cfg.Smtp.Addr, cfg.Smtp.Username, cfg.Smtp.Password)
err := s.SendMail(cfg.Smtp.From, tos, subject, content)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
http.Error(w, "success", http.StatusOK)
}
})

}
Loading

0 comments on commit 7634639

Please sign in to comment.