Some Structure

This commit is contained in:
Jan Barfuß 2024-03-21 19:32:40 +01:00
parent e26aa4fb31
commit 62577d4c41
10 changed files with 160 additions and 92 deletions

View File

@ -1,14 +0,0 @@
package fetch
import (
"net/http"
)
func Fetch(url string) (*http.Response, error) {
u, err := url.ParseRequestURI(url)
if err != nil {
return nil, err
}
return http.Get(url)
}

View File

@ -1,78 +0,0 @@
package main
import (
"io"
"net/http"
"net/url"
"strings"
"golang.org/x/net/html"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/bl/:bundesland", bundesland)
r.Run("localhost:8080")
}
func bundesland(c *gin.Context) {
bundesland := c.Param("bundesland")
if bundesland == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "bundesland is required",
})
return
}
resp, err := Fetch("bl/" + bundesland)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
defer resp.Body.Close()
d, err := io.ReadAll(resp.Body)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
html := string(d)
scraped := ScrapeBundesland(html)
c.JSON(http.StatusOK, scraped)
}
func Fetch(path string) (*http.Response, error) {
baseurl := "https://www.imensa.de/"
queryurl := baseurl + "/" + path
u, err := url.ParseRequestURI(queryurl)
if err != nil {
return nil, err
}
return http.Get(u.String())
}
func ScrapeBundesland(h string) []string {
tkn := html.NewTokenizer(strings.NewReader(h))
var mensen []string
for {
if tkn.Next() == html.ErrorToken {
return mensen
}
t := tkn.Token()
attr := t.Attr
for _, a := range attr {
if a.Key == "class" && a.Val == "elements" {
print(t.Data)
mensen = append(mensen, t.Data)
}
}
}
}

18
cache/cache.go vendored Normal file
View File

@ -0,0 +1,18 @@
package cache
import (
"mensa/types"
)
func NewCache() *types.Cache {
cache := types.Cache{}
return &cache
}
func setCacheData(cache *types.Cache, key string, data string, lifetime ...int64) {
cache.SetData(key, data, lifetime...)
}
func getCacheData(cache *types.Cache) string {
return cache.GetData()
}

16
fetch/fetch.go Normal file
View File

@ -0,0 +1,16 @@
package fetch
import (
"net/http"
"net/url"
)
func Fetch(path string) (*http.Response, error) {
baseurl := "https://www.imensa.de/"
queryurl := baseurl + "/" + path
u, err := url.ParseRequestURI(queryurl)
if err != nil {
return nil, err
}
return http.Get(u.String())
}

9
main.go Normal file
View File

@ -0,0 +1,9 @@
package main
import (
"mensa/server"
)
func main() {
server.Run()
}

29
scrape/scrape.go Normal file
View File

@ -0,0 +1,29 @@
package scrape
import (
"strings"
"golang.org/x/net/html"
)
func ScrapeBundesland(h string) []string {
tkn := html.NewTokenizer(strings.NewReader(h))
var mensen []string
for {
if tkn.Next() == html.ErrorToken {
return mensen
}
t := tkn.Token()
attr := t.Attr
for _, a := range attr {
if a.Key == "class" && a.Val == "elements" {
print(t.Data)
mensen = append(mensen, t.Data)
}
}
}
}

53
server/server.go Normal file
View File

@ -0,0 +1,53 @@
package server
import (
"io"
"mensa/cache"
"mensa/fetch"
"mensa/scrape"
"mensa/types"
"net/http"
"github.com/gin-gonic/gin"
)
var c = cache.NewCache()
func Run() {
r := gin.Default()
r.GET("/bl/:bundesland", bundesland)
r.Run("localhost:8080")
}
func bundesland(c *gin.Context) {
bundesland := c.Param("bundesland")
if bundesland == "" {
c.JSON(http.StatusBadRequest, gin.H{
"error": "bundesland is required",
})
return
}
if c.GetCacheData(bundesland) != "" {
return c.GetCacheData(bundesland)
}
resp, err := fetch.Fetch("bl/" + bundesland)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
defer resp.Body.Close()
d, err := io.ReadAll(resp.Body)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
return
}
html := string(d)
scraped := scrape.ScrapeBundesland(html)
c.JSON(http.StatusOK, scraped)
}

1
types/bundesland.go Normal file
View File

@ -0,0 +1 @@
package types

33
types/cache.go Normal file
View File

@ -0,0 +1,33 @@
package types
import "time"
type Cache struct {
data string
lastUpdated time.Time
lifetime int64
key string
}
func (c *Cache) SetData(key string, data string, lifetime ...int64) {
if len(lifetime) > 0 {
c.lifetime = lifetime[0]
} else {
c.lifetime = 60
}
c.data = data
c.lastUpdated = time.Now()
}
func (c *Cache) GetData() string {
if time.Now().Unix()-c.lastUpdated.Unix() > c.lifetime {
return ""
}
if c.data == "" {
return ""
}
return c.data
}

1
types/mensa.go Normal file
View File

@ -0,0 +1 @@
package types