Merge pull request #1 from oscarmlage/new-skel

New skel
main
Óscar M. Lage 2022-03-18 23:41:25 +01:00 committed by GitHub
commit 35ca8cc5ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 152 additions and 55 deletions

View File

@ -6,6 +6,7 @@ import (
)
func main() {
fmt.Println("Hola mundo")
fmt.Println("Starting wikingo...")
server.Serve()
}

View File

@ -1,42 +1,9 @@
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)
}

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("Lab: Store Open")
}
func (s *StoreLab) AddPage() {
fmt.Println("Lab: Page created")
}
func (s *StoreLab) GetPage() {
fmt.Println("Lab: Get Page")
}
func (s *StoreLab) GetAllPages() {
fmt.Println("Lab: Get All Pages")
}

29
server/app.go Normal file
View File

@ -0,0 +1,29 @@
package server
import (
"fmt"
"github.com/labstack/echo/v4"
"log"
"net/http"
)
func WikiHome(c echo.Context) error {
res := store.GetPage()
fmt.Println(res)
return c.String(http.StatusOK, "WikiHome")
}
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")
}

View File

@ -1,36 +1,35 @@
package server
import (
"fmt"
"github.com/labstack/echo/v4"
"github.com/oscarmlage/wikingo/model"
"log"
"net/http"
)
var store *model.Store
var (
store model.Store
)
func Serve() {
store = &model.Store{}
// Store instance
// Depending on config we should open one store or other
// (Gorm, File, Git...)
store = new(model.StoreGorm)
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"))
}
func WikiHome(c echo.Context) error {
res := store.GetPage()
fmt.Println(res)
return c.String(http.StatusOK, "WikiHome")
}
func WikiAbout(c echo.Context) error {
id := c.Param("id")
return c.String(http.StatusOK, "About the wiki. id:"+id)
}

1
views/base.html Normal file
View File

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