diff --git a/Lab01/app/.idea/app.iml b/Lab01/app/.idea/app.iml index 5e764c4..2be4a98 100644 --- a/Lab01/app/.idea/app.iml +++ b/Lab01/app/.idea/app.iml @@ -5,5 +5,6 @@ + \ No newline at end of file diff --git a/Lab01/app/.idea/dataSources.xml b/Lab01/app/.idea/dataSources.xml new file mode 100644 index 0000000..b9c0962 --- /dev/null +++ b/Lab01/app/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:C:\Users\FlorianHoss\Documents\GitHub\SWB6-ITSec\Lab01\app\sqlite.db + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/Lab01/app/.idea/jsLibraryMappings.xml b/Lab01/app/.idea/jsLibraryMappings.xml new file mode 100644 index 0000000..b843835 --- /dev/null +++ b/Lab01/app/.idea/jsLibraryMappings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Lab01/app/api/api.go b/Lab01/app/api/api.go deleted file mode 100644 index cf6e83c..0000000 --- a/Lab01/app/api/api.go +++ /dev/null @@ -1,26 +0,0 @@ -package api - -import ( - "app/database" - "github.com/gin-gonic/gin" -) - -func (api *Api) defineRoutes() { - api.Router.GET("/ping", func(c *gin.Context) { - c.JSON(200, gin.H{ - "message": "pong", - }) - }) -} - -func (api *Api) initialize() { - api.Database = database.Database{Location: "sqlite.db"} - api.Database.Initialize() -} - -func (api *Api) Run() { - api.initialize() - api.Router = gin.Default() - api.defineRoutes() - api.Router.Run() -} diff --git a/Lab01/app/database/types.go b/Lab01/app/database/types.go index 2aef4a9..a0078b8 100644 --- a/Lab01/app/database/types.go +++ b/Lab01/app/database/types.go @@ -8,6 +8,7 @@ type Database struct { } type User struct { + ID int Username string Password string } diff --git a/Lab01/app/go.mod b/Lab01/app/go.mod index a6591fa..89c6d96 100644 --- a/Lab01/app/go.mod +++ b/Lab01/app/go.mod @@ -3,6 +3,7 @@ module app go 1.18 require ( + github.com/gin-contrib/static v0.0.1 github.com/gin-gonic/gin v1.7.7 gorm.io/driver/sqlite v1.3.1 gorm.io/gorm v1.23.3 diff --git a/Lab01/app/go.sum b/Lab01/app/go.sum index 3260c61..3f07ea8 100644 --- a/Lab01/app/go.sum +++ b/Lab01/app/go.sum @@ -3,6 +3,9 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-contrib/static v0.0.1 h1:JVxuvHPuUfkoul12N7dtQw7KRn/pSMq7Ue1Va9Swm1U= +github.com/gin-contrib/static v0.0.1/go.mod h1:CSxeF+wep05e0kCOsqWdAWbSszmc31zTIbD8TvWl7Hs= +github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs= github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U= github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= @@ -11,6 +14,7 @@ github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8c github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I= diff --git a/Lab01/app/main.go b/Lab01/app/main.go index 98786d7..917bdea 100644 --- a/Lab01/app/main.go +++ b/Lab01/app/main.go @@ -1,8 +1,8 @@ package main -import "app/api" +import "app/webpage" func main() { - backend := api.Api{} + backend := webpage.Webpage{} backend.Run() } diff --git a/Lab01/app/static/icons/android-chrome-192x192.png b/Lab01/app/static/icons/android-chrome-192x192.png new file mode 100644 index 0000000..b36e23b Binary files /dev/null and b/Lab01/app/static/icons/android-chrome-192x192.png differ diff --git a/Lab01/app/static/icons/android-chrome-512x512.png b/Lab01/app/static/icons/android-chrome-512x512.png new file mode 100644 index 0000000..3ea3a5a Binary files /dev/null and b/Lab01/app/static/icons/android-chrome-512x512.png differ diff --git a/Lab01/app/static/icons/apple-touch-icon.png b/Lab01/app/static/icons/apple-touch-icon.png new file mode 100644 index 0000000..b0daa64 Binary files /dev/null and b/Lab01/app/static/icons/apple-touch-icon.png differ diff --git a/Lab01/app/static/icons/browserconfig.xml b/Lab01/app/static/icons/browserconfig.xml new file mode 100644 index 0000000..b3930d0 --- /dev/null +++ b/Lab01/app/static/icons/browserconfig.xml @@ -0,0 +1,9 @@ + + + + + + #da532c + + + diff --git a/Lab01/app/static/icons/favicon-16x16.png b/Lab01/app/static/icons/favicon-16x16.png new file mode 100644 index 0000000..ff14dcc Binary files /dev/null and b/Lab01/app/static/icons/favicon-16x16.png differ diff --git a/Lab01/app/static/icons/favicon-32x32.png b/Lab01/app/static/icons/favicon-32x32.png new file mode 100644 index 0000000..5557af4 Binary files /dev/null and b/Lab01/app/static/icons/favicon-32x32.png differ diff --git a/Lab01/app/static/icons/favicon.ico b/Lab01/app/static/icons/favicon.ico new file mode 100644 index 0000000..6748823 Binary files /dev/null and b/Lab01/app/static/icons/favicon.ico differ diff --git a/Lab01/app/static/icons/mstile-144x144.png b/Lab01/app/static/icons/mstile-144x144.png new file mode 100644 index 0000000..b00b0a1 Binary files /dev/null and b/Lab01/app/static/icons/mstile-144x144.png differ diff --git a/Lab01/app/static/icons/mstile-150x150.png b/Lab01/app/static/icons/mstile-150x150.png new file mode 100644 index 0000000..2b3744e Binary files /dev/null and b/Lab01/app/static/icons/mstile-150x150.png differ diff --git a/Lab01/app/static/icons/mstile-310x150.png b/Lab01/app/static/icons/mstile-310x150.png new file mode 100644 index 0000000..5ba0180 Binary files /dev/null and b/Lab01/app/static/icons/mstile-310x150.png differ diff --git a/Lab01/app/static/icons/mstile-310x310.png b/Lab01/app/static/icons/mstile-310x310.png new file mode 100644 index 0000000..9872429 Binary files /dev/null and b/Lab01/app/static/icons/mstile-310x310.png differ diff --git a/Lab01/app/static/icons/mstile-70x70.png b/Lab01/app/static/icons/mstile-70x70.png new file mode 100644 index 0000000..7342bc8 Binary files /dev/null and b/Lab01/app/static/icons/mstile-70x70.png differ diff --git a/Lab01/app/static/icons/safari-pinned-tab.svg b/Lab01/app/static/icons/safari-pinned-tab.svg new file mode 100644 index 0000000..8fa1266 --- /dev/null +++ b/Lab01/app/static/icons/safari-pinned-tab.svg @@ -0,0 +1,35 @@ + + + + +Created by potrace 1.14, written by Peter Selinger 2001-2017 + + + + + + diff --git a/Lab01/app/static/icons/site.webmanifest b/Lab01/app/static/icons/site.webmanifest new file mode 100644 index 0000000..b20abb7 --- /dev/null +++ b/Lab01/app/static/icons/site.webmanifest @@ -0,0 +1,19 @@ +{ + "name": "", + "short_name": "", + "icons": [ + { + "src": "/android-chrome-192x192.png", + "sizes": "192x192", + "type": "image/png" + }, + { + "src": "/android-chrome-512x512.png", + "sizes": "512x512", + "type": "image/png" + } + ], + "theme_color": "#ffffff", + "background_color": "#ffffff", + "display": "standalone" +} diff --git a/Lab01/app/templates/index.tmpl b/Lab01/app/templates/index.tmpl new file mode 100644 index 0000000..b1d9195 --- /dev/null +++ b/Lab01/app/templates/index.tmpl @@ -0,0 +1,49 @@ + + + + + {{ .title }} + {{template "head" .}} + + +{{template "navbar" .}} + +
+
Welcome
+
+ + + + + +{{define "head"}} + + + + + + + + +{{end}} + +{{define "navbar"}} + +{{end}} \ No newline at end of file diff --git a/Lab01/app/templates/login.tmpl b/Lab01/app/templates/login.tmpl new file mode 100644 index 0000000..bfec003 --- /dev/null +++ b/Lab01/app/templates/login.tmpl @@ -0,0 +1,29 @@ + + + + + {{ .title }} + {{template "head" .}} + + +{{template "navbar" .}} + +
+
+
+ + +
The username needs to be unique
+
+
+ + +
The password needs to be different from the username
+
+ +
+
+ + + + \ No newline at end of file diff --git a/Lab01/app/templates/register.tmpl b/Lab01/app/templates/register.tmpl new file mode 100644 index 0000000..b64084b --- /dev/null +++ b/Lab01/app/templates/register.tmpl @@ -0,0 +1,45 @@ + + + + + {{ .title }} + {{template "head" .}} + + +{{template "navbar" .}} + +
+
+
+ + +
The username needs to be unique
+
+
+ + +
The password needs to be different from the username
+
+ +
+
+ + + + + \ No newline at end of file diff --git a/Lab01/app/api/types.go b/Lab01/app/webpage/types.go similarity index 74% rename from Lab01/app/api/types.go rename to Lab01/app/webpage/types.go index e9b4aed..578fc26 100644 --- a/Lab01/app/api/types.go +++ b/Lab01/app/webpage/types.go @@ -1,11 +1,11 @@ -package api +package webpage import ( "app/database" "github.com/gin-gonic/gin" ) -type Api struct { +type Webpage struct { Database database.Database Router *gin.Engine } diff --git a/Lab01/app/webpage/webpage.go b/Lab01/app/webpage/webpage.go new file mode 100644 index 0000000..e6da9a9 --- /dev/null +++ b/Lab01/app/webpage/webpage.go @@ -0,0 +1,69 @@ +package webpage + +import ( + "app/database" + "github.com/gin-contrib/static" + "github.com/gin-gonic/gin" + "net/http" + "time" +) + +func (wp *Webpage) defineRoutes() { + + wp.Router.GET("/", func(c *gin.Context) { + c.HTML(http.StatusOK, "index.tmpl", gin.H{ + "title": "Register", + }) + }) + wp.Router.GET("/login", func(c *gin.Context) { + c.HTML(http.StatusOK, "login.tmpl", gin.H{ + "title": "Login", + }) + }) + wp.Router.GET("/register", func(c *gin.Context) { + c.HTML(http.StatusOK, "register.tmpl", gin.H{ + "title": "Register", + }) + }) + wp.Router.GET("/health", func(c *gin.Context) { + currentTime := time.Now().UnixMilli() + c.JSON(http.StatusOK, gin.H{ + "timestamp": currentTime, + }) + }) + wp.Router.NoRoute(func(c *gin.Context) { + c.Redirect(http.StatusTemporaryRedirect, "/") + }) + + wp.Router.POST("/register", func(c *gin.Context) { + username, err := c.GetPostForm("username") + password, err := c.GetPostForm("password") + if err == false { + c.JSON(400, gin.H{"message": "bad post form"}) + return + } + user := database.User{Username: username, Password: password} + result := wp.Database.ORM.Create(&user) + if result.Error != nil { + c.JSON(200, gin.H{"message": "cannot create user"}) + } + c.JSON(200, gin.H{"message": "user registered"}) + }) +} + +func (wp *Webpage) initialize() { + wp.Database = database.Database{Location: "sqlite.db"} + wp.Database.Initialize() +} + +func (wp *Webpage) Run() { + wp.initialize() + wp.Router = gin.New() + wp.Router.Use(gin.Recovery()) + wp.Router.Use(gin.Logger()) + wp.Router.SetTrustedProxies(nil) + wp.Router.Use(static.Serve("/static", static.LocalFile("./static", false))) + wp.Router.LoadHTMLGlob("templates/*") + wp.defineRoutes() + wp.Router.Run() +}