Skip to content

Commit

Permalink
🎨 archive fiber api & add svelte poc
Browse files Browse the repository at this point in the history
  • Loading branch information
zcubbs committed Aug 20, 2023
1 parent 77e86fe commit f951e60
Show file tree
Hide file tree
Showing 44 changed files with 3,264 additions and 264 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions _archives/api/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package api

import (
"github.com/stretchr/testify/require"
db "github.com/zcubbs/tlz/db/sqlc"
"github.com/zcubbs/tlz/internal/util"
"github.com/zcubbs/tlz/pkg/random"
"os"
"testing"
"time"
)

func newTestServer(t *testing.T, store db.Store) *Server {
config := util.Config{
Auth: util.AuthConfig{
TokenSymmetricKey: random.RandomString(32),
AccessTokenDuration: time.Minute,
RefreshTokenDuration: 5 * time.Minute,
},
}

server, err := NewServer(store, nil, config)
require.NoError(t, err)

return server
}

func TestMain(m *testing.M) {
os.Exit(m.Run())
}
File renamed without changes.
File renamed without changes.
111 changes: 111 additions & 0 deletions _archives/api/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package api

import (
"embed"
"fmt"
"github.com/charmbracelet/log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2/middleware/requestid"
db "github.com/zcubbs/tlz/db/sqlc"
"github.com/zcubbs/tlz/internal/util"
"github.com/zcubbs/tlz/pkg/charmlogfiber"
"github.com/zcubbs/tlz/pkg/token"
"net/http"
)

type Server struct {
store db.Store
app *fiber.App
tokenMaker token.Maker
cfg util.Config
staticEmbed *embed.FS
validate *XValidator
}

func NewServer(store db.Store, staticEmbed *embed.FS, cfg util.Config) (*Server, error) {
tokenMaker, err := token.NewPasetoMaker(cfg.Auth.TokenSymmetricKey)
if err != nil {
return nil, fmt.Errorf("cannot create new tokenMaker: %w", err)
}
s := &Server{
store: store,
tokenMaker: tokenMaker,
cfg: cfg,
staticEmbed: staticEmbed,
}

s.ApplyDefaultConfig()

return s, nil
}

func (s *Server) Start() error {
log.Info("starting HTTP server", "port", s.cfg.HttpServer.Port)
return s.app.Listen(fmt.Sprintf(":%d", s.cfg.HttpServer.Port))
}

func (s *Server) ApplyDefaultConfig() {
app := fiber.New(fiber.Config{
EnablePrintRoutes: s.cfg.HttpServer.EnablePrintRoutes,
DisableStartupMessage: true,
})

s.app = app
s.addValidator()
s.applyMiddleware()
s.addRoutes()
s.embedStatic()
}

func (s *Server) addRoutes() {
users := s.app.Group("/api/users")
users.Post("/login", s.loginUser)
users.Post("/", s.createUser)

tokens := s.app.Group("/api/tokens")
tokens.Post("/refresh", s.renewAccessToken)

domains := s.app.Group("/api/domains")
domains.Use(AuthMiddleware(s.tokenMaker))
domains.Post("/", s.CreateDomain)
domains.Get("/", s.GetDomains)
}

func (s *Server) applyMiddleware() {
// Initialize default config
s.app.Use(cors.New())
s.app.Use(requestid.New())
// Logging Request ID
s.app.Use(requestid.New())
s.app.Use(charmlogfiber.New(log.Default()))

// Or extend your config for customization
s.app.Use(cors.New(cors.Config{
AllowOrigins: s.cfg.HttpServer.AllowOrigins,
AllowHeaders: s.cfg.HttpServer.AllowHeaders,
}))
}

func (s *Server) embedStatic() {
if s.staticEmbed != nil {
s.app.Use("/", filesystem.New(filesystem.Config{
Root: http.FS(s.staticEmbed),
PathPrefix: "web/dist",
}))
}
}

func (s *Server) addValidator() {
val := &XValidator{
validator: validate,
}

err := val.validator.RegisterValidation("domain-name", validDomainName)
if err != nil {
log.Fatal("cannot register domain-name validator", "error", err)
}

s.validate = val
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion gapi/authorization.go → api/authorization.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gapi
package api

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion gapi/converter.go → api/converter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gapi
package api

import (
db "github.com/zcubbs/tlz/db/sqlc"
Expand Down
2 changes: 1 addition & 1 deletion gapi/error.go → api/error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gapi
package api

import (
"google.golang.org/genproto/googleapis/rpc/errdetails"
Expand Down
2 changes: 1 addition & 1 deletion gapi/logger.go → api/logger.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gapi
package api

import (
"context"
Expand Down
25 changes: 20 additions & 5 deletions api/main_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package api

import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
db "github.com/zcubbs/tlz/db/sqlc"
"github.com/zcubbs/tlz/internal/util"
"github.com/zcubbs/tlz/pkg/random"
"os"
"github.com/zcubbs/tlz/pkg/token"
"github.com/zcubbs/tlz/worker"
"google.golang.org/grpc/metadata"
"testing"
"time"
)

func newTestServer(t *testing.T, store db.Store) *Server {
func newTestServer(t *testing.T, store db.Store, taskDistributor worker.TaskDistributor) *Server {
config := util.Config{
Auth: util.AuthConfig{
TokenSymmetricKey: random.RandomString(32),
Expand All @@ -19,12 +24,22 @@ func newTestServer(t *testing.T, store db.Store) *Server {
},
}

server, err := NewServer(store, nil, config)
server, err := NewServer(store, taskDistributor, config)
require.NoError(t, err)

return server
}

func TestMain(m *testing.M) {
os.Exit(m.Run())
func newContextWithBearerToken(t *testing.T, tokenMaker token.Maker, username string, userId uuid.UUID, duration time.Duration) context.Context {

Check failure on line 33 in api/main_test.go

View workflow job for this annotation

GitHub Actions / lint

func `newContextWithBearerToken` is unused (unused)
accessToken, _, err := tokenMaker.CreateToken(username, userId, duration)
require.NoError(t, err)

bearerToken := fmt.Sprintf("%s %s", authorizationBearer, accessToken)
md := metadata.MD{
authorizationHeader: []string{
bearerToken,
},
}

return metadata.NewIncomingContext(context.Background(), md)
}
2 changes: 1 addition & 1 deletion gapi/metadata.go → api/metadata.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gapi
package api

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion gapi/rpc_create_user.go → api/rpc_create_user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gapi
package api

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gapi
package api

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion gapi/rpc_login_user.go → api/rpc_login_user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gapi
package api

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion gapi/rpc_ping.go → api/rpc_ping.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gapi
package api

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion gapi/rpc_update_user.go → api/rpc_update_user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gapi
package api

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion gapi/rpc_verify_user.go → api/rpc_verify_user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gapi
package api

import (
"context"
Expand Down
Loading

0 comments on commit f951e60

Please sign in to comment.