Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds static content serving #104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/web/restserver/fiber_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,10 @@ func (f *fiberWebServer) registerCustomMiddleware(m CustomMiddleware) {

f.srv.Use(fn)
}

func (f *fiberWebServer) injectStaticRoutes() {
for _, staticRoute := range staticContentRoutes {
logging.Info("Registering static route on %s serving from folder %s", staticRoute.URI, staticRoute.Path)
f.srv.Static(staticRoute.URI, staticRoute.Path)
}
}
5 changes: 5 additions & 0 deletions pkg/web/restserver/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ type Route struct {
BeforeEnter func(ctx WebContext) *MiddlewareError
}

type StaticRoute struct {
URI string
Path string
}

type healtCheck struct {
Status string `json:"status"`
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/web/restserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var (
srv Server
customAuth CustomAuthenticationMiddleware

staticContentRoutes []StaticRoute

errUserUnauthenticated = errors.New("user not authenticated")
)

Expand All @@ -26,13 +28,18 @@ type Server interface {
injectCustomMiddlewares()
injectRoutes()
listenAndServe() error
injectStaticRoutes()
}

// AddRoutes add list of routes in the webrest server
func AddRoutes(routes []Route) {
srvRoutes = append(srvRoutes, routes...)
}

func AddStaticRoute(route StaticRoute) {
staticContentRoutes = append(staticContentRoutes, route)
}

func CustomAuthMiddleware(fn CustomAuthenticationMiddleware) {
customAuth = fn
}
Expand All @@ -51,6 +58,7 @@ func ListenAndServe() {
srv.injectMiddlewares()
srv.injectCustomMiddlewares()
srv.injectRoutes()
srv.injectStaticRoutes()

observer.Attach(restObserver{})
logging.Info("Service '%s' running in %d port", "WEB-REST", config.PORT)
Expand Down
Loading