Skip to content

Commit

Permalink
update workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
Glowman554 committed May 27, 2024
1 parent 2498c88 commit bba20b3
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Build

on:
push:
branches: [ "main" ]
branches: [ "compiler" ]
pull_request:
branches: [ "main" ]
branches: [ "compiler" ]

jobs:

Expand Down Expand Up @@ -34,4 +34,4 @@ jobs:
uses: actions/upload-artifact@v2
with:
name: compiler
path: fire/fire.*
path: fire/fire.*
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Test

on:
push:
branches: [ "main" ]
branches: [ "compiler" ]
pull_request:
branches: [ "main" ]
branches: [ "compiler" ]

jobs:

Expand Down
106 changes: 106 additions & 0 deletions frontend/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package frontend

import (
"context"
"io"
"net/http"
"time"

"encore.app/authentication"
"encore.app/frontend/templates"
"encore.dev/beta/auth"
"encore.dev/rlog"
"github.com/a-h/templ"
)

//encore:service
type Service struct {
mux *http.ServeMux
}

func initService() (*Service, error) {
mux := http.NewServeMux()

mux.HandleFunc("/frontend/", renderedLayout(templates.LayoutProps{Title: "FireStorm - Home"}, nofail(templates.Index)))
mux.HandleFunc("/frontend/authentication", renderedLayout(templates.LayoutProps{Title: "FireStorm - Account"}, nofail(templates.AccountPage)))
mux.HandleFunc("/frontend/authentication/create", wrap(CreateAccount))
mux.HandleFunc("/frontend/authentication/login", wrap(LoginAccount))
mux.HandleFunc("/frontend/authentication/delete", wrap(DeleteAccount))
mux.HandleFunc("/frontend/authentication/password/change", wrap(ChangePasswordAccount))

mux.HandleFunc("/frontend/package", renderedLayout(templates.LayoutProps{Title: "FireStorm - Packages"}, nofail(templates.PackagesPage)))
mux.HandleFunc("/frontend/package/create", wrap(CreatePackage))
mux.HandleFunc("/frontend/package/list", wrap(ListPackage))
mux.HandleFunc("/frontend/package/show", renderedLayout(templates.LayoutProps{Title: "FireStorm - Package"}, PackagePage))
mux.HandleFunc("/frontend/package/show/version", renderedLayout(templates.LayoutProps{Title: "FireStorm - Home"}, PackageVersionPage))
mux.HandleFunc("/frontend/package/show/file", renderedLayout(templates.LayoutProps{Title: "FireStorm - Package"}, PackageFileVersionPage))

return &Service{
mux: mux,
}, nil
}

func withContext(f func(context.Context) error) error {
context, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

return f(context)
}

func withBody(r *http.Request, f func([]byte) error) error {
defer r.Body.Close()

data, err := io.ReadAll(r.Body)
if err != nil {
return err
}

return f(data)
}

func wrap(f func(ctx context.Context, w http.ResponseWriter, r *http.Request) error) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
err := withContext(func(ctx context.Context) error {
return f(ctx, w, r)
})

if err != nil {
rlog.Error(err.Error())
w.Write([]byte(err.Error()))
}
}
}

func nofail(component func(*authentication.User, *http.Request) templ.Component) func(*authentication.User, *http.Request) (templ.Component, error) {
return func(u *authentication.User, r *http.Request) (templ.Component, error) {
return component(u, r), nil
}
}

func render(component templ.Component, ctx context.Context, w http.ResponseWriter) error {
return component.Render(ctx, w)
}

func renderedLayout(props templates.LayoutProps, component func(*authentication.User, *http.Request) (templ.Component, error)) func(w http.ResponseWriter, r *http.Request) {
return wrap(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
user, _ := auth.Data().(*authentication.User)
c, err := component(user, r)
if err != nil {
return err
}
return render(templates.Layout(props, c), ctx, w)
})

}

func rendered(component func(*authentication.User) templ.Component) func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
user, _ := auth.Data().(*authentication.User)
return render(component(user), ctx, w)
}
}

//encore:api public raw path=/frontend/*endpoint
func (service *Service) Serve(w http.ResponseWriter, req *http.Request) {
service.mux.ServeHTTP(w, req)
}

0 comments on commit bba20b3

Please sign in to comment.