Skip to content

Commit

Permalink
feat(web-service): add webserver to main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorien Grönwald committed Oct 31, 2023
1 parent 1acbe4c commit 5824036
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 118 deletions.
129 changes: 21 additions & 108 deletions src/web-service/frontend/package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/web-service/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/kit": "^1.20.4",
"svelte": "^4.0.5",
"svelte-check": "^3.4.3",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^4.4.2"
},
"type": "module"
"type": "module",
"dependencies": {
"@sveltejs/adapter-static": "^2.0.3"
}
}
8 changes: 8 additions & 0 deletions src/web-service/frontend/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script lang="ts">
import { base } from '$app/paths';
</script>

<nav>
<a href="{base}/">Home</a>
</nav>
<slot />
2 changes: 2 additions & 0 deletions src/web-service/frontend/src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// this is needed to give us force prerendering of all pages
export const prerender = true
5 changes: 5 additions & 0 deletions src/web-service/frontend/src/routes/index.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script lang="ts">
</script>

<h1>Combining SvelteKit and Golang server</h1>

2 changes: 1 addition & 1 deletion src/web-service/frontend/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import adapter from '@sveltejs/adapter-auto';
import adapter from '@sveltejs/adapter-static';
import { vitePreprocess } from '@sveltejs/kit/vite';

const config = {
Expand Down
2 changes: 2 additions & 0 deletions src/web-service/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module hsfl.de/group6/hsfl-master-ai-cloud-engineering/web-service

go 1.21

require github.com/munxar/goapi v0.0.0-20230125125843-fe3f706866d0 // indirect
23 changes: 16 additions & 7 deletions src/web-service/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package main

import (
"hsfl.de/group6/hsfl-master-ai-cloud-engineering/web-service/api/router"
"hsfl.de/group6/hsfl-master-ai-cloud-engineering/web-service/service"
"log"
"net/http"
"path/filepath"
)

func main() {
productsController := service.NewDefaultController()
handler := router.New(productsController)
buildDir := "frontend/build"

if err := http.ListenAndServe(":3000", handler); err != nil {
log.Fatalf("error while listen and serve: %s", err.Error())
fileServer := http.FileServer(http.Dir(buildDir))

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.ServeFile(w, r, filepath.Join(buildDir, "index.html"))
return
}

fileServer.ServeHTTP(w, r)
})

err := http.ListenAndServe(":3000", nil)
if err != nil {
panic(err)
}
}

0 comments on commit 5824036

Please sign in to comment.