serve frontend from go
This commit is contained in:
1 parent
a06444c4df
commit
73a62b63ae
88 files changed
+76
-36
No files matched your search
@@ -1 +1,2 @@
|
|||||||
|
blog.db
|
||||||
**/.DS_Store
|
**/.DS_Store
|
||||||
Vendored
+1
-1
@@ -10,7 +10,7 @@
|
|||||||
"type": "go",
|
"type": "go",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mode": "auto",
|
"mode": "auto",
|
||||||
"program": "${workspaceFolder}/backend/cmd/",
|
"program": "${workspaceFolder}/cmd/",
|
||||||
"args": ["serve"]
|
"args": ["serve"]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
File renamed without changes.
@@ -1 +0,0 @@
|
|||||||
blog.db
|
|
||||||
Binary file not shown.
@@ -6,8 +6,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/config"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/config"
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/initialize"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/initialize"
|
||||||
|
|
||||||
"go-simpler.org/env"
|
"go-simpler.org/env"
|
||||||
)
|
)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
module git.schreifuchs.ch/schreifuchs/ng-blog/backend
|
module git.schreifuchs.ch/schreifuchs/ng-blog
|
||||||
|
|
||||||
go 1.24.2
|
go 1.24.2
|
||||||
|
|
||||||
File renamed without changes.
@@ -10,7 +10,7 @@ import (
|
|||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/model"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Signup handles user signup by decoding request body, hashing the password, and saving user data to the database.
|
// Signup handles user signup by decoding request body, hashing the password, and saving user data to the database.
|
||||||
File renamed without changes.
@@ -7,7 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/model"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/model"
|
||||||
jwt "github.com/golang-jwt/jwt/v5"
|
jwt "github.com/golang-jwt/jwt/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/model"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/model"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/model"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Authenticated: This function is a middleware that authenticates incoming HTTP requests using JWT tokens and role-based access control.
|
// Authenticated: This function is a middleware that authenticates incoming HTTP requests using JWT tokens and role-based access control.
|
||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/model"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/model"
|
||||||
jwt "github.com/golang-jwt/jwt/v5"
|
jwt "github.com/golang-jwt/jwt/v5"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -3,7 +3,7 @@ package config
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/auth"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/auth"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config holds configuration settings for the application, loaded from environment variables.
|
// Config holds configuration settings for the application, loaded from environment variables.
|
||||||
@@ -3,23 +3,39 @@ package initialize
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/auth"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/auth"
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/config"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/config"
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/model"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/model"
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/posts"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/posts"
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/pkg/cors"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/pkg/cors"
|
||||||
|
"git.schreifuchs.ch/schreifuchs/ng-blog/pkg/middlewares"
|
||||||
|
"git.schreifuchs.ch/schreifuchs/ng-blog/web"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateMux creates and configures a mux router with authentication and post-related routes.
|
// CreateMux creates and configures a mux router with authentication and post-related routes.
|
||||||
func CreateMux(cfg *config.Config) (r *mux.Router) {
|
func CreateMux(cfg *config.Config) (r *mux.Router) {
|
||||||
|
r = mux.NewRouter()
|
||||||
|
r.Use(cors.HandlerForOrigin("*"))
|
||||||
|
|
||||||
|
app(r.PathPrefix("/api").Subrouter(), cfg)
|
||||||
|
|
||||||
|
frontend := web.Frontend
|
||||||
|
r.PathPrefix("/").Handler(middlewares.AddPrefix("/dist/frontend/browser", http.FileServerFS(frontend)))
|
||||||
|
|
||||||
|
r.Methods("OPTIONS").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// The CORS middleware should set up the headers for you
|
||||||
|
w.WriteHeader(http.StatusNoContent)
|
||||||
|
})
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func app(r *mux.Router, cfg *config.Config) {
|
||||||
db := model.Init()
|
db := model.Init()
|
||||||
blg := posts.New(db)
|
blg := posts.New(db)
|
||||||
auth := auth.New(&cfg.Auth, db)
|
auth := auth.New(&cfg.Auth, db)
|
||||||
|
|
||||||
r = mux.NewRouter()
|
|
||||||
r.Use(cors.HandlerForOrigin("*"))
|
|
||||||
|
|
||||||
// auth
|
// auth
|
||||||
r.HandleFunc("/login", auth.Login).Methods("POST")
|
r.HandleFunc("/login", auth.Login).Methods("POST")
|
||||||
r.HandleFunc("/signup", auth.Signup).Methods("POST")
|
r.HandleFunc("/signup", auth.Signup).Methods("POST")
|
||||||
@@ -30,11 +46,4 @@ func CreateMux(cfg *config.Config) (r *mux.Router) {
|
|||||||
r.Handle("/posts", auth.Authenticated(blg.SavePost, model.RoleUser, model.RoleAdmin)).Methods("PUT")
|
r.Handle("/posts", auth.Authenticated(blg.SavePost, model.RoleUser, model.RoleAdmin)).Methods("PUT")
|
||||||
r.Handle("/posts/{postID}", auth.Authenticated(blg.DeletePost, model.RoleUser, model.RoleAdmin)).Methods("DELETE")
|
r.Handle("/posts/{postID}", auth.Authenticated(blg.DeletePost, model.RoleUser, model.RoleAdmin)).Methods("DELETE")
|
||||||
r.Handle("/posts", http.HandlerFunc(blg.GetAllPosts)).Methods("GET")
|
r.Handle("/posts", http.HandlerFunc(blg.GetAllPosts)).Methods("GET")
|
||||||
|
|
||||||
r.Methods("OPTIONS").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// The CORS middleware should set up the headers for you
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
|
||||||
})
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -7,8 +7,8 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/auth"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/auth"
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/model"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/model"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
File renamed without changes.
@@ -6,8 +6,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/auth"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/auth"
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/model"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/model"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
File renamed without changes.
@@ -7,8 +7,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/auth"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/auth"
|
||||||
"git.schreifuchs.ch/schreifuchs/ng-blog/backend/internal/model"
|
"git.schreifuchs.ch/schreifuchs/ng-blog/internal/model"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
File renamed without changes.
@@ -0,0 +1,25 @@
|
|||||||
|
package middlewares
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
func AddPrefix(prefix string, h http.Handler) http.Handler {
|
||||||
|
if prefix == "" {
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
p := prefix + r.URL.Path
|
||||||
|
rp := prefix + r.URL.RawPath
|
||||||
|
|
||||||
|
r2 := new(http.Request)
|
||||||
|
*r2 = *r
|
||||||
|
r2.URL = new(url.URL)
|
||||||
|
*r2.URL = *r.URL
|
||||||
|
r2.URL.Path = p
|
||||||
|
r2.URL.RawPath = rp
|
||||||
|
h.ServeHTTP(w, r2)
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,6 @@
|
|||||||
|
package web
|
||||||
|
|
||||||
|
import "embed"
|
||||||
|
|
||||||
|
//go:embed all:dist/frontend/browser
|
||||||
|
var Frontend embed.FS
|
||||||
+2
-2
@@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "frontend",
|
"name": "web",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "frontend",
|
"name": "web",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@angular/common": "^19.2.0",
|
"@angular/common": "^19.2.0",
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "frontend",
|
"name": "web",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"ng": "ng",
|
"ng": "ng",
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,4 +1,4 @@
|
|||||||
export const environment = {
|
export const environment = {
|
||||||
production: false,
|
production: false,
|
||||||
apiRoot: 'http://localhost:8080',
|
apiRoot: 'http://localhost:8080/api',
|
||||||
};
|
};
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Reference in new issue
Block a user