Начал пытаться что-то написать на JS. Ненавижу JS

This commit is contained in:
B4D_US3R 2024-12-13 23:30:30 +05:00
parent 3760787091
commit 9dbcc53fd1
6 changed files with 39 additions and 5 deletions

3
.gitignore vendored
View file

@ -25,3 +25,6 @@ go.work
images/ images/
main main
node_modules
dist
bun.lockb

View file

@ -2,7 +2,8 @@
<html> <html>
<head> <head>
<title>CatGirls Town</title> <title>CatGirls Town</title>
<link rel="stylesheet" href="static/picnic.min.css"> <link rel="stylesheet" href="src/picnic.min.css">
<script type="module" src="src/main.js"></script>
<meta charset="utf-8"> <meta charset="utf-8">
</head> </head>
<body> <body>
@ -34,7 +35,7 @@
</div> </div>
<div class="flex "> <div class="flex ">
<article class="card"> <article class="card">
<img src="images/{{ . }}"> <img id="CatGirlPicture" onclick="getRandomPicture()" src="images/{{ . }}">
</article> </article>
</div> </div>
<div class="flex"></div> <div class="flex"></div>

14
main.go
View file

@ -7,6 +7,7 @@ import (
"math/rand" "math/rand"
"net/http" "net/http"
"os" "os"
"strings"
"text/template" "text/template"
) )
@ -30,6 +31,11 @@ func GetRandImg() string {
return picsArray[rand.Intn(len(picsArray))] return picsArray[rand.Intn(len(picsArray))]
} }
func ReturnPicFilename(w http.ResponseWriter, req *http.Request) {
var picFilename io.Reader = strings.NewReader(GetRandImg())
io.Copy(w, picFilename)
}
func ReturnPic(w http.ResponseWriter, req *http.Request) { func ReturnPic(w http.ResponseWriter, req *http.Request) {
picture, _ := os.Open(fmt.Sprintf("images/%v", GetRandImg())) picture, _ := os.Open(fmt.Sprintf("images/%v", GetRandImg()))
var pictureReader io.Reader = picture var pictureReader io.Reader = picture
@ -38,7 +44,7 @@ func ReturnPic(w http.ResponseWriter, req *http.Request) {
} }
func ReturnHTML(w http.ResponseWriter, req *http.Request) { func ReturnHTML(w http.ResponseWriter, req *http.Request) {
htmlOpen, err := template.ParseFiles("main.html") htmlOpen, err := template.ParseFiles("dist/index.html")
if err != nil { if err != nil {
log.Println(err.Error()) log.Println(err.Error())
return return
@ -51,9 +57,11 @@ func ReturnHTML(w http.ResponseWriter, req *http.Request) {
func main() { func main() {
log.Println("Server Start") log.Println("Server Start")
imagesDir := http.FileServer(http.Dir("images")) imagesDir := http.FileServer(http.Dir("images"))
staticDir := http.FileServer(http.Dir("static")) staticDir := http.FileServer(http.Dir("dist/assets"))
http.Handle("/images/", http.StripPrefix("/images/", imagesDir)) http.Handle("/images/", http.StripPrefix("/images/", imagesDir))
http.Handle("/static/", http.StripPrefix("/static/", staticDir)) http.Handle("/assets/", http.StripPrefix("/assets/", staticDir))
http.HandleFunc("/rand", ReturnPicFilename)
http.HandleFunc("/", ReturnHTML) http.HandleFunc("/", ReturnHTML)
http.ListenAndServe(":3666", nil) http.ListenAndServe(":3666", nil)
} }

15
package.json Normal file
View file

@ -0,0 +1,15 @@
{
"name": "picRan",
"private": "true",
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"vite": "^6.0.1",
"axios": "1.7.9"
}
}

7
src/main.js Normal file
View file

@ -0,0 +1,7 @@
import axios from "axios";
let pictureId = document.getElementById("CatGirlPicture")
pictureId.onclick = function() {
let picFilename = axios.get(location.href + "rand")
console.log(picFilename)
}