mirror of
https://github.com/OpenFactorioServerManager/factorio-server-manager.git
synced 2025-02-15 14:03:39 +02:00
added handler for accessing mod-portal-api
This commit is contained in:
parent
a97a24dbcd
commit
ff0011a4d5
@ -50,7 +50,7 @@ func ListInstalledMods(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
// Login into the Factorio Mod Portal System
|
||||
// Returns JSON response with the userKey or the error-message
|
||||
func LoginFactorioModPortal(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
resp := JSONResponse{
|
||||
@ -82,6 +82,39 @@ func LoginFactorioModPortal(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
//Returns JSON response with the found mods
|
||||
func ModPortalSearchHandler(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
resp := JSONResponse{
|
||||
Success: false,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
|
||||
|
||||
//Get Data out of the request
|
||||
search_keyword := r.FormValue("search")
|
||||
log.Printf("search_keyword: %s", search_keyword)
|
||||
|
||||
var statusCode int
|
||||
resp.Data, err, statusCode = searchModPortal(search_keyword)
|
||||
|
||||
w.WriteHeader(statusCode)
|
||||
|
||||
if err != nil {
|
||||
resp.Data = fmt.Sprintf("Error in searchModPortal: %s", err)
|
||||
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
||||
log.Printf("Error in searchModPortal: %s", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
resp.Success = true
|
||||
|
||||
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
||||
log.Printf("Error in ModPortalSearch: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Toggles mod passed in through mod variable
|
||||
// Updates mod-list.json file to toggle the enabled status of mods
|
||||
/*func ToggleMod(w http.ResponseWriter, r *http.Request) {
|
||||
|
30
src/mods.go
30
src/mods.go
@ -54,6 +54,7 @@ func getUserToken(username string, password string) (string, error, int) {
|
||||
return "error", get_err, 500
|
||||
}
|
||||
|
||||
//get the response-text
|
||||
text, err_io := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
@ -66,3 +67,32 @@ func getUserToken(username string, password string) (string, error, int) {
|
||||
|
||||
return text_string, nil, resp.StatusCode
|
||||
}
|
||||
|
||||
|
||||
//Search inside the factorio mod portal
|
||||
func searchModPortal(keyword string) (string, error, int) {
|
||||
//resp, get_err := http.Get
|
||||
req, err := http.NewRequest(http.MethodGet, "https://mods.factorio.com/api/mods", nil)
|
||||
if err != nil {
|
||||
return "error", err, 500
|
||||
}
|
||||
|
||||
query := req.URL.Query()
|
||||
query.Add("q", keyword)
|
||||
req.URL.RawQuery = query.Encode()
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return "error", err, 500
|
||||
}
|
||||
|
||||
text, err := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if err != nil {
|
||||
return "error", err, 500
|
||||
}
|
||||
|
||||
text_string := string(text)
|
||||
|
||||
return text_string, nil, resp.StatusCode
|
||||
}
|
||||
|
@ -158,6 +158,11 @@ var apiRoutes = Routes{
|
||||
"POST",
|
||||
"/mods/factorio/login",
|
||||
LoginFactorioModPortal,
|
||||
}, {
|
||||
"SearchModPortal",
|
||||
"GET",
|
||||
"/mods/search",
|
||||
ModPortalSearchHandler,
|
||||
}, /*{
|
||||
"ListMods",
|
||||
"GET",
|
||||
|
@ -10,14 +10,27 @@ class ModOverview extends React.Component {
|
||||
|
||||
this.state = {
|
||||
username: "",
|
||||
userKey: ""
|
||||
userKey: "",
|
||||
shownModList: []
|
||||
}
|
||||
}
|
||||
|
||||
handlerSearchMod(e) {
|
||||
console.log($(e.target).find("input").val());
|
||||
e.preventDefault();
|
||||
//TODO
|
||||
|
||||
$.ajax({
|
||||
url: "/api/mods/search",
|
||||
method: "GET",
|
||||
data: $(e.target).serialize(),
|
||||
dataType: "JSON",
|
||||
success: (data) => {
|
||||
console.log(data);
|
||||
},
|
||||
error: (jqXHR) => {
|
||||
console.log(jqXHR.statusText);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
handlerFactorioLogin(e) {
|
||||
@ -27,11 +40,8 @@ class ModOverview extends React.Component {
|
||||
let username = $form.find('input[name=username]').val();
|
||||
|
||||
$.ajax({
|
||||
// url: "https://auth.factorio.com/api-login",
|
||||
// url: "https://mods.factorio.com/api/mods",
|
||||
url: "/api/mods/factorio/login",
|
||||
method: "POST",
|
||||
crossDomain: true,
|
||||
data: $form.serialize(),
|
||||
dataType: "JSON",
|
||||
success: (data) => {
|
||||
|
@ -6,7 +6,7 @@ class ModSearch extends React.Component {
|
||||
return (
|
||||
<form onSubmit={this.props.submitSearchMod}>
|
||||
<div className="input-group col-lg-5">
|
||||
<input type="text" className="form-control" placeholder="Search for Mod"/>
|
||||
<input type="text" className="form-control" placeholder="Search for Mod" name="search" />
|
||||
<span className="input-group-btn">
|
||||
<input className="btn btn-default" type="submit" value="Go!"/>
|
||||
</span>
|
||||
|
Loading…
x
Reference in New Issue
Block a user