-
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.
- Loading branch information
kubegu
committed
Mar 1, 2024
1 parent
0d25d6f
commit 0c801ad
Showing
4 changed files
with
56 additions
and
35 deletions.
There are no files selected for viewing
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,46 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"embed" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/gunni1/leipzig-library-game-stock-api/domain" | ||
libClient "github.com/gunni1/leipzig-library-game-stock-api/library-le" | ||
"github.com/gunni1/leipzig-library-game-stock-api/web" | ||
) | ||
|
||
//go:embed templates | ||
var htmlTemplates embed.FS | ||
|
||
func indexHandler(respWriter http.ResponseWriter, request *http.Request) { | ||
templ := template.Must(template.ParseFS(htmlTemplates, "templates/index.html")) | ||
templ.Execute(respWriter, nil) | ||
} | ||
|
||
func gameIndexHandler(respWriter http.ResponseWriter, request *http.Request) { | ||
log.Print("received htmx game-index") | ||
branch := strings.ToLower(request.PostFormValue("branch")) | ||
platform := strings.ToLower(request.PostFormValue("platform")) | ||
branchCode, exists := libClient.GetBranchCode(branch) | ||
if !exists { | ||
log.Printf("Requested branch: %s does not exists.", branch) | ||
return | ||
} | ||
client := libClient.Client{} | ||
games := client.FindAvailabelGames(branchCode, platform) | ||
data := map[string][]domain.Game{ | ||
"Games": games, | ||
} | ||
templ := template.Must(template.ParseFS(htmlTemplates, "templates/games.html")) | ||
templ.Execute(respWriter, data) | ||
} | ||
|
||
func main() { | ||
mux := http.NewServeMux() | ||
port := flag.Int("port", 8080, "Webserver Port") | ||
flag.Parse() | ||
|
||
mux.HandleFunc("/", indexHandler) | ||
mux.HandleFunc("/game-index/", gameIndexHandler) | ||
log.Fatal(http.ListenAndServe(":8080", mux)) | ||
mux := web.InitMux() | ||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), mux)) | ||
} |
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,49 @@ | ||
package web | ||
|
||
import ( | ||
"embed" | ||
"log" | ||
"net/http" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/gunni1/leipzig-library-game-stock-api/domain" | ||
libClient "github.com/gunni1/leipzig-library-game-stock-api/library-le" | ||
) | ||
|
||
//go:embed templates | ||
var htmlTemplates embed.FS | ||
|
||
type Server struct { | ||
Mux *http.ServeMux | ||
} | ||
|
||
func InitMux() *http.ServeMux { | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", indexHandler) | ||
mux.HandleFunc("/game-index/", gameIndexHandler) | ||
return mux | ||
} | ||
|
||
func indexHandler(respWriter http.ResponseWriter, request *http.Request) { | ||
templ := template.Must(template.ParseFS(htmlTemplates, "templates/index.html")) | ||
templ.Execute(respWriter, nil) | ||
} | ||
|
||
func gameIndexHandler(respWriter http.ResponseWriter, request *http.Request) { | ||
log.Print("received htmx game-index") | ||
branch := strings.ToLower(request.PostFormValue("branch")) | ||
platform := strings.ToLower(request.PostFormValue("platform")) | ||
branchCode, exists := libClient.GetBranchCode(branch) | ||
if !exists { | ||
log.Printf("Requested branch: %s does not exists.", branch) | ||
return | ||
} | ||
client := libClient.Client{} | ||
games := client.FindAvailabelGames(branchCode, platform) | ||
data := map[string][]domain.Game{ | ||
"Games": games, | ||
} | ||
templ := template.Must(template.ParseFS(htmlTemplates, "templates/games.html")) | ||
templ.Execute(respWriter, data) | ||
} |
File renamed without changes.
File renamed without changes.