-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from LyricTian/develop
Add fasthttp server handle
- Loading branch information
Showing
6 changed files
with
365 additions
and
76 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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/valyala/fasthttp" | ||
"gopkg.in/oauth2.v2/manage" | ||
"gopkg.in/oauth2.v2/models" | ||
"gopkg.in/oauth2.v2/server" | ||
"gopkg.in/oauth2.v2/store/client" | ||
"gopkg.in/oauth2.v2/store/token" | ||
) | ||
|
||
func main() { | ||
// 创建基于redis的oauth2管理实例 | ||
manager := manage.NewRedisManager( | ||
&token.RedisConfig{Addr: "192.168.33.70:6379"}, | ||
) | ||
// 使用临时客户端存储 | ||
manager.MapClientStorage(client.NewTempStore(&models.Client{ | ||
ID: "222222", | ||
Secret: "22222222", | ||
Domain: "http://localhost:9094", | ||
})) | ||
|
||
srv := server.NewFastServer(server.NewConfig(), manager) | ||
|
||
log.Println("OAuth2 server is running at 9096 port.") | ||
fasthttp.ListenAndServe(":9096", func(ctx *fasthttp.RequestCtx) { | ||
switch string(ctx.Request.URI().Path()) { | ||
case "/authorize": | ||
authReq, err := srv.GetAuthorizeRequest(ctx) | ||
if err != nil { | ||
ctx.Error(err.Error(), 400) | ||
return | ||
} | ||
authReq.UserID = "000000" | ||
// TODO: 登录验证、授权处理 | ||
err = srv.HandleAuthorizeRequest(ctx, authReq) | ||
if err != nil { | ||
ctx.Error(err.Error(), 400) | ||
} | ||
case "/token": | ||
err := srv.HandleTokenRequest(ctx) | ||
if err != nil { | ||
ctx.Error(err.Error(), 400) | ||
} | ||
} | ||
}) | ||
} |
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
Oops, something went wrong.