Json config
This commit is contained in:
parent
9f4eef0515
commit
91bcbe422c
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
/.idea/
|
||||
*.json
|
||||
57
main.go
57
main.go
@ -1,22 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const DOMAIN = "git.landgrafhomyak.ru"
|
||||
type config struct {
|
||||
Listen string `json:"listen"`
|
||||
Domain string `json:"domain"`
|
||||
Organizations []string `json:"organizations"`
|
||||
}
|
||||
|
||||
var ORGS_LIST = []string{"Multitasking", "LanguageUtilities", "xomrk"}
|
||||
|
||||
func searcher(w http.ResponseWriter, r *http.Request) {
|
||||
for _, org := range ORGS_LIST {
|
||||
func searcher(config *config, w http.ResponseWriter, r *http.Request) {
|
||||
for _, org := range config.Organizations {
|
||||
actualUrl := fmt.Sprintf(
|
||||
"https://%s/api/packages/%s/maven/%s",
|
||||
DOMAIN, org, strings.TrimLeft(r.URL.Path, "/"),
|
||||
config.Domain, org, strings.TrimLeft(r.URL.Path, "/"),
|
||||
)
|
||||
resp, err := http.Head(actualUrl)
|
||||
if err != nil {
|
||||
@ -59,7 +63,42 @@ func searcher(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(404)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", searcher)
|
||||
log.Fatal(http.ListenAndServe("localhost:49186", nil))
|
||||
func readConfig(path string) config {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var data config
|
||||
if err := json.NewDecoder(file).Decode(&data); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if err := file.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("Missed path to config")
|
||||
return
|
||||
}
|
||||
if len(os.Args) > 2 {
|
||||
fmt.Println("Excess arguments")
|
||||
return
|
||||
}
|
||||
|
||||
config := readConfig(os.Args[1])
|
||||
if config.Organizations == nil {
|
||||
fmt.Println("Missed organizations list")
|
||||
return
|
||||
}
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
searcher(&config, w, r)
|
||||
})
|
||||
log.Fatal(http.ListenAndServe(config.Listen, nil))
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user