mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
This commit is contained in:
44
_examples/database/mysql/main.go
Normal file
44
_examples/database/mysql/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"myapp/api"
|
||||
"myapp/sql"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?parseTime=true&charset=utf8mb4&collation=utf8mb4_unicode_ci",
|
||||
getenv("MYSQL_USER", "user_myapp"),
|
||||
getenv("MYSQL_PASSWORD", "dbpassword"),
|
||||
getenv("MYSQL_HOST", "localhost"),
|
||||
getenv("MYSQL_DATABASE", "myapp"),
|
||||
)
|
||||
|
||||
db, err := sql.ConnectMySQL(dsn)
|
||||
if err != nil {
|
||||
log.Fatalf("error connecting to the MySQL database: %v", err)
|
||||
}
|
||||
|
||||
secret := getenv("JWT_SECRET", "EbnJO3bwmX")
|
||||
|
||||
app := iris.New()
|
||||
subRouter := api.Router(db, secret)
|
||||
app.PartyFunc("/", subRouter)
|
||||
|
||||
addr := fmt.Sprintf(":%s", getenv("PORT", "8080"))
|
||||
app.Listen(addr)
|
||||
}
|
||||
|
||||
func getenv(key string, def string) string {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
return def
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
Reference in New Issue
Block a user