rewrite of request and page render system

This commit is contained in:
Michael 2019-06-13 18:11:42 +01:00
parent a42d4fd0d9
commit 889a4e7a8b
3 changed files with 154 additions and 106 deletions

30
api.go
View File

@ -1,21 +1,22 @@
package request
import (
//"fmt"
"encoding/json"
"io/ioutil"
"log"
"net/http"
//"context"
"io/ioutil"
"encoding/json"
"time"
"strings"
"time"
"github.com/fatih/structs"
)
func url(path string) string {
return string("https://forum.0cd.xyz/" + path)
}
func Request(path string) (b []byte) {
// Request sends GET to path
func Request(path string) []byte {
req, err := http.NewRequest("GET", url(path), nil)
if err != nil {
log.Fatal("Error reading request. ", err)
@ -43,26 +44,33 @@ func Request(path string) (b []byte) {
//fmt.Printf("%s %s %d\n", resp.Request.Method, url("about.json"), resp.StatusCode)
if resp.StatusCode == 200 {
/*var result map[string]interface{}
json.Unmarshal([]byte(body), &result)
return result*/
return body
}
return nil
}
func About() (*AutoGen) {
// About gets json data from about page
func About() map[string]interface{} {
var about AutoGen
json.Unmarshal(Request("about"), &about)
for i := 0; i < len(about.About.Admins); i++ {
about.About.Admins[i].AvatarTemplate = strings.ReplaceAll(about.About.Admins[i].AvatarTemplate, "{size}", "120")
}
return &about
//return about
m := structs.Map(about)
return m
}
func GetTopics(path string) (*TagTopics) {
/*// GetTopics gets topic list from tag
func GetTopics(path string) *TagTopics {
var topics TagTopics
json.Unmarshal(Request(path), &topics)
return &topics
}
}*/
// Topics n/a
func Topics(path string) {
}

12
data.go
View File

@ -4,6 +4,16 @@ import (
"time"
)
// Contacts list
type Contacts struct {
Contacts []struct {
Name string `json:"name"`
Address string `json:"address"`
Icon string `json:"icon"`
} `json:"contacts"`
}
// AutoGen about page data
type AutoGen struct {
About struct {
Stats struct {
@ -46,6 +56,7 @@ type AutoGen struct {
} `json:"about"`
}
// TagTopics list of topics via tag
type TagTopics struct {
TopicList struct {
Topics []struct {
@ -54,6 +65,7 @@ type TagTopics struct {
} `json:"topic_list"`
}
// Topic data
type Topic struct {
PostStream struct {
Posts []struct {

28
file.go Normal file
View File

@ -0,0 +1,28 @@
package request
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"github.com/fatih/structs"
)
// File reads request json file
func File(path string) (b []byte) {
jsonFile, err := os.Open(path)
if err != nil {
log.Fatal("Error reading request. ", err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
return byteValue
}
func Contact() map[string]interface{} {
var contact Contacts
json.Unmarshal(File("./json/contacts.json"), &contact)
m := structs.Map(contact)
return m
}