-
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.
Merge pull request #124 from rebuy-de/add-cdnmirror
add cdnmirror
- Loading branch information
Showing
4 changed files
with
162 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
|
||
"github.com/evanw/esbuild/pkg/api" | ||
"github.com/pkg/errors" | ||
"github.com/rebuy-de/rebuy-go-sdk/v4/pkg/cmdutil" | ||
"github.com/rebuy-de/rebuy-go-sdk/v4/pkg/webutil" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const targetPathPrefix = `assets/cdnmirror` | ||
|
||
func main() { | ||
defer cmdutil.HandleExit() | ||
if err := NewRootCommand().Execute(); err != nil { | ||
logrus.Fatal(err) | ||
} | ||
} | ||
|
||
func NewRootCommand() *cobra.Command { | ||
return cmdutil.New( | ||
"cdnmirror SOURCE_NAME..", "Downloads assets from CDNs so the server can serve them directly.", | ||
cmdutil.WithLogVerboseFlag(), | ||
cmdutil.WithRun(Generate), | ||
) | ||
} | ||
|
||
func Generate(ctx context.Context, cmd *cobra.Command, args []string) { | ||
err := os.MkdirAll(targetPathPrefix, 0755) | ||
cmdutil.Must(err) | ||
|
||
writeGitignore() | ||
|
||
for _, name := range args { | ||
source := resolve(name) | ||
download(source) | ||
} | ||
} | ||
|
||
func writeGitignore() { | ||
filename := path.Join(targetPathPrefix, ".gitignore") | ||
|
||
buf := new(bytes.Buffer) | ||
fmt.Fprintln(buf, "*") | ||
fmt.Fprintln(buf, "!.gitignore") | ||
|
||
err := ioutil.WriteFile(filename, buf.Bytes(), 0644) | ||
cmdutil.Must(err) | ||
} | ||
|
||
func resolve(name string) webutil.CDNMirrorSource { | ||
switch name { | ||
case "@hotwired/turbo": | ||
return webutil.CDNMirrorSourceHotwiredTurbo() | ||
case "bootstrap": | ||
return webutil.CDNMirrorSourceBootstrap() | ||
case "font-awesome-sprites": | ||
return webutil.CDNMirrorSourceFontAwesomeSprites() | ||
case "bulma": | ||
return webutil.CDNMirrorSourceBulma() | ||
default: | ||
cmdutil.Must(errors.Errorf("invalid source name")) | ||
return webutil.CDNMirrorSource{} | ||
} | ||
} | ||
|
||
func download(source webutil.CDNMirrorSource) { | ||
targetFile := filepath.FromSlash(path.Join(targetPathPrefix, source.Target)) | ||
|
||
resp, err := http.Get(source.URL) | ||
cmdutil.Must(err) | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
cmdutil.Must(fmt.Errorf(resp.Status)) | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
cmdutil.Must(err) | ||
|
||
var code string | ||
|
||
switch source.Minify { | ||
case webutil.CDNMirrorMinifyJS: | ||
result := api.Transform(string(body), api.TransformOptions{ | ||
MinifyWhitespace: true, | ||
MinifyIdentifiers: true, | ||
MinifySyntax: true, | ||
}) | ||
if len(result.Errors) != 0 { | ||
cmdutil.Must(errors.Errorf("%#v", result.Errors)) | ||
} | ||
code = string(result.Code) | ||
|
||
default: | ||
code = string(body) | ||
} | ||
|
||
f, err := os.Create(targetFile) | ||
cmdutil.Must(err) | ||
defer f.Close() | ||
|
||
_, err = io.WriteString(f, code) | ||
cmdutil.Must(err) | ||
} |
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
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
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,43 @@ | ||
package webutil | ||
|
||
type CDNMirrorSource struct { | ||
URL string | ||
Target string | ||
Minify CDNMirrorMinifier | ||
} | ||
|
||
type CDNMirrorMinifier string | ||
|
||
const ( | ||
CDNMirrorMinifyJS = "js" | ||
CDNMirrorMinifyCSS = "css" | ||
) | ||
|
||
func CDNMirrorSourceHotwiredTurbo() CDNMirrorSource { | ||
return CDNMirrorSource{ | ||
URL: "https://unpkg.com/@hotwired/[email protected]/dist/turbo.es2017-umd.js", | ||
Target: "hotwired-turbo-7.1.0-min.js", | ||
Minify: CDNMirrorMinifyJS, | ||
} | ||
} | ||
|
||
func CDNMirrorSourceBootstrap() CDNMirrorSource { | ||
return CDNMirrorSource{ | ||
URL: "https://unpkg.com/[email protected]/dist/css/bootstrap.min.css", | ||
Target: "bootstrap-5.1.3-min.css", | ||
} | ||
} | ||
|
||
func CDNMirrorSourceFontAwesomeSprites() CDNMirrorSource { | ||
return CDNMirrorSource{ | ||
URL: "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/sprites/solid.svg", | ||
Target: "font-awesome-6.1.2-sprites-solid.svg", | ||
} | ||
} | ||
|
||
func CDNMirrorSourceBulma() CDNMirrorSource { | ||
return CDNMirrorSource{ | ||
URL: "https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css", | ||
Target: "bulma-0.7.4.min.css", | ||
} | ||
} |