-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: project list refactor
- Loading branch information
chancel.yang
committed
Jun 13, 2022
1 parent
1ec01c9
commit 9f058c0
Showing
7 changed files
with
185 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
.vscode | ||
go-tools-api | ||
dist/ | ||
.goreleaser.yml | ||
*.log | ||
conf/app.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[general] | ||
host = 0.0.0.0 | ||
port = 8042 | ||
|
||
[image] | ||
path = /home/chancel/images |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package controllers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"math/rand" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
|
||
"go-tools-api/models" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
var ImagePath string | ||
|
||
// Handler | ||
func IP(c echo.Context) error { | ||
r := &models.Response{ | ||
Status: 1, | ||
Msg: "查询成功", | ||
Data: map[string]interface{}{ | ||
"ip": c.RealIP(), | ||
}, | ||
} | ||
return c.JSON(http.StatusOK, r) | ||
} | ||
|
||
func Telegram(c echo.Context) (err error) { | ||
t := new(models.Telegram) | ||
if err = c.Bind(t); err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, err.Error()) | ||
} | ||
url := fmt.Sprintf("https://api.telegram.org/bot%s/SendMessage?text=%s&chat_id=%s&parse_mode=MarkdownV2", t.ApiKey, t.MsgText, t.ChatID) | ||
resp, gErr := http.Get(url) | ||
if gErr != nil { | ||
r := &models.Response{ | ||
Status: 0, | ||
Msg: gErr.Error(), | ||
Data: nil, | ||
} | ||
return c.JSON(http.StatusOK, r) | ||
} | ||
defer resp.Body.Close() | ||
body, _ := ioutil.ReadAll(resp.Body) | ||
var telegramResponse models.TelegramResponse | ||
json.Unmarshal(body, &telegramResponse) | ||
if !telegramResponse.Ok { | ||
r := &models.Response{ | ||
Status: 0, | ||
Msg: telegramResponse.Description, | ||
Data: telegramResponse.Result, | ||
} | ||
return c.JSON(http.StatusOK, r) | ||
} | ||
r := &models.Response{ | ||
Status: 1, | ||
Msg: "消息发送成功", | ||
Data: telegramResponse.Result, | ||
} | ||
return c.JSON(http.StatusOK, r) | ||
} | ||
|
||
var imageIndex int | ||
|
||
func Image(c echo.Context) error { | ||
var files []string | ||
|
||
err := filepath.Walk(ImagePath, func(path string, info os.FileInfo, err error) error { | ||
files = append(files, path) | ||
return nil | ||
}) | ||
if err != nil || len(files) < 2 { | ||
r := &models.Response{ | ||
Status: 0, | ||
Msg: "Get image error", | ||
Data: nil, | ||
} | ||
return c.JSON(http.StatusOK, r) | ||
} | ||
for { | ||
randomIndex := rand.Intn(len(files) - 1) | ||
if randomIndex != imageIndex { | ||
imageIndex = randomIndex | ||
break | ||
} | ||
} | ||
return c.File(files[imageIndex]) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"io/ioutil" | ||
"go-tools-api/controllers" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/go-ini/ini" | ||
"github.com/labstack/echo/v4" | ||
"github.com/labstack/echo/v4/middleware" | ||
) | ||
|
||
type Response struct { | ||
Status int `json:"status"` | ||
Msg string `json:"msg"` | ||
Data map[string]interface{} `json:"data"` | ||
} | ||
|
||
type Telegram struct { | ||
ApiKey string `json:"apiKey"` | ||
ChatID string `json:"chatId"` | ||
MsgText string `json:"msgText"` | ||
} | ||
|
||
type TelegramResponse struct { | ||
Ok bool `json:"ok"` | ||
Result map[string]interface{} `json:"result"` | ||
ErrorCode int `json:"error_code"` | ||
Description string `json:"description"` | ||
} | ||
|
||
var ( | ||
h bool | ||
host string | ||
port int | ||
) | ||
|
||
func init() { | ||
flag.BoolVar(&h, "h", false, "this help") | ||
flag.StringVar(&host, "host", "127.0.0.1", "listen host") | ||
flag.IntVar(&port, "port", 1323, "listen port") | ||
} | ||
|
||
func main() { | ||
h := flag.Bool("h", false, "--help") | ||
conf := flag.String("c", "app.conf", "app.conf path") | ||
flag.Parse() | ||
|
||
if h { | ||
if *h { | ||
flag.Usage() | ||
} | ||
_, err := os.Stat(*conf) | ||
if err != nil { | ||
fmt.Println("app.conf not found") | ||
os.Exit(-1) | ||
} | ||
|
||
cfg, err := ini.Load(*conf) | ||
if err != nil { | ||
log.Fatal("Fail to read file: ", err) | ||
os.Exit(-1) | ||
} | ||
|
||
host := cfg.Section("general").Key("host").String() | ||
port, err := cfg.Section("general").Key("port").Int() | ||
if err != nil { | ||
log.Fatal("Read conf error: ", err) | ||
os.Exit(-1) | ||
} | ||
|
||
// Echo instance | ||
e := echo.New() | ||
|
||
// Middleware | ||
e.Use(middleware.Logger()) | ||
fileWriter, err := os.OpenFile("ta.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0755) | ||
fileWriter, err := os.OpenFile("go-tools-api-running.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0755) | ||
if err != nil { | ||
log.Fatalf("create file ta.log failed:%v", err.Error()) | ||
log.Fatalf("create file go-tools-api-running.log failed:%v", err.Error()) | ||
} | ||
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ | ||
Format: "${time_rfc3339} - ${remote_ip} - ${method} - ${status} - ${uri}\n", | ||
Output: fileWriter, | ||
})) | ||
e.Use(middleware.Recover()) | ||
|
||
controllers.ImagePath = cfg.Section("image").Key("path").String() | ||
|
||
// Routes | ||
e.GET("/rest/api/v1/ip", ip) | ||
e.GET("/rest/api/v1/ip", controllers.IP) | ||
|
||
e.POST("/rest/api/v1/telegram", controllers.Telegram) | ||
|
||
e.POST("/rest/api/v1/telegram", telegram) | ||
e.GET("/rest/api/v1/image", controllers.Image) | ||
|
||
// Start server | ||
e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%d", host, port))) | ||
} | ||
|
||
// Handler | ||
func ip(c echo.Context) error { | ||
r := &Response{ | ||
Status: 1, | ||
Msg: "查询成功", | ||
Data: map[string]interface{}{ | ||
"ip": c.RealIP(), | ||
}, | ||
} | ||
return c.JSON(http.StatusOK, r) | ||
} | ||
|
||
func telegram(c echo.Context) (err error) { | ||
t := new(Telegram) | ||
if err = c.Bind(t); err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, err.Error()) | ||
} | ||
url := fmt.Sprintf("https://api.telegram.org/bot%s/SendMessage?text=%s&chat_id=%s&parse_mode=MarkdownV2", t.ApiKey, t.MsgText, t.ChatID) | ||
resp, gErr := http.Get(url) | ||
if gErr != nil { | ||
r := &Response{ | ||
Status: 0, | ||
Msg: gErr.Error(), | ||
Data: nil, | ||
} | ||
return c.JSON(http.StatusOK, r) | ||
} | ||
defer resp.Body.Close() | ||
body, _ := ioutil.ReadAll(resp.Body) | ||
var telegramResponse TelegramResponse | ||
json.Unmarshal(body, &telegramResponse) | ||
if !telegramResponse.Ok { | ||
r := &Response{ | ||
Status: 0, | ||
Msg: telegramResponse.Description, | ||
Data: telegramResponse.Result, | ||
} | ||
return c.JSON(http.StatusOK, r) | ||
} | ||
r := &Response{ | ||
Status: 1, | ||
Msg: "消息发送成功", | ||
Data: telegramResponse.Result, | ||
} | ||
return c.JSON(http.StatusOK, r) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package models | ||
|
||
type Response struct { | ||
Status int `json:"status"` | ||
Msg string `json:"msg"` | ||
Data map[string]interface{} `json:"data"` | ||
} | ||
|
||
type Telegram struct { | ||
ApiKey string `json:"apiKey"` | ||
ChatID string `json:"chatId"` | ||
MsgText string `json:"msgText"` | ||
} | ||
|
||
type TelegramResponse struct { | ||
Ok bool `json:"ok"` | ||
Result map[string]interface{} `json:"result"` | ||
ErrorCode int `json:"error_code"` | ||
Description string `json:"description"` | ||
} |