-
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.
🎨 archive fiber api & add svelte poc
- Loading branch information
Showing
44 changed files
with
3,264 additions
and
264 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,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.
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,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.
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gapi | ||
package api | ||
|
||
import ( | ||
"context" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gapi | ||
package api | ||
|
||
import ( | ||
db "github.com/zcubbs/tlz/db/sqlc" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gapi | ||
package api | ||
|
||
import ( | ||
"google.golang.org/genproto/googleapis/rpc/errdetails" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gapi | ||
package api | ||
|
||
import ( | ||
"context" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gapi | ||
package api | ||
|
||
import ( | ||
"context" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gapi | ||
package api | ||
|
||
import ( | ||
"context" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gapi | ||
package api | ||
|
||
import ( | ||
"context" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gapi | ||
package api | ||
|
||
import ( | ||
"context" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gapi | ||
package api | ||
|
||
import ( | ||
"context" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gapi | ||
package api | ||
|
||
import ( | ||
"context" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package gapi | ||
package api | ||
|
||
import ( | ||
"context" | ||
|
Oops, something went wrong.