-
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.
- Loading branch information
Showing
6 changed files
with
196 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# we-mid/go | ||
|
||
> Go util/SDK 合集 | ||
代码仓库: | ||
|
||
- Gitee:https://gitee.com/we-mid/go | ||
- GitHub:https://github.com/we-mid/go | ||
|
||
文件目录: | ||
|
||
- [basic_auth](./basic_auth):HTTP Basic-Auth 辅助函数库 | ||
- [bd_fanyi](./bd_fanyi):简单易用的 百度翻译 SDK | ||
- [tc_sms](./tc_sms):简单易用的 腾讯云SMS短信发送 SDK | ||
- [util](./util) | ||
- ...更多 |
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,44 @@ | ||
# basic_auth | ||
|
||
> HTTP Basic-Auth 辅助函数库 | ||
```ini | ||
# .env | ||
BASICAUTH_USERLIST='[{"user":"xxx","pass":"xxxxxx"},{"user":"xxx","pass":"xxxxxx"}]' | ||
``` | ||
|
||
```go | ||
import ( | ||
// ... | ||
"gitee.com/we-mid/go/basic_auth" | ||
"gitee.com/we-mid/go/util" | ||
) | ||
|
||
func init() { | ||
if err := basic_auth.InitFromEnv(); err != nil { | ||
log.Fatalln("basic_auth.InitFromEnv:", err) | ||
} | ||
} | ||
|
||
func main() { | ||
// API路由 | ||
http.HandleFunc("/api/foo", util.HandlerWrap(apiFoo)) | ||
http.HandleFunc("/api/bar", util.HandlerWrap(apiBar)) | ||
// 页面路由,创建一个文件服务器 | ||
fs := http.FileServer(http.Dir("./public")) | ||
http.HandleFunc("/", basic_auth.Wrap(func(w http.ResponseWriter, r *http.Request) { | ||
fs.ServeHTTP(w, r) | ||
})) | ||
// ... | ||
} | ||
|
||
func apiFoo(w http.ResponseWriter, r *http.Request) error { | ||
if err := util.EnableCors(w, r); err != nil { | ||
return err | ||
} | ||
if user := basic_auth.Check(r); user == "" { | ||
return util.Err401 | ||
} | ||
// ... | ||
} | ||
``` |
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,58 @@ | ||
package basic_auth | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
type HandlerFunc func(http.ResponseWriter, *http.Request) | ||
|
||
type User struct { | ||
Username string `json:"user"` | ||
Password string `json:"pass"` | ||
} | ||
|
||
var ( | ||
userMap = map[string]User{} | ||
) | ||
|
||
func AddUser(user User) { | ||
userMap[user.Username] = user | ||
} | ||
|
||
func InitFromEnv() error { | ||
var list []User | ||
JSON := os.Getenv("BASICAUTH_USERLIST") | ||
if err := json.Unmarshal([]byte(JSON), &list); err != nil { | ||
return err | ||
} | ||
for _, user := range list { | ||
AddUser(user) | ||
} | ||
return nil | ||
} | ||
|
||
func Wrap(next HandlerFunc) HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
user := Check(r) | ||
if user == "" { // reject | ||
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`) | ||
http.Error(w, "Unauthorized.", http.StatusUnauthorized) | ||
return | ||
} | ||
next(w, r) // pass | ||
} | ||
} | ||
|
||
func Check(r *http.Request) string { | ||
username, password, ok := r.BasicAuth() | ||
if ok { | ||
if user, ok := userMap[username]; ok { | ||
if password == user.Password { | ||
return username | ||
} | ||
} | ||
} | ||
return "" | ||
} |
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,3 @@ | ||
module gitee.com/we-mid/go/basic_auth | ||
|
||
go 1.21.1 |
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,62 @@ | ||
# bd_fanyi | ||
|
||
> 简单易用的 百度翻译 SDK | ||
```ini | ||
# .env | ||
BAIDUFANYI_APP_ID=xxxxxxxx | ||
BAIDUFANYI_APP_SECRET=xxxxxxxx | ||
``` | ||
|
||
```go | ||
import "gitee.com/we-mid/go/bd_fanyi" | ||
|
||
func init() { | ||
bd_fanyi.InitFromEnv() | ||
} | ||
|
||
func handleFanyi(w http.ResponseWriter, r *http.Request) (any, error) { | ||
defer r.Body.Close() | ||
// ... | ||
var req bd_fanyi.FanyiReq | ||
if err := json.NewDecoder(r.Body).Decode(&req); err!=nil { | ||
return nil, fmt.Errorf("json.Decode: %w", err) | ||
} | ||
bd_fanyi.CompleteReq(&req) | ||
// list: []*bd_fanyi.TranslateResult | ||
list, err := client.Translate(&req) | ||
if err != nil { | ||
return nil, fmt.Errorf("client.Translate: %w", err) | ||
} | ||
return list, nil | ||
} | ||
``` | ||
|
||
```go | ||
// bd_fanyi.FanyiReq: | ||
type FanyiReq struct { | ||
Text string `json:"text"` | ||
From baidufanyi.Language `json:"from"` | ||
To baidufanyi.Language `json:"to"` | ||
} | ||
// bd_fanyi.Language = baidufanyi.Language: | ||
// https://github.com/chyroc/baidufanyi/blob/master/language.go | ||
LanguageEn Language = "en" // 英 | ||
LanguageCht Language = "cht" // 中文(繁体) | ||
LanguageWyw Language = "wyw" // 中文(文言文) | ||
LanguageYue Language = "yue" // 中文(粤语) | ||
LanguageZh Language = "zh" // 中文(简体) | ||
|
||
// bd_fanyi.TranslateResult = baidufanyi.TranslateResult: | ||
// https://github.com/chyroc/baidufanyi/blob/master/translate.go | ||
type TranslateResult struct { | ||
Src string `json:"src"` | ||
Dst string `json:"dst"` | ||
} | ||
``` | ||
|
||
### 参考链接 | ||
|
||
- 通用翻译API接入文档:https://fanyi-api.baidu.com/doc/21 | ||
- 通用翻译API版本权益调整通知(计费标准):https://fanyi-api.baidu.com/doc/8 | ||
- 本SDK基于/二次封装了baidufanyi:https://github.com/chyroc/baidufanyi |
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