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

94
api.go
View File

@ -1,68 +1,76 @@
package request
import (
//"fmt"
"log"
"net/http"
//"context"
"io/ioutil"
"encoding/json"
"time"
"strings"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/fatih/structs"
)
func url(path string) string {
return string("https://forum.0cd.xyz/" + path)
return string("https://forum.0cd.xyz/" + path)
}
func Request(path string) (b []byte) {
req, err := http.NewRequest("GET", url(path), nil)
// 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)
}
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Api-Key", "57dc92f574f7f400e1d12670940c19930a1f85b78485ca25ac96d96590dc7f99")
req.Header.Set("Api-Username", "Michael")
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Api-Key", "57dc92f574f7f400e1d12670940c19930a1f85b78485ca25ac96d96590dc7f99")
req.Header.Set("Api-Username", "Michael")
client := &http.Client{Timeout: time.Second * 10}
client := &http.Client{Timeout: time.Second * 10}
resp, err := client.Do(req)
if err != nil {
resp, err := client.Do(req)
if err != nil {
log.Fatal("Error reading request. ", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("Error reading request. ", err)
}
defer resp.Body.Close()
}
defer resp.Body.Close()
//fmt.Println("response Status:", resp.Status)
//fmt.Println("response Headers:", resp.Header)
//fmt.Printf("%s %s %d\n", resp.Request.Method, url("about.json"), resp.StatusCode)
//fmt.Printf("%s %s %d\n", resp.Request.Method, url("about.json"), resp.StatusCode)
if resp.StatusCode == 200 {
return body
}
return nil
if resp.StatusCode == 200 {
/*var result map[string]interface{}
json.Unmarshal([]byte(body), &result)
return result*/
return body
}
return nil
}
func About() (*AutoGen) {
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
// 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
m := structs.Map(about)
return m
}
func GetTopics(path string) (*TagTopics) {
var topics TagTopics
json.Unmarshal(Request(path), &topics)
return &topics
}
/*// 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) {
}
}

138
data.go
View File

@ -4,74 +4,86 @@ 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 {
TopicCount int `json:"topic_count"`
PostCount int `json:"post_count"`
UserCount int `json:"user_count"`
Topics7Days int `json:"topics_7_days"`
Topics30Days int `json:"topics_30_days"`
Posts7Days int `json:"posts_7_days"`
Posts30Days int `json:"posts_30_days"`
Users7Days int `json:"users_7_days"`
Users30Days int `json:"users_30_days"`
ActiveUsers7Days int `json:"active_users_7_days"`
ActiveUsers30Days int `json:"active_users_30_days"`
LikeCount int `json:"like_count"`
Likes7Days int `json:"likes_7_days"`
Likes30Days int `json:"likes_30_days"`
} `json:"stats"`
Description string `json:"description"`
Title string `json:"title"`
Locale string `json:"locale"`
Version string `json:"version"`
HTTPS bool `json:"https"`
Moderators []struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
AvatarTemplate string `json:"avatar_template"`
Title string `json:"title"`
LastSeenAt time.Time `json:"last_seen_at"`
} `json:"moderators"`
Admins []struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
AvatarTemplate string `json:"avatar_template"`
Title string `json:"title"`
LastSeenAt time.Time `json:"last_seen_at"`
} `json:"admins"`
} `json:"about"`
About struct {
Stats struct {
TopicCount int `json:"topic_count"`
PostCount int `json:"post_count"`
UserCount int `json:"user_count"`
Topics7Days int `json:"topics_7_days"`
Topics30Days int `json:"topics_30_days"`
Posts7Days int `json:"posts_7_days"`
Posts30Days int `json:"posts_30_days"`
Users7Days int `json:"users_7_days"`
Users30Days int `json:"users_30_days"`
ActiveUsers7Days int `json:"active_users_7_days"`
ActiveUsers30Days int `json:"active_users_30_days"`
LikeCount int `json:"like_count"`
Likes7Days int `json:"likes_7_days"`
Likes30Days int `json:"likes_30_days"`
} `json:"stats"`
Description string `json:"description"`
Title string `json:"title"`
Locale string `json:"locale"`
Version string `json:"version"`
HTTPS bool `json:"https"`
Moderators []struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
AvatarTemplate string `json:"avatar_template"`
Title string `json:"title"`
LastSeenAt time.Time `json:"last_seen_at"`
} `json:"moderators"`
Admins []struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
AvatarTemplate string `json:"avatar_template"`
Title string `json:"title"`
LastSeenAt time.Time `json:"last_seen_at"`
} `json:"admins"`
} `json:"about"`
}
// TagTopics list of topics via tag
type TagTopics struct {
TopicList struct {
Topics []struct {
Slug string `json:"slug"`
} `json:"topics"`
} `json:"topic_list"`
TopicList struct {
Topics []struct {
Slug string `json:"slug"`
} `json:"topics"`
} `json:"topic_list"`
}
// Topic data
type Topic struct {
PostStream struct {
Posts []struct {
Cooked string `json:"cooked"`
UserTitle string `json:"user_title"`
} `json:"posts"`
} `json:"post_stream"`
Tags []string `json:"tags"`
ID int `json:"id"`
Title string `json:"title"`
CreatedAt time.Time `json:"created_at"`
Slug string `json:"slug"`
Details struct {
CreatedBy struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
AvatarTemplate string `json:"avatar_template"`
} `json:"created_by"`
} `json:"details"`
PostStream struct {
Posts []struct {
Cooked string `json:"cooked"`
UserTitle string `json:"user_title"`
} `json:"posts"`
} `json:"post_stream"`
Tags []string `json:"tags"`
ID int `json:"id"`
Title string `json:"title"`
CreatedAt time.Time `json:"created_at"`
Slug string `json:"slug"`
Details struct {
CreatedBy struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
AvatarTemplate string `json:"avatar_template"`
} `json:"created_by"`
} `json:"details"`
}

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
}