♻️ Refactored main entrypoint and http endpoints schema

This commit is contained in:
Maxim Lebedev 2023-08-07 09:08:44 +06:00
parent ed78b50f4e
commit 6baff22775
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
4 changed files with 56 additions and 46 deletions

View File

@ -268,18 +268,8 @@ func (h *Handler) handleExchange(w http.ResponseWriter, r *http.Request) {
return
}
var userInfo *AuthProfileResponse
if profile != nil {
userInfo = &AuthProfileResponse{
Email: profile.GetEmail(),
Photo: &domain.URL{URL: profile.GetPhoto()},
URL: &domain.URL{URL: profile.GetURL()},
Name: profile.GetName(),
}
}
_ = encoder.Encode(&AuthExchangeResponse{
Me: *me,
Profile: userInfo,
Me: me.String(),
Profile: NewAuthProfileResponse(profile),
})
}

View File

@ -79,15 +79,15 @@ type (
}
AuthExchangeResponse struct {
Me domain.Me `json:"me"`
Profile *AuthProfileResponse `json:"profile,omitempty"`
Me string `json:"me"`
}
AuthProfileResponse struct {
Email *domain.Email `json:"email,omitempty"`
Photo *domain.URL `json:"photo,omitempty"`
URL *domain.URL `json:"url,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
Photo string `json:"photo,omitempty"`
URL string `json:"url,omitempty"`
Name string `json:"name,omitempty"`
}
)
@ -192,3 +192,27 @@ func (r *AuthExchangeRequest) bind(req *http.Request) error {
return nil
}
func NewAuthProfileResponse(in *domain.Profile) *AuthProfileResponse {
out := new(AuthProfileResponse)
if in == nil {
return out
}
out.Name = in.Name
if in.URL != nil {
out.URL = in.URL.String()
}
if in.Email != nil {
out.Email = in.Email.String()
}
if in.Photo != nil {
out.Photo = in.Photo.String()
}
return out
}

View File

@ -38,6 +38,7 @@ type Dependencies struct {
config *domain.Config
}
//nolint:funlen
func TestAuthorize(t *testing.T) {
t.Parallel()
@ -98,7 +99,7 @@ func TestAuthorize(t *testing.T) {
t.Errorf("%s %s = %d, want %d", req.Method, u.String(), resp.StatusCode, http.StatusOK)
}
expResult := `Authorize ` + client.GetName()
expResult := `Authorize ` + client.Name
if result := string(body); !strings.Contains(result, expResult) {
t.Errorf("%s %s = %s, want %s", req.Method, u.String(), result, expResult)
}

51
main.go
View File

@ -87,18 +87,14 @@ var (
// NOTE(toby3d): write logs in stdout, see: https://12factor.net/logs
logger = log.New(os.Stdout, "IndieAuth\t", log.Lmsgprefix|log.LstdFlags|log.LUTC)
// NOTE(toby3d): read configuration from environment, see: https://12factor.net/config
config = new(domain.Config)
indieAuthClient = &domain.Client{
ID: domain.ClientID{},
Logo: make([]*url.URL, 1),
RedirectURI: make([]*url.URL, 1),
URL: make([]*url.URL, 1),
Name: make([]string, 0),
}
config = new(domain.Config)
)
//nolint:gochecknoglobals
var cpuProfilePath, memProfilePath string
var (
indieAuthClient *domain.Client
cpuProfilePath, memProfilePath string
)
//go:embed web/static/*
var static embed.FS
@ -109,31 +105,27 @@ func init() {
flag.StringVar(&memProfilePath, "memprofile", "", "set path to saving pprof memory profile")
flag.Parse()
if err := env.ParseWithOptions(config, env.Options{Prefix: "AUTH_"}); err != nil {
if err := env.ParseWithOptions(config, env.Options{Prefix: "INDIEAUTH_"}); err != nil {
logger.Fatalln(err)
}
// NOTE(toby3d): The server instance itself can be as a client.
rootURL := config.Server.GetRootURL()
indieAuthClient.Name = []string{config.Name}
rootUrl, err := url.Parse(config.Server.GetRootURL())
if err != nil {
logger.Fatalln(err)
}
cid, err := domain.ParseClientID(rootURL)
cid, err := domain.ParseClientID(rootUrl.String())
if err != nil {
logger.Fatalln("fail to read config:", err)
}
indieAuthClient.ID = *cid
if indieAuthClient.URL[0], err = url.Parse(rootURL); err != nil {
logger.Fatalln("cannot parse root URL as client URL:", err)
}
if indieAuthClient.Logo[0], err = url.Parse(rootURL + "icon.svg"); err != nil {
logger.Fatalln("cannot parse root URL as client URL:", err)
}
if indieAuthClient.RedirectURI[0], err = url.Parse(rootURL + "callback"); err != nil {
logger.Fatalln("cannot parse root URL as client URL:", err)
indieAuthClient = &domain.Client{
Logo: rootUrl.JoinPath("icon.svg"),
URL: rootUrl,
ID: *cid,
Name: config.Name,
RedirectURI: []*url.URL{rootUrl.JoinPath("callback")},
}
}
@ -171,9 +163,7 @@ func main() {
opts.Client = new(http.Client)
opts.Clients = clienthttprepo.NewHTTPClientRepository(opts.Client)
opts.Profiles = profilehttprepo.NewHTPPClientRepository(opts.Client)
app := NewApp(opts)
server := &http.Server{
Addr: config.Server.GetAddress(),
BaseContext: nil,
@ -210,7 +200,12 @@ func main() {
logger.Printf("started at %s, available at %s", config.Server.GetAddress(),
config.Server.GetRootURL())
err = server.ListenAndServe()
if config.Server.CertificateFile != "" && config.Server.KeyFile != "" {
err = server.ListenAndServeTLS(config.Server.CertificateFile, config.Server.KeyFile)
} else {
err = server.ListenAndServe()
}
if err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Fatalln("cannot listen and serve:", err)
}