Add index page to the development server (#146)

* Add index page to dev server

* Add target=_blank to packwiz-installer link

* Improve sentence about packwiz-installer

Co-authored-by: comp500 <comp500@users.noreply.github.com>
This commit is contained in:
Maxim Slipenko 2022-08-19 06:17:28 +03:00 committed by GitHub
parent 3f0ebebede
commit 4c68c7240f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 1 deletions

View File

@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>packwiz development server</title>
<style>
h1, p {
text-align: center;
margin: 1.5em;
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
hr {
width: 100px;
height: 1px;
border: none;
background-color: #ccc;
color: #ccc;
}
code {
background-color: #f5f5f5;
border-radius: 2px;
padding: 1px 5px;
font-size: 0.85rem;
}
</style>
</head>
<body>
<h1>
It works! Packwiz development server is running!
</h1>
<p>
Use <a href="https://packwiz.infra.link/tutorials/installing/packwiz-installer/" target="_blank">packwiz-installer</a> to install the pack from this HTTP server - works best with MultiMC/PolyMC/ATLauncher, or standalone for servers.
</p>
<p>
Your <code>pack.toml</code> is hosted at <code>http://localhost:{{.Port}}/pack.toml</code>.
</p>
<hr>
<p>
<a href="https://packwiz.infra.link" target="_blank">packwiz</a>
</p>
</body>
</html>

View File

@ -1,7 +1,10 @@
package cmd package cmd
import ( import (
"bytes"
_ "embed"
"fmt" "fmt"
"html/template"
"io" "io"
"net/http" "net/http"
"os" "os"
@ -18,6 +21,9 @@ import (
var refreshMutex sync.RWMutex var refreshMutex sync.RWMutex
//go:embed serve-templates/index.html
var indexPage string
// serveCmd represents the serve command // serveCmd represents the serve command
var serveCmd = &cobra.Command{ var serveCmd = &cobra.Command{
Use: "serve", Use: "serve",
@ -26,6 +32,8 @@ var serveCmd = &cobra.Command{
Aliases: []string{"server"}, Aliases: []string{"server"},
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
port := strconv.Itoa(viper.GetInt("serve.port"))
if viper.GetBool("serve.basic") { if viper.GetBool("serve.basic") {
http.Handle("/", http.FileServer(http.Dir("."))) http.Handle("/", http.FileServer(http.Dir(".")))
} else { } else {
@ -43,7 +51,21 @@ var serveCmd = &cobra.Command{
indexPath := filepath.Join(filepath.Dir(viper.GetString("pack-file")), filepath.FromSlash(pack.Index.File)) indexPath := filepath.Join(filepath.Dir(viper.GetString("pack-file")), filepath.FromSlash(pack.Index.File))
indexDir := filepath.Dir(indexPath) indexDir := filepath.Dir(indexPath)
t, err := template.New("index-page").Parse(indexPage)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
indexPageBuf := new(bytes.Buffer)
t.Execute(indexPageBuf, struct{ Port string }{Port: port})
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" {
_, _ = w.Write(indexPageBuf.Bytes())
return
}
urlPath := strings.TrimPrefix(path.Clean("/"+strings.TrimPrefix(req.URL.Path, "/")), "/") urlPath := strings.TrimPrefix(path.Clean("/"+strings.TrimPrefix(req.URL.Path, "/")), "/")
indexRelPath, err := filepath.Rel(indexDir, filepath.FromSlash(urlPath)) indexRelPath, err := filepath.Rel(indexDir, filepath.FromSlash(urlPath))
if err != nil { if err != nil {
@ -144,7 +166,6 @@ var serveCmd = &cobra.Command{
}) })
} }
port := strconv.Itoa(viper.GetInt("serve.port"))
fmt.Println("Running on port " + port) fmt.Println("Running on port " + port)
err := http.ListenAndServe(":"+port, nil) err := http.ListenAndServe(":"+port, nil)
if err != nil { if err != nil {