New skel (WIP)

main
Óscar M. Lage 2022-03-18 13:15:55 +01:00
parent 08488127f8
commit 0a392ac639
8 changed files with 145 additions and 42 deletions

16
main.go
View File

@ -2,10 +2,24 @@ package main
import (
"fmt"
"github.com/labstack/echo/v4"
"github.com/oscarmlage/wikingo/server"
"html/template"
"io"
)
// Define the template registry struct
type TemplateRegistry struct {
templates *template.Template
}
// Implement e.Renderer intercace
func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
fmt.Println("Hola mundo")
fmt.Println("Starting wikingo...")
server.Serve()
}

View File

@ -1,42 +1,10 @@
package model
import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Store struct {
db *gorm.DB
}
func (s *Store) Open() error {
var err error
s.db, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
return err
}
fmt.Printf("%s\n", s.db.Name())
// Migrate the Schema
s.db.AutoMigrate(&Page{})
// Create
s.addPage()
return nil
}
func (s *Store) GetPage() Page {
var page Page
s.db.First(&page)
return page
}
func (s *Store) addPage() {
s.db.Create(&Page{Code: "D55", Price: 200})
fmt.Println("Page created")
}
func getUser() {
// Store interface
type Store interface {
Open() error
AddPage()
GetPage() Page
GetAllPages() ([]Page, error)
Noexiste()
}

27
model/store_file.go Normal file
View File

@ -0,0 +1,27 @@
package model
import (
"fmt"
)
type StoreFile struct {
db string
s Store
}
func (s *StoreFile) Open() error {
fmt.Printf("File: Store Open")
return nil
}
func (s *StoreFile) AddPage() {
fmt.Println("File: Page created")
}
func (s *StoreFile) GetPage() {
fmt.Println("File: Get Page")
}
func (s *StoreFile) GetAllPages() {
fmt.Println("File: Get All Pages")
}

47
model/store_gorm.go Normal file
View File

@ -0,0 +1,47 @@
package model
import (
"fmt"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type StoreGorm struct {
db *gorm.DB
store Store
}
func (s *StoreGorm) Open() error {
var err error
s.db, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
return err
}
fmt.Printf("%s\n", s.db.Name())
// Migrate the Schema
s.db.AutoMigrate(&Page{})
return nil
}
func (s *StoreGorm) AddPage() {
s.db.Create(&Page{Code: "D55", Price: 200})
fmt.Println("Page created")
}
func (s *StoreGorm) GetPage() Page {
var page Page
s.db.First(&page)
return page
}
func (s *StoreGorm) GetAllPages() ([]Page, error) {
var pages []Page
tx := s.db.Find(&pages)
if tx.Error != nil {
return []Page{}, tx.Error
}
return pages, nil
}

26
model/store_lab.go Normal file
View File

@ -0,0 +1,26 @@
package model
import (
"fmt"
)
type StoreLab struct {
db string
s Store
}
func (s *StoreLab) Open() {
fmt.Printf("File: Store Open")
}
func (s *StoreLab) AddPage() {
fmt.Println("File: Page created")
}
func (s *StoreLab) GetPage() {
fmt.Println("File: Get Page")
}
func (s *StoreLab) GetAllPages() {
fmt.Println("File: Get All Pages")
}

View File

@ -8,19 +8,29 @@ import (
"net/http"
)
var store *model.Store
// Depending on config we should open one store or other (Gorm, File,
// Git...)
var (
store model.StoreGorm
)
func Serve() {
store = &model.Store{}
// Store instance
err := store.Open()
if err != nil {
log.Panicln(err)
}
// Echo instance
e := echo.New()
// Routes
e.GET("/", WikiHome)
e.GET("/list", WikiListPages)
e.GET("/about", WikiAbout)
e.GET("/about/:id", WikiAbout)
// Logger
e.Logger.Fatal(e.Start(":2323"))
}
@ -34,3 +44,13 @@ func WikiAbout(c echo.Context) error {
id := c.Param("id")
return c.String(http.StatusOK, "About the wiki. id:"+id)
}
func WikiListPages(c echo.Context) error {
fmt.Println("WikiListPages")
res, err := store.GetAllPages()
if err != nil {
log.Panicln(err)
}
fmt.Println(res)
return c.String(http.StatusOK, "WikiListPages")
}

1
views/base.html Normal file
View File

@ -0,0 +1 @@
<h1>Wikingo</h1>