opensearch.xml generator
This commit is contained in:
parent
fe96287442
commit
cc824b5820
13 changed files with 198 additions and 14 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
config.json
|
||||
opensearch.xml
|
116
init.go
Normal file
116
init.go
Normal file
|
@ -0,0 +1,116 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Configuration structure
|
||||
type Config struct {
|
||||
Port int
|
||||
OpenSearch OpenSearchConfig
|
||||
}
|
||||
|
||||
type OpenSearchConfig struct {
|
||||
Domain string
|
||||
}
|
||||
|
||||
// Default configuration values
|
||||
var defaultConfig = Config{
|
||||
Port: 5000,
|
||||
OpenSearch: OpenSearchConfig{
|
||||
Domain: "localhost",
|
||||
},
|
||||
}
|
||||
|
||||
const configFilePath = "config.json"
|
||||
|
||||
func main() {
|
||||
// Run the initialization process
|
||||
err := initConfig()
|
||||
if err != nil {
|
||||
fmt.Println("Error during initialization:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Start the main application
|
||||
runServer()
|
||||
}
|
||||
|
||||
func initConfig() error {
|
||||
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
|
||||
return createConfig()
|
||||
}
|
||||
|
||||
fmt.Println("Configuration file already exists.")
|
||||
return nil
|
||||
}
|
||||
|
||||
func createConfig() error {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Println("Configuration file not found.")
|
||||
fmt.Print("Do you want to use default values? (yes/no): ")
|
||||
useDefaults, _ := reader.ReadString('\n')
|
||||
|
||||
config := defaultConfig
|
||||
if useDefaults != "yes\n" {
|
||||
fmt.Print("Enter port (default 5000): ")
|
||||
portStr, _ := reader.ReadString('\n')
|
||||
if portStr != "\n" {
|
||||
port, err := strconv.Atoi(portStr[:len(portStr)-1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
config.Port = port
|
||||
}
|
||||
|
||||
fmt.Print("Enter your domain address (e.g., domain.com): ")
|
||||
domain, _ := reader.ReadString('\n')
|
||||
if domain != "\n" {
|
||||
config.OpenSearch.Domain = domain[:len(domain)-1]
|
||||
}
|
||||
}
|
||||
|
||||
saveConfig(config)
|
||||
return nil
|
||||
}
|
||||
|
||||
func saveConfig(config Config) {
|
||||
file, err := os.Create(configFilePath)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating config file:", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
configData, err := json.MarshalIndent(config, "", " ")
|
||||
if err != nil {
|
||||
fmt.Println("Error marshalling config data:", err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = file.Write(configData)
|
||||
if err != nil {
|
||||
fmt.Println("Error writing to config file:", err)
|
||||
}
|
||||
}
|
||||
|
||||
func loadConfig() Config {
|
||||
configFile, err := os.Open(configFilePath)
|
||||
if err != nil {
|
||||
log.Fatalf("Error opening config file: %v", err)
|
||||
}
|
||||
defer configFile.Close()
|
||||
|
||||
var config Config
|
||||
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
|
||||
log.Fatalf("Error decoding config file: %v", err)
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
34
main.go
34
main.go
|
@ -63,19 +63,6 @@ var languageOptions = []LanguageOption{
|
|||
{Code: "lang_vi", Name: "Tiếng Việt (Vietnamese)"},
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||
http.HandleFunc("/", handleSearch)
|
||||
http.HandleFunc("/search", handleSearch)
|
||||
http.HandleFunc("/img_proxy", handleImageProxy)
|
||||
http.HandleFunc("/settings", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "templates/settings.html")
|
||||
})
|
||||
initializeTorrentSites()
|
||||
fmt.Println("Server is listening on http://localhost:5000")
|
||||
log.Fatal(http.ListenAndServe(":5000", nil))
|
||||
}
|
||||
|
||||
func handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
query, safe, lang, searchType, page := parseSearchParams(r)
|
||||
|
||||
|
@ -133,3 +120,24 @@ func parsePageParameter(pageStr string) int {
|
|||
}
|
||||
return page
|
||||
}
|
||||
|
||||
func runServer() {
|
||||
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||
http.HandleFunc("/", handleSearch)
|
||||
http.HandleFunc("/search", handleSearch)
|
||||
http.HandleFunc("/img_proxy", handleImageProxy)
|
||||
http.HandleFunc("/settings", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "templates/settings.html")
|
||||
})
|
||||
http.HandleFunc("/opensearch.xml", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/opensearchdescription+xml")
|
||||
http.ServeFile(w, r, "static/opensearch.xml")
|
||||
})
|
||||
initializeTorrentSites()
|
||||
|
||||
config := loadConfig()
|
||||
generateOpenSearchXML(config)
|
||||
|
||||
fmt.Printf("Server is listening on http://localhost:%d\n", config.Port)
|
||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil))
|
||||
}
|
||||
|
|
50
open-search.go
Normal file
50
open-search.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type OpenSearchDescription struct {
|
||||
XMLName xml.Name `xml:"OpenSearchDescription"`
|
||||
Xmlns string `xml:"xmlns,attr"`
|
||||
ShortName string `xml:"ShortName"`
|
||||
Description string `xml:"Description"`
|
||||
Tags string `xml:"Tags"`
|
||||
URL URL `xml:"Url"`
|
||||
}
|
||||
|
||||
type URL struct {
|
||||
Type string `xml:"type,attr"`
|
||||
Template string `xml:"template,attr"`
|
||||
}
|
||||
|
||||
func generateOpenSearchXML(config Config) {
|
||||
opensearch := OpenSearchDescription{
|
||||
Xmlns: "http://a9.com/-/spec/opensearch/1.1/",
|
||||
ShortName: "Ocásek",
|
||||
Description: "Search engine",
|
||||
Tags: "search, engine",
|
||||
URL: URL{
|
||||
Type: "text/html",
|
||||
Template: fmt.Sprintf("https://%s/search?q={searchTerms}", config.OpenSearch.Domain),
|
||||
},
|
||||
}
|
||||
|
||||
file, err := os.Create("static/opensearch.xml")
|
||||
if err != nil {
|
||||
fmt.Println("Error creating OpenSearch file:", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
enc := xml.NewEncoder(file)
|
||||
enc.Indent(" ", " ")
|
||||
if err := enc.Encode(opensearch); err != nil {
|
||||
fmt.Println("Error encoding OpenSearch XML:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("OpenSearch description file generated successfully.")
|
||||
}
|
2
run.sh
2
run.sh
|
@ -1,3 +1,3 @@
|
|||
#!/bin/bash
|
||||
|
||||
go run main.go common.go images.go imageproxy.go images-quant.go images-imgur.go video.go map.go text.go text-searchxng.go text-librex.go text-google.go cache.go forums.go files.go files-torrentgalaxy.go files-thepiratebay.go agent.go
|
||||
go run main.go common.go init.go open-search.go images.go imageproxy.go images-quant.go images-imgur.go video.go map.go text.go text-searchxng.go text-librex.go text-google.go cache.go forums.go files.go files-torrentgalaxy.go files-thepiratebay.go agent.go
|
|
@ -5,6 +5,7 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{.Query}} - Ocásek</title>
|
||||
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
|
||||
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
|
||||
</head>
|
||||
<body>
|
||||
<form action="/search" id="prev-next-form" class="results-search-container" method="GET" autocomplete="off">
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{.Query}} - Ocásek</title>
|
||||
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
|
||||
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
|
||||
</head>
|
||||
<body>
|
||||
<form action="/search" id="prev-next-form" class="results-search-container" method="GET" autocomplete="off">
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{.Query}} - Ocásek</title>
|
||||
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
|
||||
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
|
||||
</head>
|
||||
<body>
|
||||
<form action="/search" id="prev-next-form" class="results-search-container" method="GET" autocomplete="off">
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{ .Query }} - Ocásek</title>
|
||||
<link rel="stylesheet" href="/static/css/style.css">
|
||||
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
|
||||
<script src="https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.js"></script>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.css" />
|
||||
<style>
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Search with Ocásek</title>
|
||||
<link rel="stylesheet" href="/static/css/style.css">
|
||||
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
|
||||
</head>
|
||||
<body>
|
||||
<div class="settings-search-div settings-search-div-search">
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Settings - Ocásek</title>
|
||||
<link rel="stylesheet" type="text/css" href="static/css/style.css">
|
||||
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
|
||||
</head>
|
||||
<body>
|
||||
<form action="/search" id="prev-next-form" class="results-search-container" method="GET" autocomplete="off">
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{.Query}} - Ocásek</title>
|
||||
<link rel="stylesheet" type="text/css" href="/static/css/style.css">
|
||||
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
|
||||
</head>
|
||||
<body>
|
||||
<form action="/search" id="prev-next-form" class="results-search-container" method="GET" autocomplete="off">
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{.Query}} - Ocásek</title>
|
||||
<link rel="stylesheet" href="/static/css/style.css">
|
||||
<link rel="search" type="application/opensearchdescription+xml" title="Ocásek" href="/opensearch.xml">
|
||||
</head>
|
||||
<body>
|
||||
<form action="/search" id="prev-next-form" class="results-search-container" method="GET" autocomplete="off">
|
||||
|
|
Loading…
Add table
Reference in a new issue